From 8258b81bcbe5146374f272425850d5ae4b0defda Mon Sep 17 00:00:00 2001 From: minh-bq <97180373+minh-bq@users.noreply.github.com> Date: Mon, 17 Apr 2023 10:39:45 +0700 Subject: [PATCH] feat: allow to bump the gas limit higher than returned from estimate gas (#36) --- types.go | 3 ++- utils/main.go | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/types.go b/types.go index 2357efa..a885aa1 100644 --- a/types.go +++ b/types.go @@ -181,7 +181,8 @@ type LsConfig struct { // MaxProcessingTasks is used to specify max processing tasks allowed while processing tasks // if number of tasks reaches this number, it waits until this number decrease - MaxProcessingTasks int `json:"maxProcessingTasks"` + MaxProcessingTasks int `json:"maxProcessingTasks"` + GasLimitBumpRatio uint64 `json:"gasLimitBumpRatio"` } type Secret struct { diff --git a/utils/main.go b/utils/main.go index 965a952..1ed5844 100644 --- a/utils/main.go +++ b/utils/main.go @@ -5,14 +5,15 @@ import ( "encoding/json" "errors" "fmt" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/rlp" - "golang.org/x/crypto/sha3" "math/big" "os" "reflect" "sync" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/crypto/sha3" + kmsUtils "github.com/axieinfinity/ronin-kms-client/utils" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" @@ -26,6 +27,8 @@ import ( "golang.org/x/text/language" ) +const Percentage = 100 + // hasherPool holds LegacyKeccak256 hashers for rlpHash. var hasherPool = sync.Pool{ New: func() interface{} { return sha3.NewLegacyKeccak256() }, @@ -50,7 +53,7 @@ type Utils interface { UnpackToInterface(a abi.ABI, name string, data []byte, isInput bool, v interface{}) error Title(text string) string NewEthClient(url string) (EthClient, error) - SendContractTransaction(signMethod ISign, chainId *big.Int, fn func(opts *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) + SendContractTransaction(signMethod ISign, chainId *big.Int, gasLimitBumpRatio uint64, fn func(opts *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) SignTypedData(typedData core.TypedData, signMethod ISign) (hexutil.Bytes, error) FilterLogs(client EthClient, opts *bind.FilterOpts, contractAddresses []common.Address, filteredMethods map[*abi.ABI]map[string]struct{}) ([]types.Log, error) RlpHash(x interface{}) (h common.Hash) @@ -178,11 +181,25 @@ func newKeyedTransactorWithChainID(signMethod ISign, chainID *big.Int) (*bind.Tr }, nil } -func (u *utils) SendContractTransaction(signMethod ISign, chainId *big.Int, fn func(opts *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) { +func (u *utils) SendContractTransaction( + signMethod ISign, + chainId *big.Int, + gasLimitBumpRatio uint64, + fn func(opts *bind.TransactOpts) (*types.Transaction, error), +) (*types.Transaction, error) { opts, err := newKeyedTransactorWithChainID(signMethod, chainId) if err != nil { return nil, err } + // Estimate gas + opts.NoSend = true + tx, err := fn(opts) + if err != nil { + return nil, err + } + + opts.NoSend = false + opts.GasLimit = tx.Gas() * gasLimitBumpRatio / Percentage return fn(opts) }