Skip to content

Commit 7949ec0

Browse files
committed
internal/ethapi: set default fees before gas estimation
1 parent 8fd9c7b commit 7949ec0

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

eth/gasestimator/gasestimator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func Estimate(ctx context.Context, call *core.Message, opts *Options, gasCap uin
7575

7676
available := balance
7777
if call.Value != nil {
78-
if call.Value.Cmp(available) >= 0 {
78+
if call.Value.Cmp(available) > 0 {
7979
return 0, nil, core.ErrInsufficientFundsForTransfer
8080
}
8181
available.Sub(available, call.Value)

internal/ethapi/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,9 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
12841284
// configuration (if non-zero).
12851285
// Note: Required blob gas is not computed in this method.
12861286
func (s *BlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, blockNrOrHash *rpc.BlockNumberOrHash, overrides *StateOverride) (hexutil.Uint64, error) {
1287+
if err := args.setFeeDefaults(ctx, s.b); err != nil {
1288+
return 0, err
1289+
}
12871290
bNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
12881291
if blockNrOrHash != nil {
12891292
bNrOrHash = *blockNrOrHash

internal/ethapi/api_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,19 +671,19 @@ func TestEstimateGas(t *testing.T) {
671671
To: &accounts[1].addr,
672672
Value: (*hexutil.Big)(big.NewInt(1000)),
673673
},
674-
expectErr: core.ErrInsufficientFunds,
675-
want: 21000,
674+
expectErr: core.ErrInsufficientFundsForTransfer,
676675
},
677676
// empty create
678677
{
679678
blockNumber: rpc.LatestBlockNumber,
680679
call: TransactionArgs{},
681-
expectErr: nil,
682-
want: 53000,
680+
expectErr: errors.New("gas required exceeds allowance (0)"),
683681
},
684682
{
685683
blockNumber: rpc.LatestBlockNumber,
686-
call: TransactionArgs{},
684+
call: TransactionArgs{
685+
From: &randomAccounts[0].addr,
686+
},
687687
overrides: StateOverride{
688688
randomAccounts[0].addr: OverrideAccount{Balance: newRPCBalance(new(big.Int).Mul(big.NewInt(1), big.NewInt(params.Ether)))},
689689
},
@@ -700,7 +700,7 @@ func TestEstimateGas(t *testing.T) {
700700
overrides: StateOverride{
701701
randomAccounts[0].addr: OverrideAccount{Balance: newRPCBalance(big.NewInt(0))},
702702
},
703-
expectErr: core.ErrInsufficientFunds,
703+
expectErr: core.ErrInsufficientFundsForTransfer,
704704
},
705705
// Test for a bug where the gas price was set to zero but the basefee non-zero
706706
//
@@ -763,7 +763,7 @@ func TestEstimateGas(t *testing.T) {
763763
t.Errorf("test %d: want error %v, have nothing", i, tc.expectErr)
764764
continue
765765
}
766-
if !errors.Is(err, tc.expectErr) {
766+
if !errors.Is(err, tc.expectErr) && err.Error() != tc.expectErr.Error() {
767767
t.Errorf("test %d: error mismatch, want %v, have %v", i, tc.expectErr, err)
768768
}
769769
continue

0 commit comments

Comments
 (0)