From 4b3b31e24530a3011d18fcb529d0d0b1dad0e450 Mon Sep 17 00:00:00 2001 From: codchen Date: Tue, 1 Oct 2024 13:23:04 +0800 Subject: [PATCH] Add DoEstimateGasAfterCalls --- lib/ethapi/api.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/ethapi/api.go b/lib/ethapi/api.go index 251dfd00da..1a4e78224d 100644 --- a/lib/ethapi/api.go +++ b/lib/ethapi/api.go @@ -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