Skip to content

Commit

Permalink
fix: cancellation transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jan 4, 2025
1 parent 8fbcb47 commit b459dcb
Showing 1 changed file with 13 additions and 12 deletions.
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

0 comments on commit b459dcb

Please sign in to comment.