diff --git a/x/evm/keeper/grpc_query.go b/x/evm/keeper/grpc_query.go index cafb4b57cc..2ea309239f 100644 --- a/x/evm/keeper/grpc_query.go +++ b/x/evm/keeper/grpc_query.go @@ -227,6 +227,8 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms if types.IsLondon(ethCfg, ctx.BlockHeight()) { baseFee = k.feeMarketKeeper.GetBaseFee(ctx) } + nonce := k.GetNonce(args.GetFrom()) + args.Nonce = (*hexutil.Uint64)(&nonce) msg, err := args.ToMessage(req.GasCap, baseFee) if err != nil { @@ -297,6 +299,9 @@ func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*type baseFee = k.feeMarketKeeper.GetBaseFee(ctx) } + nonce := k.GetNonce(args.GetFrom()) + args.Nonce = (*hexutil.Uint64)(&nonce) + // Create a helper to check if a gas allowance results in an executable transaction executable := func(gas uint64) (vmerror bool, rsp *types.MsgEthereumTxResponse, err error) { args.Gas = (*hexutil.Uint64)(&gas) diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 6ee5fceabb..1ed0a3ee1e 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -3,6 +3,8 @@ package keeper_test import ( "encoding/json" "fmt" + "github.com/tharsis/ethermint/crypto/ethsecp256k1" + "github.com/tharsis/ethermint/server/config" "math/big" "github.com/ethereum/go-ethereum/common" @@ -855,3 +857,36 @@ func (suite *KeeperTestSuite) TestTraceBlock() { suite.dynamicTxFee = false // reset flag } + +func (suite *KeeperTestSuite) TestNonceInQuery() { + suite.SetupTest() + priv, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + address := common.BytesToAddress(priv.PubKey().Address().Bytes()) + suite.Require().Equal(uint64(0), suite.app.EvmKeeper.GetNonce(address)) + supply := sdk.NewIntWithDecimal(1000, 18).BigInt() + + // accupy nonce 0 + _ = suite.DeployTestContract(suite.T(), address, supply) + + // do an EthCall/EstimateGas with nonce 0 + ctorArgs, err := types.ERC20Contract.ABI.Pack("", address, supply) + data := append(types.ERC20Contract.Bin, ctorArgs...) + args, err := json.Marshal(&types.TransactionArgs{ + From: &address, + Data: (*hexutil.Bytes)(&data), + }) + suite.Require().NoError(err) + + _, err = suite.queryClient.EstimateGas(sdk.WrapSDKContext(suite.ctx), &types.EthCallRequest{ + Args: args, + GasCap: uint64(config.DefaultGasCap), + }) + suite.Require().NoError(err) + + _, err = suite.queryClient.EthCall(sdk.WrapSDKContext(suite.ctx), &types.EthCallRequest{ + Args: args, + GasCap: uint64(config.DefaultGasCap), + }) + suite.Require().NoError(err) +}