-
Notifications
You must be signed in to change notification settings - Fork 16
Add support for price bump when tx_type="auto" #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ import ( | |||||||||||||||
| "github.com/ethereum/go-ethereum/common" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| const basefeeWiggleMultiplier = 2 | ||||||||||||||||
|
|
||||||||||||||||
| type GasFeeCalculator struct { | ||||||||||||||||
| client IChainClient | ||||||||||||||||
| config *ChainConfig | ||||||||||||||||
|
|
@@ -35,15 +37,9 @@ func (m *GasFeeCalculator) Apply(ctx context.Context, txOpts *bind.TransactOpts) | |||||||||||||||
| } | ||||||||||||||||
| switch m.config.TxType { | ||||||||||||||||
| case TxTypeLegacy: | ||||||||||||||||
| gasPrice, err := m.client.SuggestGasPrice(ctx) | ||||||||||||||||
| gasPrice, err := m.calculateGasPrice(ctx, oldTx, minFeeCap) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return fmt.Errorf("failed to suggest gas price: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
| if oldTx != nil && oldTx.GasPrice != nil && oldTx.GasPrice.ToInt().Cmp(gasPrice) > 0 { | ||||||||||||||||
| return fmt.Errorf("old tx's gasPrice(%v) is higher than suggestion(%v)", oldTx.GasPrice.ToInt(), gasPrice) | ||||||||||||||||
| } | ||||||||||||||||
| if gasPrice.Cmp(minFeeCap) < 0 { | ||||||||||||||||
| gasPrice = minFeeCap | ||||||||||||||||
| return fmt.Errorf("failed to calculate gas price: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
| txOpts.GasPrice = gasPrice | ||||||||||||||||
| return nil | ||||||||||||||||
|
|
@@ -85,9 +81,62 @@ func (m *GasFeeCalculator) Apply(ctx context.Context, txOpts *bind.TransactOpts) | |||||||||||||||
| txOpts.GasFeeCap = gasFeeCap | ||||||||||||||||
| txOpts.GasTipCap = gasTipCap | ||||||||||||||||
| return nil | ||||||||||||||||
| case TxTypeAuto: | ||||||||||||||||
| // Calculate gas options in the same way as bind.BoundContract.transact | ||||||||||||||||
| head, err := m.client.HeaderByNumber(ctx, nil) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return fmt.Errorf("failed to get latest header: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if head.BaseFee == nil { | ||||||||||||||||
| gasPrice, err := m.calculateGasPrice(ctx, oldTx, minFeeCap) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return fmt.Errorf("failed to calculate gas price: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
| txOpts.GasPrice = gasPrice | ||||||||||||||||
| return nil | ||||||||||||||||
| } else { | ||||||||||||||||
| gasTipCap, err := m.client.SuggestGasTipCap(ctx) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
| if gasTipCap.Cmp(minTipCap) < 0 { | ||||||||||||||||
| gasTipCap = minTipCap | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| gasFeeCap := new(big.Int).Add( | ||||||||||||||||
| gasTipCap, | ||||||||||||||||
| new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)), | ||||||||||||||||
| ) | ||||||||||||||||
| if gasFeeCap.Cmp(minFeeCap) < 0 { | ||||||||||||||||
| gasFeeCap = minFeeCap | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if gasFeeCap.Cmp(gasTipCap) < 0 { | ||||||||||||||||
| return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap) | ||||||||||||||||
| } | ||||||||||||||||
|
||||||||||||||||
| } | |
| } | |
| if oldTx != nil && oldTx.GasFeeCap != nil && oldTx.GasTipCap != nil { | |
| if oldTx.GasFeeCap.ToInt().Cmp(gasFeeCap) >= 0 && oldTx.GasTipCap.ToInt().Cmp(gasTipCap) >= 0 { | |
| return fmt.Errorf("old tx's gasFeeCap(%v) and gasTipCap(%v) are greater than or equal to suggestion(%v, %v)", oldTx.GasFeeCap.ToInt(), oldTx.GasTipCap.ToInt(), gasFeeCap, gasTipCap) | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the validation in the commit 5c4ff80
Uh oh!
There was an error while loading. Please reload this page.