Skip to content

Commit af96b66

Browse files
rjl493456442karalabe
authored andcommitted
internal/ethapi: estimate gas usage automatically (#19508)
1 parent 504f88b commit af96b66

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

‎internal/ethapi/api.go‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,10 +1260,6 @@ type SendTxArgs struct {
12601260

12611261
// setDefaults is a helper function that fills in default values for unspecified tx fields.
12621262
func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error {
1263-
if args.Gas == nil {
1264-
args.Gas = new(hexutil.Uint64)
1265-
*(*uint64)(args.Gas) = 90000
1266-
}
12671263
if args.GasPrice == nil {
12681264
price, err := b.SuggestPrice(ctx)
12691265
if err != nil {
@@ -1296,15 +1292,37 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error {
12961292
return errors.New(`contract creation without any data provided`)
12971293
}
12981294
}
1295+
// Estimate the gas usage if necessary.
1296+
if args.Gas == nil {
1297+
// For backwards-compatibility reason, we try both input and data
1298+
// but input is preferred.
1299+
input := args.Input
1300+
if input == nil {
1301+
input = args.Data
1302+
}
1303+
callArgs := CallArgs{
1304+
From: &args.From, // From shouldn't be nil
1305+
To: args.To,
1306+
GasPrice: args.GasPrice,
1307+
Value: args.Value,
1308+
Data: input,
1309+
}
1310+
estimated, err := DoEstimateGas(ctx, b, callArgs, rpc.PendingBlockNumber, b.RPCGasCap())
1311+
if err != nil {
1312+
return err
1313+
}
1314+
args.Gas = &estimated
1315+
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
1316+
}
12991317
return nil
13001318
}
13011319

13021320
func (args *SendTxArgs) toTransaction() *types.Transaction {
13031321
var input []byte
1304-
if args.Data != nil {
1305-
input = *args.Data
1306-
} else if args.Input != nil {
1322+
if args.Input != nil {
13071323
input = *args.Input
1324+
} else if args.Data != nil {
1325+
input = *args.Data
13081326
}
13091327
if args.To == nil {
13101328
return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input)
@@ -1334,7 +1352,6 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
13341352
// SendTransaction creates a transaction for the given argument, sign it and submit it to the
13351353
// transaction pool.
13361354
func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) {
1337-
13381355
// Look up the wallet containing the requested signer
13391356
account := accounts.Account{Address: args.From}
13401357

0 commit comments

Comments
 (0)