Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit fae8147

Browse files
authored
Fix failed to call EstimateGas (#761)
* fix failed to call function EstimateGas * add ut to test estimateGas after contract deployed
1 parent 177574a commit fae8147

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444

4545
### Bug Fixes
4646

47+
48+
* (evm) [\#760](https://github.com/cosmos/ethermint/issues/760) Fix Failed to call function EstimateGas.
4749
* (evm) [\#767](https://github.com/cosmos/ethermint/issues/767) Fix error of timeout when using Truffle to deploy contract.
4850
* (evm) [\#751](https://github.com/cosmos/ethermint/issues/751) Fix misused method to calculate block hash in evm related function.
4951
* (evm) [\#721](https://github.com/cosmos/ethermint/issues/721) Fix mismatch block hash in rpc response when use eht.getBlock.

rpc/namespaces/eth/api.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ func (api *PublicEthereumAPI) doCall(
551551
addr = *args.From
552552
}
553553

554+
nonce, _ := api.accountNonce(api.clientCtx, addr, true)
555+
554556
// Set default gas & gas price if none were set
555557
// Change this to uint64(math.MaxUint64 / 2) if gas cap can be configured
556558
gas := uint64(ethermint.DefaultRPCGasLimit)
@@ -588,7 +590,7 @@ func (api *PublicEthereumAPI) doCall(
588590

589591
var msgs []sdk.Msg
590592
// Create new call message
591-
msg := evmtypes.NewMsgEthermint(0, &toAddr, sdk.NewIntFromBigInt(value), gas,
593+
msg := evmtypes.NewMsgEthermint(nonce, &toAddr, sdk.NewIntFromBigInt(value), gas,
592594
sdk.NewIntFromBigInt(gasPrice), data, sdk.AccAddress(addr.Bytes()))
593595
msgs = append(msgs, msg)
594596

@@ -1086,8 +1088,9 @@ func (api *PublicEthereumAPI) pendingMsgs() ([]sdk.Msg, error) {
10861088
}
10871089

10881090
pendingData := pendingTx.Input
1091+
nonce, _ := api.accountNonce(api.clientCtx, pendingTx.From, true)
10891092

1090-
msg := evmtypes.NewMsgEthermint(0, &pendingTo, sdk.NewIntFromBigInt(pendingValue), pendingGas,
1093+
msg := evmtypes.NewMsgEthermint(nonce, &pendingTo, sdk.NewIntFromBigInt(pendingValue), pendingGas,
10911094
sdk.NewIntFromBigInt(pendingGasPrice), pendingData, pendingFrom)
10921095

10931096
msgs = append(msgs, msg)

tests/rpc_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ func TestEth_PendingTransactionFilter(t *testing.T) {
526526
}
527527

528528
func TestEth_EstimateGas(t *testing.T) {
529+
529530
param := make([]map[string]string, 1)
530531
param[0] = make(map[string]string)
531532
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
@@ -539,26 +540,33 @@ func TestEth_EstimateGas(t *testing.T) {
539540
err := json.Unmarshal(rpcRes.Result, &gas)
540541
require.NoError(t, err, string(rpcRes.Result))
541542

542-
require.Equal(t, "0xf54c", gas)
543+
require.Equal(t, "0xf560", gas)
543544
}
544545

545546
func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
546547
bytecode := "0x608060405234801561001057600080fd5b5060117f775a94827b8fd9b519d36cd827093c664f93347070a554f65e4a6f56cd73889860405160405180910390a260d08061004d6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b606060048036036040811015604157600080fd5b8101908080359060200190929190803590602001909291905050506062565b005b8160008190555080827ff3ca124a697ba07e8c5e80bebcfcc48991fc16a63170e8a9206e30508960d00360405160405180910390a3505056fea265627a7a723158201d94d2187aaf3a6790527b615fcc40970febf0385fa6d72a2344848ebd0df3e964736f6c63430005110032"
547548

549+
//deploy contract befor call estimateGas, make account nonce changed
550+
hash, _ := DeployTestContract(t, from)
551+
time.Sleep(5 * time.Second)
552+
paramdeploy := []string{hash.String()}
553+
rpcRes := Call(t, "eth_getTransactionReceipt", paramdeploy)
554+
require.Nil(t, rpcRes.Error)
555+
548556
param := make([]map[string]string, 1)
549557
param[0] = make(map[string]string)
550558
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
551559
param[0]["data"] = bytecode
552560

553-
rpcRes := Call(t, "eth_estimateGas", param)
561+
rpcRes = Call(t, "eth_estimateGas", param)
554562
require.NotNil(t, rpcRes)
555563
require.NotEmpty(t, rpcRes.Result)
556564

557565
var gas hexutil.Uint64
558566
err := json.Unmarshal(rpcRes.Result, &gas)
559567
require.NoError(t, err, string(rpcRes.Result))
560568

561-
require.Equal(t, "0x1a724", gas.String())
569+
require.Equal(t, "0x1a738", gas.String())
562570
}
563571

564572
func TestEth_GetBlockByNumber(t *testing.T) {

0 commit comments

Comments
 (0)