From fbfb9436635f37fc6e57c21992082c3e2b304cee Mon Sep 17 00:00:00 2001 From: Zodomo Date: Mon, 18 Nov 2024 15:37:59 -0600 Subject: [PATCH 1/3] fix deployment of feeoraclev2 --- lib/contracts/feeoraclev2/deploy.go | 17 +++++++++-------- lib/contracts/feeoraclev2/feeparams.go | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/contracts/feeoraclev2/deploy.go b/lib/contracts/feeoraclev2/deploy.go index 69fe098fb..c35916e79 100644 --- a/lib/contracts/feeoraclev2/deploy.go +++ b/lib/contracts/feeoraclev2/deploy.go @@ -25,7 +25,7 @@ type DeploymentConfig struct { Owner common.Address Deployer common.Address Manager common.Address // manager is the address that can set fee parameters (gas price, conversion rates) - BaseGasLimit uint32 // must fit in uint24 (max: 16,777,215) + BaseGasLimit *big.Int // must fit in uint24 (max: 16,777,215) ProtocolFee *big.Int // must fit in uint72 (max: 4,722,366,482,869,645,213,695) } @@ -33,6 +33,7 @@ func isEmpty(addr common.Address) bool { return addr == common.Address{} } +var maxUint24 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 24), big.NewInt(1)) var maxUint72 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 72), big.NewInt(1)) func (cfg DeploymentConfig) Validate() error { @@ -57,7 +58,7 @@ func (cfg DeploymentConfig) Validate() error { if isEmpty(cfg.Manager) { return errors.New("manager is zero") } - if cfg.BaseGasLimit > (1<<24 - 1) { + if cfg.BaseGasLimit.Cmp(maxUint24) > 0 { return errors.New("base gas limit too high") } if cfg.ProtocolFee.Cmp(maxUint72) > 0 { @@ -125,14 +126,14 @@ func Deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainID Owner: eoa.MustAddress(network, eoa.RoleManager), Deployer: eoa.MustAddress(network, eoa.RoleDeployer), Manager: eoa.MustAddress(network, eoa.RoleMonitor), // NOTE: monitor is owner of fee oracle contracts, because monitor manages on chain gas prices / conversion rates - BaseGasLimit: 100_000, + BaseGasLimit: big.NewInt(100_000), ProtocolFee: big.NewInt(0), } - return deploy(ctx, chainID, destChainIDs, cfg, backend, backends) + return deploy(ctx, network, chainID, destChainIDs, cfg, backend, backends) } -func deploy(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backend *ethbackend.Backend, backends ethbackend.Backends) (common.Address, *ethtypes.Receipt, error) { +func deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backend *ethbackend.Backend, backends ethbackend.Backends) (common.Address, *ethtypes.Receipt, error) { if err := cfg.Validate(); err != nil { return common.Address{}, nil, errors.Wrap(err, "validate config") } @@ -166,7 +167,7 @@ func deploy(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg Depl return common.Address{}, nil, errors.Wrap(err, "wait mined implementation") } - initCode, err := packInitCode(ctx, chainID, destChainIDs, cfg, backends, impl) + initCode, err := packInitCode(ctx, network, chainID, destChainIDs, cfg, backends, impl) if err != nil { return common.Address{}, nil, errors.Wrap(err, "pack init code") } @@ -184,7 +185,7 @@ func deploy(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg Depl return addr, receipt, nil } -func packInitCode(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backends ethbackend.Backends, impl common.Address) ([]byte, error) { +func packInitCode(ctx context.Context, network netconf.ID, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backends ethbackend.Backends, impl common.Address) ([]byte, error) { feeOracleAbi, err := bindings.FeeOracleV2MetaData.GetAbi() if err != nil { return nil, errors.Wrap(err, "get fee oracle abi") @@ -195,7 +196,7 @@ func packInitCode(ctx context.Context, chainID uint64, destChainIDs []uint64, cf return nil, errors.Wrap(err, "get proxy abi") } - feeparams, err := feeParams(ctx, chainID, destChainIDs, backends, coingecko.New()) + feeparams, err := feeParams(ctx, network, chainID, destChainIDs, backends, coingecko.New()) if err != nil { return nil, errors.Wrap(err, "fee params") } diff --git a/lib/contracts/feeoraclev2/feeparams.go b/lib/contracts/feeoraclev2/feeparams.go index 7b838f160..b4d6a01b6 100644 --- a/lib/contracts/feeoraclev2/feeparams.go +++ b/lib/contracts/feeoraclev2/feeparams.go @@ -9,13 +9,14 @@ import ( "github.com/omni-network/omni/lib/ethclient/ethbackend" "github.com/omni-network/omni/lib/evmchain" "github.com/omni-network/omni/lib/log" + "github.com/omni-network/omni/lib/netconf" "github.com/omni-network/omni/lib/tokens" "github.com/omni-network/omni/monitor/xfeemngr/gasprice" "github.com/ethereum/go-ethereum/params" ) -func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, backends ethbackend.Backends, pricer tokens.Pricer, +func feeParams(ctx context.Context, network netconf.ID, srcChainID uint64, destChainIDs []uint64, backends ethbackend.Backends, pricer tokens.Pricer, ) ([]bindings.IFeeOracleV2FeeParams, error) { // used cached pricer, to avoid multiple price requests for same token pricer = tokens.NewCachedPricer(pricer) @@ -32,9 +33,20 @@ func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, ba return nil, errors.New("meta by chain id", "dest_chain", destChain.Name) } - ps, err := destFeeParams(ctx, srcChain, destChain, backends, pricer) - if err != nil { - return nil, err + var ps bindings.IFeeOracleV2FeeParams + if network == netconf.Mainnet { + var err error + ps, err = destFeeParams(ctx, srcChain, destChain, backends, pricer) + if err != nil { + return nil, err + } + } else { + ps = bindings.IFeeOracleV2FeeParams{ + ChainId: destChain.ChainID, + ExecGasPrice: params.GWei, + DataGasPrice: params.GWei, + ToNativeRate: 1_000_000, + } } resp = append(resp, ps) From f4ba1983be706807932f77a9d4baf81641cb9722 Mon Sep 17 00:00:00 2001 From: Zodomo Date: Mon, 18 Nov 2024 15:58:35 -0600 Subject: [PATCH 2/3] default to 1 gwei gas price only when chain cannot be found --- lib/contracts/feeoraclev2/deploy.go | 10 +++++----- lib/contracts/feeoraclev2/feeparams.go | 14 ++++---------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/contracts/feeoraclev2/deploy.go b/lib/contracts/feeoraclev2/deploy.go index c35916e79..2981d43c9 100644 --- a/lib/contracts/feeoraclev2/deploy.go +++ b/lib/contracts/feeoraclev2/deploy.go @@ -130,10 +130,10 @@ func Deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainID ProtocolFee: big.NewInt(0), } - return deploy(ctx, network, chainID, destChainIDs, cfg, backend, backends) + return deploy(ctx, chainID, destChainIDs, cfg, backend, backends) } -func deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backend *ethbackend.Backend, backends ethbackend.Backends) (common.Address, *ethtypes.Receipt, error) { +func deploy(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backend *ethbackend.Backend, backends ethbackend.Backends) (common.Address, *ethtypes.Receipt, error) { if err := cfg.Validate(); err != nil { return common.Address{}, nil, errors.Wrap(err, "validate config") } @@ -167,7 +167,7 @@ func deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainID return common.Address{}, nil, errors.Wrap(err, "wait mined implementation") } - initCode, err := packInitCode(ctx, network, chainID, destChainIDs, cfg, backends, impl) + initCode, err := packInitCode(ctx, chainID, destChainIDs, cfg, backends, impl) if err != nil { return common.Address{}, nil, errors.Wrap(err, "pack init code") } @@ -185,7 +185,7 @@ func deploy(ctx context.Context, network netconf.ID, chainID uint64, destChainID return addr, receipt, nil } -func packInitCode(ctx context.Context, network netconf.ID, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backends ethbackend.Backends, impl common.Address) ([]byte, error) { +func packInitCode(ctx context.Context, chainID uint64, destChainIDs []uint64, cfg DeploymentConfig, backends ethbackend.Backends, impl common.Address) ([]byte, error) { feeOracleAbi, err := bindings.FeeOracleV2MetaData.GetAbi() if err != nil { return nil, errors.Wrap(err, "get fee oracle abi") @@ -196,7 +196,7 @@ func packInitCode(ctx context.Context, network netconf.ID, chainID uint64, destC return nil, errors.Wrap(err, "get proxy abi") } - feeparams, err := feeParams(ctx, network, chainID, destChainIDs, backends, coingecko.New()) + feeparams, err := feeParams(ctx, chainID, destChainIDs, backends, coingecko.New()) if err != nil { return nil, errors.Wrap(err, "fee params") } diff --git a/lib/contracts/feeoraclev2/feeparams.go b/lib/contracts/feeoraclev2/feeparams.go index b4d6a01b6..d32621a65 100644 --- a/lib/contracts/feeoraclev2/feeparams.go +++ b/lib/contracts/feeoraclev2/feeparams.go @@ -9,14 +9,13 @@ import ( "github.com/omni-network/omni/lib/ethclient/ethbackend" "github.com/omni-network/omni/lib/evmchain" "github.com/omni-network/omni/lib/log" - "github.com/omni-network/omni/lib/netconf" "github.com/omni-network/omni/lib/tokens" "github.com/omni-network/omni/monitor/xfeemngr/gasprice" "github.com/ethereum/go-ethereum/params" ) -func feeParams(ctx context.Context, network netconf.ID, srcChainID uint64, destChainIDs []uint64, backends ethbackend.Backends, pricer tokens.Pricer, +func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, backends ethbackend.Backends, pricer tokens.Pricer, ) ([]bindings.IFeeOracleV2FeeParams, error) { // used cached pricer, to avoid multiple price requests for same token pricer = tokens.NewCachedPricer(pricer) @@ -33,14 +32,9 @@ func feeParams(ctx context.Context, network netconf.ID, srcChainID uint64, destC return nil, errors.New("meta by chain id", "dest_chain", destChain.Name) } - var ps bindings.IFeeOracleV2FeeParams - if network == netconf.Mainnet { - var err error - ps, err = destFeeParams(ctx, srcChain, destChain, backends, pricer) - if err != nil { - return nil, err - } - } else { + ps, err := destFeeParams(ctx, srcChain, destChain, backends, pricer) + if err != nil { + log.Warn(ctx, "Failed getting fee params, defaulting to 1 gwei gas prices", err, "src_chain", srcChain.Name, "dest_chain", destChain.Name) ps = bindings.IFeeOracleV2FeeParams{ ChainId: destChain.ChainID, ExecGasPrice: params.GWei, From 70bc7cf9df9f4971e0f162c6c0183df488c00164 Mon Sep 17 00:00:00 2001 From: Zodomo Date: Mon, 18 Nov 2024 16:52:05 -0600 Subject: [PATCH 3/3] apply default 1 Gwei gas price only for backends that have issues --- lib/contracts/feeoraclev2/feeparams.go | 51 +++++++++++++------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/lib/contracts/feeoraclev2/feeparams.go b/lib/contracts/feeoraclev2/feeparams.go index d32621a65..626f6cb8e 100644 --- a/lib/contracts/feeoraclev2/feeparams.go +++ b/lib/contracts/feeoraclev2/feeparams.go @@ -32,16 +32,7 @@ func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, ba return nil, errors.New("meta by chain id", "dest_chain", destChain.Name) } - ps, err := destFeeParams(ctx, srcChain, destChain, backends, pricer) - if err != nil { - log.Warn(ctx, "Failed getting fee params, defaulting to 1 gwei gas prices", err, "src_chain", srcChain.Name, "dest_chain", destChain.Name) - ps = bindings.IFeeOracleV2FeeParams{ - ChainId: destChain.ChainID, - ExecGasPrice: params.GWei, - DataGasPrice: params.GWei, - ToNativeRate: 1_000_000, - } - } + ps := destFeeParams(ctx, srcChain, destChain, backends, pricer) resp = append(resp, ps) } @@ -51,7 +42,7 @@ func feeParams(ctx context.Context, srcChainID uint64, destChainIDs []uint64, ba // feeParams returns the fee parameters for the given source token and destination chains. func destFeeParams(ctx context.Context, srcChain evmchain.Metadata, destChain evmchain.Metadata, backends ethbackend.Backends, pricer tokens.Pricer, -) (bindings.IFeeOracleV2FeeParams, error) { +) bindings.IFeeOracleV2FeeParams { // conversion rate from "dest token" to "src token" // ex if dest chain is ETH, and src chain is OMNI, we need to know the rate of ETH to OMNI. toNativeRate, err := conversionRate(ctx, pricer, destChain.NativeToken, srcChain.NativeToken) @@ -60,26 +51,34 @@ func destFeeParams(ctx context.Context, srcChain evmchain.Metadata, destChain ev toNativeRate = 1 } - localBackend, err := backends.Backend(destChain.ChainID) + // Get execution gas price, defaulting to 1 Gwei if any error occurs. + var execBackend *ethbackend.Backend + var execGasPrice *big.Int + execBackend, err = backends.Backend(destChain.ChainID) if err != nil { - return bindings.IFeeOracleV2FeeParams{}, errors.Wrap(err, "get local backend", "dest_chain", destChain.Name) - } - - remoteBackend, err := backends.Backend(destChain.PostsTo) - if err != nil { - return bindings.IFeeOracleV2FeeParams{}, errors.Wrap(err, "get remote backend", "dest_chain", destChain.Name) - } - - execGasPrice, err := localBackend.SuggestGasPrice(ctx) - if err != nil { - log.Warn(ctx, "Failed fetching exec gas price, using default 1 Gwei", err, "dest_chain", destChain.Name) + log.Warn(ctx, "Failed getting exec backend, using default 1 Gwei", err, "dest_chain", destChain.Name) execGasPrice = big.NewInt(params.GWei) + } else { + execGasPrice, err = execBackend.SuggestGasPrice(ctx) + if err != nil { + log.Warn(ctx, "Failed fetching exec gas price, using default 1 Gwei", err, "dest_chain", destChain.Name) + execGasPrice = big.NewInt(params.GWei) + } } - dataGasPrice, err := remoteBackend.SuggestGasPrice(ctx) + // Get data gas price, defaulting to 1 Gwei if any error occurs. + var dataBackend *ethbackend.Backend + var dataGasPrice *big.Int + dataBackend, err = backends.Backend(destChain.PostsTo) if err != nil { - log.Warn(ctx, "Failed fetching data gas price, using default 1 Gwei", err, "dest_chain", destChain.Name) + log.Warn(ctx, "Failed getting data backend, using default 1 Gwei", err, "dest_chain", destChain.Name) dataGasPrice = big.NewInt(params.GWei) + } else { + dataGasPrice, err = dataBackend.SuggestGasPrice(ctx) + if err != nil { + log.Warn(ctx, "Failed fetching data gas price, using default 1 Gwei", err, "dest_chain", destChain.Name) + dataGasPrice = big.NewInt(params.GWei) + } } return bindings.IFeeOracleV2FeeParams{ @@ -87,7 +86,7 @@ func destFeeParams(ctx context.Context, srcChain evmchain.Metadata, destChain ev ExecGasPrice: gasprice.Tier(execGasPrice.Uint64()), DataGasPrice: gasprice.Tier(dataGasPrice.Uint64()), ToNativeRate: rateToNumerator(toNativeRate), - }, nil + } } // conversionRate returns the conversion rate C from token F to token T, where C = price(F) / price(T).