From 5fed6e63c522255458fac1a40b53065411328a7f Mon Sep 17 00:00:00 2001 From: charliec Date: Wed, 9 Aug 2023 15:37:43 -0500 Subject: [PATCH] use dynamic gas price for Ethereum chains --- common/chain.go | 9 ++++++++- zetaclient/evm_signer.go | 42 +++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/common/chain.go b/common/chain.go index 3c395b540b..f8509cb702 100644 --- a/common/chain.go +++ b/common/chain.go @@ -2,10 +2,11 @@ package common import ( "fmt" + "strings" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcutil" ethcommon "github.com/ethereum/go-ethereum/common" - "strings" ) var ( @@ -94,6 +95,12 @@ func IsBitcoinChain(chainID int64) bool { chainID == 8332 // mainnet } +func IsEthereumChain(chainID int64) bool { + return chainID == 1 || // eth mainnet + chainID == 5 || // Goerli + chainID == 1337 // eth privnet +} + // IsEmpty is to determinate whether the chain is empty func (chain Chain) IsEmpty() bool { return strings.TrimSpace(chain.String()) == "" diff --git a/zetaclient/evm_signer.go b/zetaclient/evm_signer.go index 7a7028b2fc..6242c94c00 100644 --- a/zetaclient/evm_signer.go +++ b/zetaclient/evm_signer.go @@ -5,6 +5,12 @@ import ( "encoding/base64" "encoding/hex" "fmt" + "math/big" + "math/rand" + "strconv" + "strings" + "time" + "github.com/ethereum/go-ethereum/accounts/abi" ethcommon "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -15,11 +21,6 @@ import ( "github.com/zeta-chain/zetacore/common" "github.com/zeta-chain/zetacore/x/crosschain/types" zetaObserverModuleTypes "github.com/zeta-chain/zetacore/x/observer/types" - "math/big" - "math/rand" - "strconv" - "strings" - "time" ) type EVMSigner struct { @@ -285,10 +286,23 @@ func (signer *EVMSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out } var sendhash [32]byte copy(sendhash[:32], sendHash[:32]) - gasprice, ok := new(big.Int).SetString(send.GetCurrentOutTxParam().OutboundTxGasPrice, 10) - if !ok { - logger.Error().Err(err).Msgf("cannot convert gas price %s ", send.GetCurrentOutTxParam().OutboundTxGasPrice) - return + + // use dynamic gas price for ethereum chains + var gasprice *big.Int + if common.IsEthereumChain(toChain.ChainId) { + suggested, err := signer.client.SuggestGasPrice(context.Background()) + if err != nil { + logger.Error().Err(err).Msgf("cannot get gas price from chain %s ", toChain) + return + } + gasprice = roundUpToNearestGwei(suggested) + } else { + specified, ok := new(big.Int).SetString(send.GetCurrentOutTxParam().OutboundTxGasPrice, 10) + if !ok { + logger.Error().Err(err).Msgf("cannot convert gas price %s ", send.GetCurrentOutTxParam().OutboundTxGasPrice) + return + } + gasprice = specified } var tx *ethtypes.Transaction @@ -423,3 +437,13 @@ func (signer *EVMSigner) SignWhitelistTx(action string, recipient ethcommon.Addr return tx, nil } + +func roundUpToNearestGwei(gasPrice *big.Int) *big.Int { + oneGwei := big.NewInt(1_000_000_000) // 1 Gwei + mod := new(big.Int) + mod.Mod(gasPrice, oneGwei) + if mod.Cmp(big.NewInt(0)) == 0 { // gasprice is already a multiple of 1 Gwei + return gasPrice + } + return new(big.Int).Add(gasPrice, new(big.Int).Sub(oneGwei, mod)) +}