Skip to content
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

fix: cancellation transaction #554

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions x/contracts/txmonitor/canceller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/primev/mev-commit/x/keysigner"
)

const basefeeWiggleMultiplier = 2

type Canceller struct {
chainID *big.Int
ethClient *ethclient.Client
Expand All @@ -37,25 +39,24 @@ func NewCanceller(
}
}

func (c *Canceller) suggestMaxFeeAndTipCap(
ctx context.Context,
gasPrice *big.Int,
) (*big.Int, *big.Int, error) {
func (c *Canceller) suggestMaxFeeAndTipCap(ctx context.Context) (*big.Int, *big.Int, error) {
// Returns priority fee per gas
gasTipCap, err := c.ethClient.SuggestGasTipCap(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to suggest gas tip cap: %w", err)
}

// Returns priority fee per gas + base fee per gas
if gasPrice == nil {
gasPrice, err = c.ethClient.SuggestGasPrice(ctx)
if err != nil {
return nil, nil, fmt.Errorf("failed to suggest gas price: %w", err)
}
head, err := c.ethClient.HeaderByNumber(ctx, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to get latest block header: %w", err)
}

return gasPrice, gasTipCap, nil
gasFeeCap := new(big.Int).Add(
gasTipCap,
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)),
)

return gasFeeCap, gasTipCap, nil
}

func (c *Canceller) CancelTx(ctx context.Context, txnHash common.Hash) (common.Hash, error) {
Expand All @@ -68,7 +69,7 @@ func (c *Canceller) CancelTx(ctx context.Context, txnHash common.Hash) (common.H
return common.Hash{}, ethereum.NotFound
}

gasFeeCap, gasTipCap, err := c.suggestMaxFeeAndTipCap(ctx, txn.GasPrice())
gasFeeCap, gasTipCap, err := c.suggestMaxFeeAndTipCap(ctx)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to suggest max fee and tip cap: %w", err)
}
Expand Down
Loading