Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ethclient gas estimation blocknumber param #25009

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/abi/bind/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type ContractTransactor interface {
// There is no guarantee that this is the true gas limit requirement as other
// transactions may be added or removed by miners, but it should provide a basis
// for setting a reasonable default.
EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
EstimateGas(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error)

// SendTransaction injects the transaction into the pending pool for execution.
SendTransaction(ctx context.Context, tx *types.Transaction) error
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func (b *SimulatedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, erro

// EstimateGas executes the requested code against the currently pending block/state and
// returns the used amount of gas.
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) {
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
b.mu.Lock()
defer b.mu.Unlock()

Expand Down
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func TestEstimateGas(t *testing.T) {
}, 21275, nil, nil},
}
for _, c := range cases {
got, err := sim.EstimateGas(context.Background(), c.message)
got, err := sim.EstimateGas(context.Background(), c.message, nil)
if c.expectError != nil {
if err == nil {
t.Fatalf("Expect error, got nil")
Expand Down Expand Up @@ -602,7 +602,7 @@ func TestEstimateGasWithPrice(t *testing.T) {
}, params.TxGas, errors.New("gas required exceeds allowance (20999)")}, // 20999=(2.2ether-0.1ether-1wei)/(1e14)
}
for i, c := range cases {
got, err := sim.EstimateGas(context.Background(), c.message)
got, err := sim.EstimateGas(context.Background(), c.message, nil)
if c.expectError != nil {
if err == nil {
t.Fatalf("test %d: expect error, got nil", i)
Expand Down
8 changes: 4 additions & 4 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
gasLimit := opts.GasLimit
if opts.GasLimit == 0 {
var err error
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value)
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Addr
gasLimit := opts.GasLimit
if opts.GasLimit == 0 {
var err error
gasLimit, err = c.estimateGasLimit(opts, contract, input, gasPrice, nil, nil, value)
gasLimit, err = c.estimateGasLimit(opts, contract, input, gasPrice, nil, nil, value, nil)
if err != nil {
return nil, err
}
Expand All @@ -329,7 +329,7 @@ func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Addr
return types.NewTx(baseTx), nil
}

func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value *big.Int) (uint64, error) {
func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value, blockNumber *big.Int) (uint64, error) {
if contract != nil {
// Gas estimation cannot succeed without code for method invocations.
if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil {
Expand All @@ -347,7 +347,7 @@ func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Ad
Value: value,
Data: input,
}
return c.transactor.EstimateGas(ensureContext(opts.Context), msg)
return c.transactor.EstimateGas(ensureContext(opts.Context), msg, nil)
}

func (c *BoundContract) getNonce(opts *TransactOpts) (uint64, error) {
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (mt *mockTransactor) SuggestGasTipCap(ctx context.Context) (*big.Int, error
return mt.gasTipCap, nil
}

func (mt *mockTransactor) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
func (mt *mockTransactor) EstimateGas(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) (gas uint64, err error) {
return 0, nil
}

Expand Down
7 changes: 5 additions & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,12 @@ func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
// but it should provide a basis for setting a reasonable default.
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
var hex hexutil.Uint64
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
if blockNumber == nil {
blockNumber = big.NewInt(-1)
}
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg), toBlockNumArg(blockNumber))
if err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func testCallContractAtHash(t *testing.T, client *rpc.Client) {
Gas: 21000,
Value: big.NewInt(1),
}
gas, err := ec.EstimateGas(context.Background(), msg)
gas, err := ec.EstimateGas(context.Background(), msg, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -547,7 +547,7 @@ func testCallContract(t *testing.T, client *rpc.Client) {
Gas: 21000,
Value: big.NewInt(1),
}
gas, err := ec.EstimateGas(context.Background(), msg)
gas, err := ec.EstimateGas(context.Background(), msg, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ type PendingContractCaller interface {
}

// GasEstimator wraps EstimateGas, which tries to estimate the gas needed to execute a
// specific transaction based on the pending state. There is no guarantee that this is the
// true gas limit requirement as other transactions may be added or removed by miners, but
// it should provide a basis for setting a reasonable default.
// specific transaction based on the block number. Passing `nil` will operate on
// pending state. There is no guarantee that this is the true gas limit requirement
// as other transactions may be added or removed by miners, but it should provide a
// basis for setting a reasonable default.
type GasEstimator interface {
EstimateGas(ctx context.Context, call CallMsg) (uint64, error)
EstimateGas(ctx context.Context, call CallMsg, blockNumber *big.Int) (uint64, error)
}

// A PendingStateEventer provides access to real time notifications about changes to the
Expand Down
4 changes: 2 additions & 2 deletions mobile/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ func (ec *EthereumClient) SuggestGasPrice(ctx *Context) (price *BigInt, _ error)
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
// but it should provide a basis for setting a reasonable default.
func (ec *EthereumClient) EstimateGas(ctx *Context, msg *CallMsg) (gas int64, _ error) {
rawGas, err := ec.client.EstimateGas(ctx.context, msg.msg)
func (ec *EthereumClient) EstimateGas(ctx *Context, msg *CallMsg, blockNumber *big.Int) (gas int64, _ error) {
rawGas, err := ec.client.EstimateGas(ctx.context, msg.msg, blockNumber)
return int64(rawGas), err
}

Expand Down