Skip to content

Commit

Permalink
flag to add gas fee ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhou committed Apr 7, 2024
1 parent 1946320 commit 5fa5835
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 4 additions & 3 deletions eth/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type txOptions struct {
maxFeePerGasGwei uint64 // aka GasFeeCap in gwei
maxPriorityFeePerGasGwei float64 // aka GasTipCap in gwei
addPriorityFeePerGasGwei float64
addPriorityFeeRatio float64
// For both Legacy and EIP-1559
addGasFeeRatio float64
// Gas limit
gasLimit uint64
addGasEstimateRatio float64
Expand Down Expand Up @@ -142,9 +143,9 @@ func WithAddPriorityFeePerGasGwei(g float64) TxOption {
})
}

func WithAddPriorityFeeRatio(r float64) TxOption {
func WithAddGassFeeRatio(r float64) TxOption {
return newFuncTxOption(func(o *txOptions) {
o.addPriorityFeeRatio = r
o.addGasFeeRatio = r
})
}

Expand Down
11 changes: 6 additions & 5 deletions eth/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ func (t *Transactor) determineGas(method TxMethod, signer *bind.TransactOpts, tx
// 1. Determine gas price
// Only accept legacy flags or EIP-1559 flags, not both
hasLegacyFlags := txopts.forceGasGwei != nil || txopts.minGasGwei > 0 || txopts.maxGasGwei > 0 || txopts.addGasGwei > 0
has1559Flags := txopts.maxFeePerGasGwei > 0 || txopts.maxPriorityFeePerGasGwei > 0 ||
txopts.addPriorityFeePerGasGwei > 0 || txopts.addPriorityFeeRatio > 0
has1559Flags := txopts.maxFeePerGasGwei > 0 || txopts.maxPriorityFeePerGasGwei > 0 || txopts.addPriorityFeePerGasGwei > 0
if hasLegacyFlags && has1559Flags {
return ErrConflictingGasFlags
}
Expand Down Expand Up @@ -295,15 +294,15 @@ func determine1559GasPrice(signer *bind.TransactOpts, txopts txOptions, client *
}
if txopts.maxPriorityFeePerGasGwei > 0 {
signer.GasTipCap = new(big.Int).SetUint64(uint64(txopts.maxPriorityFeePerGasGwei * 1e9))
} else if txopts.addPriorityFeePerGasGwei > 0 || txopts.addPriorityFeeRatio > 0 {
} else if txopts.addPriorityFeePerGasGwei > 0 || txopts.addGasFeeRatio > 0 {
suggestedGasTipCap, err := client.SuggestGasTipCap(context.Background())
if err != nil {
return fmt.Errorf("failed to call SuggestGasTipCap: %w", err)
}
if txopts.addPriorityFeePerGasGwei > 0 {
signer.GasTipCap = new(big.Int).SetUint64(uint64(txopts.addPriorityFeePerGasGwei*1e9) + suggestedGasTipCap.Uint64())
} else if txopts.addPriorityFeeRatio > 0 {
signer.GasTipCap = new(big.Int).SetUint64(uint64(float64(suggestedGasTipCap.Uint64()) * (1 + txopts.addGasEstimateRatio)))
} else if txopts.addGasFeeRatio > 0 {
signer.GasTipCap = new(big.Int).SetUint64(uint64(float64(suggestedGasTipCap.Uint64()) * (1 + txopts.addGasFeeRatio)))
}
}
return nil
Expand All @@ -323,6 +322,8 @@ func determineLegacyGasPrice(
if txopts.addGasGwei > 0 { // Add gas price to the suggested value to speed up transactions
addPrice := new(big.Int).SetUint64(uint64(txopts.addGasGwei * 1e9))
gasPrice.Add(gasPrice, addPrice)
} else if txopts.addGasFeeRatio > 0 {
gasPrice = new(big.Int).SetUint64(uint64(float64(gasPrice.Uint64()) * (1 + txopts.addGasFeeRatio)))
}
if txopts.minGasGwei > 0 { // gas can't be lower than minGas
minPrice := new(big.Int).SetUint64(uint64(txopts.minGasGwei * 1e9))
Expand Down

0 comments on commit 5fa5835

Please sign in to comment.