Skip to content

Commit

Permalink
Merge pull request #35 from sei-protocol/multi-estimate
Browse files Browse the repository at this point in the history
Add DoEstimateGasAfterCalls
  • Loading branch information
codchen authored Oct 2, 2024
2 parents af807e5 + 4b3b31e commit da7da77
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,45 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
return hexutil.Uint64(estimate), nil
}

func DoEstimateGasAfterCalls(ctx context.Context, b Backend, args TransactionArgs, calls []TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, timeout time.Duration, gasCap uint64) (hexutil.Uint64, error) {
state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
if state == nil || err != nil {
return 0, err
}
for _, call := range calls {
_, err = doCall(ctx, b, call, state, header, overrides, nil, timeout, gasCap)
if err != nil {
return 0, err
}
overrides = nil
}
// Construct the gas estimator option from the user input
opts := &gasestimator.Options{
Config: b.ChainConfig(),
Chain: NewChainContext(ctx, b),
Header: header,
State: state,
ErrorRatio: estimateGasErrorRatio,
}

if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {
return 0, err
}
call := args.ToMessage(header.BaseFee)
if err != nil {
return 0, err
}
// Run the gas estimation andwrap any revertals into a custom return
estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap)
if err != nil {
if len(revert) > 0 {
return 0, newRevertError(revert)
}
return 0, err
}
return hexutil.Uint64(estimate), nil
}

// EstimateGas returns the lowest possible gas limit that allows the transaction to run
// successfully at block `blockNrOrHash`, or the latest block if `blockNrOrHash` is unspecified. It
// returns error if the transaction would revert or if there are unexpected failures. The returned
Expand Down

0 comments on commit da7da77

Please sign in to comment.