Skip to content

Commit

Permalink
Make min burn amount configurable by chain (#48)
Browse files Browse the repository at this point in the history
* min burn amount by chain

* fix type

* add logging

* clean up

* feedback
  • Loading branch information
boojamya authored Feb 20, 2024
1 parent e6d21ae commit 002c615
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
38 changes: 28 additions & 10 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
cctptypes "github.com/circlefin/noble-cctp/x/cctp/types"
"github.com/spf13/cobra"
"github.com/strangelove-ventures/noble-cctp-relayer/circle"
"github.com/strangelove-ventures/noble-cctp-relayer/ethereum"
"github.com/strangelove-ventures/noble-cctp-relayer/noble"
"github.com/strangelove-ventures/noble-cctp-relayer/types"
)

Expand Down Expand Up @@ -187,32 +189,48 @@ func filterInvalidDestinationCallers(registeredDomains map[types.Domain]types.Ch
return !chain.IsDestinationCaller(msg.DestinationCaller)
}

// filterLowTransfers returns true if the amount being transfered to the destination chain is lower than the min-mint-amount configured
func filterLowTransfers(cfg *types.Config, logger log.Logger, msg *types.MessageState) bool {
bm, err := new(cctptypes.BurnMessage).Parse(msg.MsgBody)
if err != nil {
logger.Info("This is not a burn message", "err", err)
return true
}

if bm.Amount.LT(math.NewIntFromUint64(cfg.MinAmount)) {
// TODO: not assume that "noble" is domain 4, add "domain" to the noble chain conifg
var minBurnAmount uint64
if msg.DestDomain == types.Domain(4) {
nobleCfg, ok := cfg.Chains["noble"].(*noble.ChainConfig)
if !ok {
logger.Info("chain named 'noble' not found in config, filtering transaction")
return true
}
minBurnAmount = nobleCfg.MinMintAmount
} else {
for _, chain := range cfg.Chains {
c, ok := chain.(*ethereum.ChainConfig)
if !ok {
// noble chain, handled above
continue
}
if c.Domain == msg.DestDomain {
minBurnAmount = c.MinMintAmount
}
}
}

if bm.Amount.LT(math.NewIntFromUint64(minBurnAmount)) {
logger.Info(
"Filtered tx because the transfer amount is less than the minimum allowed amount",
"dest domain", msg.DestDomain,
"source_domain", msg.SourceDomain,
"source_tx", msg.SourceTxHash,
"amount", bm.Amount,
"min_amount", cfg.MinAmount,
"min_amount", minBurnAmount,
)
return true
}

logger.Info(
"Not filtering tx due to low transfer amount",
"source_domain", msg.SourceDomain,
"source_tx", msg.SourceTxHash,
"amount", bm.Amount.Uint64(),
"min_amount", cfg.MinAmount,
)

return false
}

Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func Parse(file string) (*types.Config, error) {
ProcessorWorkerCount: cfg.ProcessorWorkerCount,
Api: cfg.Api,
Chains: make(map[string]types.ChainConfig),
MinAmount: cfg.MinAmount,
}

for name, chain := range cfg.Chains {
Expand Down
4 changes: 4 additions & 0 deletions config/sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ chains:
broadcast-retries: 5 # number of times to attempt the broadcast
broadcast-retry-interval: 10 # time between retries in seconds

min-mint-amount: 10000000

minter-private-key: # private key


Expand All @@ -30,6 +32,8 @@ chains:

block-queue-channel-size: 1000000 # 1000000 is a safe default, increase number if starting from a very early block

min-mint-amount: 0

minter-private-key: # hex encoded privateKey

# source domain id -> destination domain id
Expand Down
3 changes: 3 additions & 0 deletions ethereum/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Ethereum struct {
minterAddress string
maxRetries int
retryIntervalSeconds int
minAmount uint64

mu sync.Mutex
}
Expand All @@ -45,6 +46,7 @@ func NewChain(
privateKey string,
maxRetries int,
retryIntervalSeconds int,
minAmount uint64,
) (*Ethereum, error) {
privEcdsaKey, ethereumAddress, err := GetEcdsaKeyAddress(privateKey)
if err != nil {
Expand All @@ -63,6 +65,7 @@ func NewChain(
minterAddress: ethereumAddress,
maxRetries: maxRetries,
retryIntervalSeconds: retryIntervalSeconds,
minAmount: minAmount,
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions ethereum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type ChainConfig struct {
BroadcastRetries int `yaml:"broadcast-retries"`
BroadcastRetryInterval int `yaml:"broadcast-retry-interval"`

MinMintAmount uint64 `yaml:"min-mint-amount"`

// TODO move to keyring
MinterPrivateKey string `yaml:"minter-private-key"`
}
Expand All @@ -36,5 +38,6 @@ func (c *ChainConfig) Chain(name string) (types.Chain, error) {
c.MinterPrivateKey,
c.BroadcastRetries,
c.BroadcastRetryInterval,
c.MinMintAmount,
)
}
3 changes: 3 additions & 0 deletions noble/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Noble struct {
maxRetries int
retryIntervalSeconds int
blockQueueChannelSize uint64
minAmount uint64

mu sync.Mutex
}
Expand All @@ -51,6 +52,7 @@ func NewChain(
maxRetries int,
retryIntervalSeconds int,
blockQueueChannelSize uint64,
minAmount uint64,
) (*Noble, error) {
cc, err := cosmos.NewProvider(rpcURL)
if err != nil {
Expand Down Expand Up @@ -80,6 +82,7 @@ func NewChain(
maxRetries: maxRetries,
retryIntervalSeconds: retryIntervalSeconds,
blockQueueChannelSize: blockQueueChannelSize,
minAmount: minAmount,
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions noble/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type ChainConfig struct {

BlockQueueChannelSize uint64 `yaml:"block-queue-channel-size"`

MinMintAmount uint64 `yaml:"min-mint-amount"`

// TODO move to keyring
MinterPrivateKey string `yaml:"minter-private-key"`
}
Expand All @@ -38,5 +40,6 @@ func (c *ChainConfig) Chain(name string) (types.Chain, error) {
c.BroadcastRetries,
c.BroadcastRetryInterval,
c.BlockQueueChannelSize,
c.MinMintAmount,
)
}
2 changes: 0 additions & 2 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Config struct {
Api struct {
TrustedProxies []string `yaml:"trusted-proxies"`
} `yaml:"api"`
MinAmount uint64 `yaml:"min-amount"`
}

type ConfigWrapper struct {
Expand All @@ -27,7 +26,6 @@ type ConfigWrapper struct {
Api struct {
TrustedProxies []string `yaml:"trusted-proxies"`
} `yaml:"api"`
MinAmount uint64 `yaml:"min-amount"`
}

type ChainConfig interface {
Expand Down

0 comments on commit 002c615

Please sign in to comment.