@@ -41,11 +41,14 @@ var (
4141 ErrTransactionReverted = errors .New ("transaction reverted" )
4242 ErrUnknownTransaction = errors .New ("unknown transaction" )
4343 ErrAlreadyImported = errors .New ("already imported" )
44+ ErrEIP1559NotSupported = errors .New ("network does not appear to support EIP-1559 (no baseFee)" )
4445)
4546
4647const (
47- DefaultTipBoostPercent = 20
48- DefaultGasLimit = 1_000_000
48+ DefaultGasLimit = 1_000_000
49+ DefaultTipBoostPercent = 25
50+ MinimumGasTipCap = 1_500_000_000 // 1.5 Gwei
51+ RedistributionTipBoostPercent = 50
4952)
5053
5154// TxRequest describes a request for a transaction that can be executed.
@@ -226,8 +229,8 @@ func (t *transactionService) waitForPendingTx(txHash common.Hash) {
226229 t .wg .Add (1 )
227230 go func () {
228231 defer t .wg .Done ()
229- switch _ , err := t .WaitForReceipt (t .ctx , txHash ); {
230- case err == nil :
232+ switch _ , err := t .WaitForReceipt (t .ctx , txHash ); err {
233+ case nil :
231234 t .logger .Info ("pending transaction confirmed" , "tx" , txHash )
232235 err = t .store .Delete (pendingTransactionKey (txHash ))
233236 if err != nil {
@@ -286,7 +289,7 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
286289 gasLimit = request .MinEstimatedGasLimit
287290 }
288291
289- gasLimit += gasLimit / 4 // add 25% on top
292+ gasLimit += gasLimit / 2 // add 50% buffer to the estimated gas limit
290293 if gasLimit < request .MinEstimatedGasLimit {
291294 gasLimit = request .MinEstimatedGasLimit
292295 }
@@ -324,28 +327,48 @@ func (t *transactionService) prepareTransaction(ctx context.Context, request *Tx
324327}
325328
326329func (t * transactionService ) suggestedFeeAndTip (ctx context.Context , gasPrice * big.Int , boostPercent int ) (* big.Int , * big.Int , error ) {
327- var err error
330+ gasTipCap , err := t .backend .SuggestGasTipCap (ctx )
331+ if err != nil {
332+ return nil , nil , err
333+ }
334+
335+ multiplier := big .NewInt (int64 (boostPercent ) + 100 )
336+ gasTipCap = new (big.Int ).Div (new (big.Int ).Mul (gasTipCap , multiplier ), big .NewInt (100 ))
337+
338+ minimumTip := big .NewInt (MinimumGasTipCap )
339+ if gasTipCap .Cmp (minimumTip ) < 0 {
340+ gasTipCap = new (big.Int ).Set (minimumTip )
341+ }
342+
343+ var gasFeeCap * big.Int
328344
329345 if gasPrice == nil {
330- gasPrice , err = t .backend .SuggestGasPrice (ctx )
346+ latestBlockHeader , err : = t .backend .HeaderByNumber (ctx , nil )
331347 if err != nil {
332- return nil , nil , err
348+ return nil , nil , fmt . Errorf ( "failed to get latest block: %w" , err )
333349 }
334- gasPrice = new (big.Int ).Div (new (big.Int ).Mul (big .NewInt (int64 (boostPercent )+ 100 ), gasPrice ), big .NewInt (100 ))
335- }
336350
337- gasTipCap , err := t .backend .SuggestGasTipCap (ctx )
338- if err != nil {
339- return nil , nil , err
351+ if latestBlockHeader .BaseFee == nil {
352+ return nil , nil , ErrEIP1559NotSupported
353+ }
354+
355+ // gasFeeCap = (2 * baseFee) + gasTipCap
356+ gasFeeCap = new (big.Int ).Add (
357+ new (big.Int ).Mul (latestBlockHeader .BaseFee , big .NewInt (2 )),
358+ gasTipCap ,
359+ )
360+ } else {
361+ gasFeeCap = new (big.Int ).Set (gasPrice )
340362 }
341363
342- gasTipCap = new (big.Int ).Div (new (big.Int ).Mul (big .NewInt (int64 (boostPercent )+ 100 ), gasTipCap ), big .NewInt (100 ))
343- gasFeeCap := new (big.Int ).Add (gasTipCap , gasPrice )
364+ if gasTipCap .Cmp (gasFeeCap ) > 0 {
365+ t .logger .Warning ("gas tip cap is higher than gas fee cap, using gas fee cap as gas tip cap" , "gas_tip_cap" , gasTipCap , "gas_fee_cap" , gasFeeCap )
366+ gasTipCap = new (big.Int ).Set (gasFeeCap )
367+ }
344368
345- t .logger .Debug ("prepare transaction" , "gas_price" , gasPrice , " gas_max_fee" , gasFeeCap , "gas_max_tip" , gasTipCap )
369+ t .logger .Debug ("prepare transaction" , "gas_max_fee" , gasFeeCap , "gas_max_tip" , gasTipCap )
346370
347371 return gasFeeCap , gasTipCap , nil
348-
349372}
350373
351374func storedTransactionKey (txHash common.Hash ) string {
@@ -371,10 +394,9 @@ func (t *transactionService) nextNonce(ctx context.Context) (uint64, error) {
371394
372395 // PendingNonceAt returns the nonce we should use, but we will
373396 // compare this to our pending tx list, therefore the -1.
374- var maxNonce uint64 = onchainNonce - 1
397+ var maxNonce = onchainNonce - 1
375398 for _ , txHash := range pendingTxs {
376399 trx , _ , err := t .backend .TransactionByHash (ctx , txHash )
377-
378400 if err != nil {
379401 t .logger .Error (err , "pending transaction not found" , "tx" , txHash )
380402 return 0 , err
@@ -419,7 +441,7 @@ func (t *transactionService) WatchSentTransaction(txHash common.Hash) (<-chan ty
419441}
420442
421443func (t * transactionService ) PendingTransactions () ([]common.Hash , error ) {
422- var txHashes []common. Hash = make ([]common.Hash , 0 )
444+ var txHashes = make ([]common.Hash , 0 )
423445 err := t .store .Iterate (pendingTransactionPrefix , func (key , value []byte ) (stop bool , err error ) {
424446 txHash := common .HexToHash (strings .TrimPrefix (string (key ), pendingTransactionPrefix ))
425447 txHashes = append (txHashes , txHash )
@@ -438,7 +460,6 @@ func (t *transactionService) filterPendingTransactions(ctx context.Context, txHa
438460
439461 for _ , txHash := range txHashes {
440462 _ , isPending , err := t .backend .TransactionByHash (ctx , txHash )
441-
442463 // When error occurres consider transaction as pending (so this transaction won't be filtered out),
443464 // unless it was not found
444465 if err != nil {
0 commit comments