Skip to content

Commit

Permalink
fix: boosts both baseFee and priorityFee upon tx retries (#22)
Browse files Browse the repository at this point in the history
* use max(new, old) base fee for txes

* boost base fee too

* nit log

* 10 retries
  • Loading branch information
shaspitz authored Feb 22, 2024
1 parent a0d3af3 commit 5fd8abf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions standard/bridge-v1/cmd/user_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func handlePendingTxes(
log.Fatal().Err(err).Msg("failed to check pending transactions")
}
if !exist {
log.Info().Msg("No pending transactions exist for signing account.")
return
}
if autoCancel {
Expand Down
38 changes: 24 additions & 14 deletions standard/bridge-v1/pkg/shared/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,50 @@ func BoostTipForTransactOpts(
opts *bind.TransactOpts,
srcClient *ethclient.Client,
) error {
// Regenerate suggestions from current mempool state
newGasTip, newGasPrice, err := SuggestGasTipCapAndPrice(ctx, srcClient)
log.Debug().Msgf("Gas params for tx that was not included: Gas tip: %s wei, gas fee cap: %s wei, base fee: %s wei",
opts.GasTipCap.String(), opts.GasFeeCap.String(), new(big.Int).Sub(opts.GasFeeCap, opts.GasTipCap).String())

newGasTip, newFeeCap, err := SuggestGasTipCapAndPrice(ctx, srcClient)
if err != nil {
return fmt.Errorf("failed to suggest gas tip cap and price: %w", err)
}

newBaseFee := new(big.Int).Sub(newGasPrice, newGasTip)
newBaseFee := new(big.Int).Sub(newFeeCap, newGasTip)
if newBaseFee.Cmp(big.NewInt(0)) == -1 {
return fmt.Errorf("new base fee cannot be negative: %s", newBaseFee.String())
}

prevBaseFee := new(big.Int).Sub(opts.GasFeeCap, opts.GasTipCap)
if prevBaseFee.Cmp(big.NewInt(0)) == -1 {
return fmt.Errorf("base fee cannot be negative: %s", prevBaseFee.String())
}

var maxBaseFee *big.Int
if newBaseFee.Cmp(prevBaseFee) == 1 {
maxBaseFee = newBaseFee
} else {
maxBaseFee = prevBaseFee
}

var maxGasTip *big.Int
if newGasTip.Cmp(opts.GasTipCap) == 1 {
maxGasTip = newGasTip
} else {
maxGasTip = opts.GasTipCap
}

// Boost tip suggestion by just above 10% for max(new, old)
boostedTip := new(big.Int).Add(maxGasTip, new(big.Int).Div(maxGasTip, big.NewInt(10)))
boostedTip = boostedTip.Add(boostedTip, big.NewInt(1))

baseFee := new(big.Int).Sub(opts.GasFeeCap, opts.GasTipCap)
if baseFee.Cmp(big.NewInt(0)) == -1 {
return fmt.Errorf("base fee cannot be negative: %s", baseFee.String())
}

log.Debug().Msgf("Gas params for tx that was not included: Gas tip: %s wei, gas fee cap: %s wei, base fee: %s wei", opts.GasTipCap.String(), opts.GasFeeCap.String(), baseFee.String())
log.Debug().Msg("Tip will be boosted by 10%, base fee will be new suggestion")
boostedBaseFee := new(big.Int).Add(maxBaseFee, new(big.Int).Div(maxBaseFee, big.NewInt(10)))
boostedBaseFee = boostedBaseFee.Add(boostedBaseFee, big.NewInt(1))

opts.GasTipCap = boostedTip
opts.GasFeeCap = new(big.Int).Add(newBaseFee, boostedTip)
opts.GasFeeCap = new(big.Int).Add(boostedBaseFee, boostedTip)

log.Debug().Msgf("Boosted gas tip to %s wei and gas fee cap to %s wei. New base fee: %s wei", opts.GasTipCap.String(), opts.GasFeeCap.String(), newBaseFee.String())
log.Debug().Msg("Tip and base fee will be boosted by 10%")
log.Debug().Msgf("Boosted gas tip cap to %s wei and gas fee cap to %s wei. Base fee: %s wei",
opts.GasTipCap.String(), opts.GasFeeCap.String(), boostedBaseFee.String())

return nil
}
Expand All @@ -118,7 +128,7 @@ func WaitMinedWithRetry(
submitTx TxSubmitFunc,
) (*types.Receipt, error) {

const maxRetries = 5
const maxRetries = 10
var err error
var tx *types.Transaction

Expand Down

0 comments on commit 5fd8abf

Please sign in to comment.