Skip to content

Commit

Permalink
fix the bug of the new version hardhat eth_estimateGas fail
Browse files Browse the repository at this point in the history
  • Loading branch information
lyh169 committed Oct 12, 2023
1 parent 6b97af6 commit bef0ade
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,13 @@ func (api *PublicEthereumAPI) doCall(

return &simResponse, nil
}
func (api *PublicEthereumAPI) simDoCall(args rpctypes.CallArgs, cap uint64) (uint64, error) {
func (api *PublicEthereumAPI) simDoCall(args rpctypes.CallArgs, cap uint64, blockNum rpctypes.BlockNumber) (uint64, error) {
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) (*sdk.SimulationResponse, error) {
if gas != 0 {
args.Gas = (*hexutil.Uint64)(&gas)
}
return api.doCall(args, 0, big.NewInt(int64(cap)), true, nil)
return api.doCall(args, blockNum, big.NewInt(int64(cap)), true, nil)
}

// get exact gas limit
Expand Down Expand Up @@ -1112,7 +1112,7 @@ func (api *PublicEthereumAPI) simDoCall(args rpctypes.CallArgs, cap uint64) (uin
}

// EstimateGas returns an estimate of gas usage for the given smart contract call.
func (api *PublicEthereumAPI) EstimateGas(args rpctypes.CallArgs) (hexutil.Uint64, error) {
func (api *PublicEthereumAPI) EstimateGas(args rpctypes.CallArgs, blockNrOrHash *rpctypes.BlockNumberOrHash) (hexutil.Uint64, error) {
monitor := monitor.GetMonitor("eth_estimateGas", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd("args", args)

Expand All @@ -1127,7 +1127,15 @@ func (api *PublicEthereumAPI) EstimateGas(args rpctypes.CallArgs) (hexutil.Uint6
args.GasPrice = api.gasPrice
}

estimatedGas, err := api.simDoCall(args, maxGasLimitPerTx)
blockNr := rpctypes.LatestBlockNumber
if blockNrOrHash != nil {
blockNr, err = api.backend.ConvertToBlockNumber(*blockNrOrHash)
if err != nil {
return 0, TransformDataError(err, "eth_estimateGas")
}
}

estimatedGas, err := api.simDoCall(args, maxGasLimitPerTx, blockNr)
if err != nil {
return 0, TransformDataError(err, "eth_estimateGas")
}
Expand Down Expand Up @@ -1667,7 +1675,7 @@ func (api *PublicEthereumAPI) generateFromArgs(args rpctypes.SendTxArgs) (*evmty
Value: args.Value,
Data: &input,
}
gl, err := api.EstimateGas(callArgs)
gl, err := api.EstimateGas(callArgs, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit bef0ade

Please sign in to comment.