Skip to content

Commit db50cdd

Browse files
authored
ethclient: add EstimateGasAt[Hash] functions ethereum#27508 (#1396)
1 parent 8679a9f commit db50cdd

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

ethclient/ethclient.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,13 @@ func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *
646646
}
647647

648648
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
649-
// the current pending state of the backend blockchain. There is no guarantee that this is
650-
// the true gas limit requirement as other transactions may be added or removed by miners,
651-
// but it should provide a basis for setting a reasonable default.
649+
// the current state of the backend blockchain. There is no guarantee that this is the
650+
// true gas limit requirement as other transactions may be added or removed by miners, but
651+
// it should provide a basis for setting a reasonable default.
652+
//
653+
// Note that the state used by this method is implementation-defined by the remote RPC
654+
// server, but it's reasonable to assume that it will either be the pending or latest
655+
// state.
652656
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
653657
var hex hexutil.Uint64
654658
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
@@ -658,6 +662,28 @@ func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64
658662
return uint64(hex), nil
659663
}
660664

665+
// EstimateGasAtBlock is almost the same as EstimateGas except that it selects the block height
666+
// instead of using the remote RPC's default state for gas estimation.
667+
func (ec *Client) EstimateGasAtBlock(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) (uint64, error) {
668+
var hex hexutil.Uint64
669+
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg), toBlockNumArg(blockNumber))
670+
if err != nil {
671+
return 0, err
672+
}
673+
return uint64(hex), nil
674+
}
675+
676+
// EstimateGasAtBlockHash is almost the same as EstimateGas except that it selects the block
677+
// hash instead of using the remote RPC's default state for gas estimation.
678+
func (ec *Client) EstimateGasAtBlockHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) (uint64, error) {
679+
var hex hexutil.Uint64
680+
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false))
681+
if err != nil {
682+
return 0, err
683+
}
684+
return uint64(hex), nil
685+
}
686+
661687
// SendTransaction injects a signed transaction into the pending pool for execution.
662688
//
663689
// If the transaction was a contract creation use the TransactionReceipt method to get the

0 commit comments

Comments
 (0)