Skip to content

Commit

Permalink
use dynamic gas price for Ethereum chains
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Aug 9, 2023
1 parent 9a315db commit 5fed6e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
9 changes: 8 additions & 1 deletion common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()) == ""
Expand Down
42 changes: 33 additions & 9 deletions zetaclient/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -423,3 +437,13 @@ func (signer *EVMSigner) SignWhitelistTx(action string, recipient ethcommon.Addr

return tx, nil
}

func roundUpToNearestGwei(gasPrice *big.Int) *big.Int {

This comment has been minimized.

Copy link
@brewmaster012

brewmaster012 Aug 9, 2023

Collaborator

I would just do

(gasPrice + 999_999_999) / 1_000_000_000 * 1_000_000_000

in int64. But you can use yours if you are confident.

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))
}

0 comments on commit 5fed6e6

Please sign in to comment.