Skip to content

Commit bd820ee

Browse files
authored
chore: apply state overrides to eth_estimateGas (#764)
* apply state override to eth call * fix changelog * fix lint
1 parent 68d9d3c commit bd820ee

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242
* (evm) [#742](https://github.com/crypto-org-chain/ethermint/pull/742) fix: prevent nil pointer dereference in tracer hooks
4343
* (evm) [#728](https://github.com/crypto-org-chain/ethermint/pull/728) feat: support preinstalls
4444
* (evm) [#722](https://github.com/crypto-org-chain/ethermint/pull/722) feat: support EIP-2935
45+
* (rpc) [#764](https://github.com/crypto-org-chain/ethermint/pull/764) rpc: apply state overrides to eth_estimateGas
4546

4647
## [v0.22.0] - 2025-08-12
4748

rpc/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type EVMBackend interface {
130130
Resend(args evmtypes.TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error)
131131
SendRawTransaction(data hexutil.Bytes) (common.Hash, error)
132132
SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error)
133-
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error)
133+
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber, overrides *json.RawMessage) (hexutil.Uint64, error)
134134
DoCall(args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber, overrides *json.RawMessage) (*evmtypes.MsgEthereumTxResponse, error)
135135
GasPrice() (*hexutil.Big, error)
136136

rpc/backend/call_tx.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (b *Backend) SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.Transac
292292
}
293293

294294
blockNr := rpctypes.NewBlockNumber(big.NewInt(0))
295-
estimated, err := b.EstimateGas(callArgs, &blockNr)
295+
estimated, err := b.EstimateGas(callArgs, &blockNr, nil)
296296
if err != nil {
297297
return args, err
298298
}
@@ -319,7 +319,11 @@ func (b *Backend) handleRevertError(vmError string, ret []byte) error {
319319
}
320320

321321
// EstimateGas returns an estimate of gas usage for the given smart contract call.
322-
func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error) {
322+
func (b *Backend) EstimateGas(
323+
args evmtypes.TransactionArgs,
324+
blockNrOptional *rpctypes.BlockNumber,
325+
overrides *json.RawMessage,
326+
) (hexutil.Uint64, error) {
323327
blockNr := rpctypes.EthPendingBlockNumber
324328
if blockNrOptional != nil {
325329
blockNr = *blockNrOptional
@@ -336,11 +340,17 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp
336340
return 0, errors.New("header not found")
337341
}
338342

343+
var bzOverrides []byte
344+
if overrides != nil {
345+
bzOverrides = *overrides
346+
}
347+
339348
req := evmtypes.EthCallRequest{
340349
Args: bz,
341350
GasCap: b.RPCGasCap(),
342351
ProposerAddress: sdk.ConsAddress(header.Header.ProposerAddress),
343352
ChainId: b.chainID.Int64(),
353+
Overrides: bzOverrides,
344354
}
345355

346356
// From ContextWithHeight: if the provided height is 0,
@@ -359,7 +369,8 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp
359369
// DoCall performs a simulated call operation through the evmtypes. It returns the
360370
// estimated gas used on the operation or an error if fails.
361371
func (b *Backend) DoCall(
362-
args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber,
372+
args evmtypes.TransactionArgs,
373+
blockNr rpctypes.BlockNumber,
363374
overrides *json.RawMessage,
364375
) (*evmtypes.MsgEthereumTxResponse, error) {
365376
bz, err := json.Marshal(&args)

rpc/namespaces/ethereum/eth/api.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ type EthereumAPI interface {
9393
// Returns information on the Ethereum network and internal settings.
9494
ProtocolVersion() hexutil.Uint
9595
GasPrice() (*hexutil.Big, error)
96-
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error)
96+
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber, overrides *json.RawMessage) (hexutil.Uint64, error)
9797
FeeHistory(blockCount math.HexOrDecimal64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*rpctypes.FeeHistoryResult, error)
9898
MaxPriorityFeePerGas() (*hexutil.Big, error)
9999
ChainId() (*hexutil.Big, error)
@@ -322,9 +322,13 @@ func (e *PublicAPI) GasPrice() (*hexutil.Big, error) {
322322
}
323323

324324
// EstimateGas returns an estimate of gas usage for the given smart contract call.
325-
func (e *PublicAPI) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error) {
325+
func (e *PublicAPI) EstimateGas(
326+
args evmtypes.TransactionArgs,
327+
blockNrOptional *rpctypes.BlockNumber,
328+
overrides *json.RawMessage,
329+
) (hexutil.Uint64, error) {
326330
e.logger.Debug("eth_estimateGas")
327-
return e.backend.EstimateGas(args, blockNrOptional)
331+
return e.backend.EstimateGas(args, blockNrOptional, overrides)
328332
}
329333

330334
func (e *PublicAPI) FeeHistory(blockCount math.HexOrDecimal64,

x/evm/keeper/grpc_query.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type
356356
return nil, status.Error(codes.Internal, "failed to load evm config")
357357
}
358358

359+
var overrides rpctypes.StateOverride
360+
if len(req.Overrides) > 0 {
361+
if err := json.Unmarshal(req.Overrides, &overrides); err != nil {
362+
return nil, status.Error(codes.InvalidArgument, err.Error())
363+
}
364+
365+
cfg.Overrides = &overrides
366+
}
367+
359368
// ApplyMessageWithConfig expect correct nonce set in msg
360369
nonce := k.GetNonce(ctx, args.GetFrom())
361370
args.Nonce = (*hexutil.Uint64)(&nonce)

0 commit comments

Comments
 (0)