From 3d10a09a37240a2c6cc7836532a71d671631c4d0 Mon Sep 17 00:00:00 2001 From: wwq Date: Tue, 23 Jan 2024 11:45:31 +0800 Subject: [PATCH] R4R: get min(sponsorAmount, sponsorBalance) while estimateGas with metatx --- internal/ethapi/api.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 1fafab4eb1..672621f321 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1232,14 +1232,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr if err != nil { return 0, err } - if metaTxParams != nil { - sponsorAmount, _ := types.CalculateSponsorPercentAmount(metaTxParams, new(big.Int).Mul(feeCap, new(big.Int).SetUint64(b.CurrentHeader().GasLimit))) - sponsorBalance := state.GetBalance(metaTxParams.GasFeeSponsor) - if sponsorBalance.Cmp(sponsorAmount) <= 0 { - return 0, types.ErrSponsorBalanceNotEnough - } - balance = new(big.Int).Add(balance, sponsorAmount) - } + available := new(big.Int).Set(balance) if args.Value != nil { if args.Value.ToInt().Cmp(available) >= 0 { @@ -1247,6 +1240,17 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr } available.Sub(available, args.Value.ToInt()) } + + if metaTxParams != nil { + sponsorAmount, _ := types.CalculateSponsorPercentAmount(metaTxParams, new(big.Int).Mul(feeCap, new(big.Int).SetUint64(b.CurrentHeader().GasLimit))) + sponsorBalance := state.GetBalance(metaTxParams.GasFeeSponsor) + if sponsorAmount.Cmp(sponsorBalance) < 0 { + available.Add(available, sponsorAmount) + } else { + available.Add(available, sponsorBalance) + } + } + allowance := new(big.Int).Div(available, feeCap) // If the allowance is larger than maximum uint64, skip checking