Skip to content

Commit

Permalink
Make eth_estimateGas CIP64 and CIP66 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ezdac committed May 14, 2024
1 parent 3fc30ba commit 947fc1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
38 changes: 32 additions & 6 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,14 +1344,35 @@ func DoEstimateGas(ctx context.Context, b CeloBackend, args TransactionArgs, blo

// Recap the highest gas limit with account's available balance.
if feeCap.BitLen() != 0 {
balance := state.GetBalance(*args.From) // from can't be nil
// Is given in native token when the feeCurrency is nil.
balance, err := b.GetFeeBalance(ctx, header.Hash(), *args.From, args.FeeCurrency) // from can't be nil
if err != nil {
return 0, err
}
available := new(big.Int).Set(balance)
if args.Value != nil {
if args.Value.ToInt().Cmp(available) >= 0 {
return 0, core.ErrInsufficientFundsForTransfer
if args.FeeCurrency != nil {
if !args.IsFeeCurrencyDenominated() {
// CIP-66, prices are given in native token.
// We need to check the allowance in the converted feeCurrency
feeCap, err = b.ConvertToCurrency(ctx, header.Hash(), feeCap, args.FeeCurrency)
if err != nil {
return 0, err
}
}
} else {
if args.Value != nil {
if args.Value.ToInt().Cmp(available) >= 0 {
return 0, core.ErrInsufficientFundsForTransfer
}
available.Sub(available, args.Value.ToInt())
}
available.Sub(available, args.Value.ToInt())
}

// cap the available by the maxFeeInFeeCurrency
if args.MaxFeeInFeeCurrency != nil {
available = math.BigMin(available, args.MaxFeeInFeeCurrency.ToInt())
}

allowance := new(big.Int).Div(available, feeCap)

// If the allowance is larger than maximum uint64, skip checking
Expand All @@ -1360,8 +1381,13 @@ func DoEstimateGas(ctx context.Context, b CeloBackend, args TransactionArgs, blo
if transfer == nil {
transfer = new(hexutil.Big)
}
maxFeeInFeeCurrency := args.MaxFeeInFeeCurrency
if maxFeeInFeeCurrency == nil {
maxFeeInFeeCurrency = new(hexutil.Big)
}
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
"sent", transfer.ToInt(), "maxFeePerGas", feeCap, "fundable", allowance)
"sent", transfer.ToInt(), "maxFeePerGas", feeCap, "fundable", allowance,
"feeCurrency", args.FeeCurrency, "maxFeeInFeeCurrency", maxFeeInFeeCurrency.ToInt())
hi = allowance.Uint64()
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int, ex
var err error
baseFee, err = exchange.ConvertGoldToCurrency(exchangeRates, args.FeeCurrency, baseFee)
if err != nil {
return nil, err
return nil, fmt.Errorf("can't convert base-fee to fee-currency: %w", err)
}
}
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
Expand Down

0 comments on commit 947fc1c

Please sign in to comment.