Skip to main content

Cancel Transaction

Cancels a pending blockchain transaction by sending a replacement transaction with zero value and higher gas price.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • Client ID - The client ID from the Create Client node.
  • Hash - The transaction hash to cancel (must start with "0x").
  • Gas Price - The new gas price in hexadecimal format (must be higher than the original).
  • Gas Limit - The gas limit for the cancellation transaction (21000 is standard).

Output

  • Hash - The transaction hash of the cancellation transaction.

How It Works

The Cancel Transaction node creates a replacement transaction to cancel a pending one. When executed, the node:

  1. Retrieves the original transaction using its hash
  2. Creates a new transaction with:
    • Same nonce as the original
    • Same destination (to address)
    • Zero value (no ETH transferred)
    • Higher gas price (to prioritize over original)
  3. Signs the cancellation transaction
  4. Broadcasts it to the network
  5. Returns the new transaction hash

Requirements

  • The original transaction must still be pending (not yet confirmed)
  • Gas price must be significantly higher than the original (typically 10-20% higher)
  • Sufficient balance to pay for gas fees
  • Same wallet that sent the original transaction

Error Handling

The node will return errors for:

  • Invalid or missing Client ID
  • Invalid hash format (must start with "0x")
  • Transaction not found or already confirmed
  • Gas price not in hex format
  • Gas price not higher than original
  • Insufficient balance for gas
  • Network connection failures

How Transaction Cancellation Works

Transaction Replacement:

  • Blockchain allows replacing pending transactions with the same nonce
  • Higher gas price gives the new transaction priority
  • Miners will include the transaction with higher gas price
  • Only one transaction with a given nonce can be confirmed
  • If replacement confirms first, original is dropped

Why it works:

  • Uses same nonce as original transaction
  • Higher gas price attracts miners
  • Zero value means safe cancellation
  • Original transaction becomes invalid

Usage Notes

  • Only works for pending transactions (not yet mined)
  • Gas price must be meaningfully higher (10-30% recommended)
  • Once a transaction is confirmed, it cannot be cancelled
  • The cancellation itself costs gas fees
  • Both transactions cannot confirm (only one per nonce)
  • Cancellation is not guaranteed if original confirms first

Example

Cancelling a stuck transaction:

First, get the current gas price and increase it:

originalGasPrice = 50 GWEI
newGasPrice = 65 GWEI (30% higher)
newGasPriceHex = "0x00000000000000000000000000000000000000000000000000000f2b9ec5200"

Inputs:

  • Client ID: 7e8f9a0b-1c2d-3e4f-5a6b-7c8d9e0f1a2b
  • Hash: 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
  • Gas Price: 0x00000000000000000000000000000000000000000000000000000f2b9ec5200 (65 GWEI)
  • Gas Limit: 21000

Output:

  • Hash: 0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890

Common Use Cases

Stuck Transactions: Cancel transactions stuck in mempool due to low gas price.

Changed Requirements: Cancel transactions that are no longer needed.

Wrong Parameters: Cancel transactions sent with incorrect parameters.

Emergency Stops: Quickly cancel potentially problematic transactions.

Gas Price Errors: Fix transactions sent with gas prices too low.

Duplicate Transactions: Cancel accidentally duplicated transactions.

Gas Price Calculation

Recommended increases:

  • Slow cancellation: 10-15% higher
  • Normal cancellation: 20-30% higher
  • Fast cancellation: 50-100% higher
  • Emergency cancellation: 200-300% higher

Example:

original = 50 GWEI
normal = 50 * 1.25 = 62.5 GWEI
fast = 50 * 1.5 = 75 GWEI
emergency = 50 * 2.0 = 100 GWEI

Complete Cancellation Flow

  1. Check Status - Verify transaction is still pending
  2. Get Transaction - Retrieve original transaction details
  3. Calculate Gas - Determine new gas price (20-30% higher)
  4. To WEI - Convert gas price to hex format
  5. Cancel Transaction - Send cancellation
  6. Monitor - Watch both transactions
  7. Verify - Confirm cancellation succeeded

Monitoring After Cancellation

Track both transactions:

originalHash = "0x1234..."
cancellationHash = "0xabcd..."

// Check which one confirms first
while (true) {
originalReceipt = getReceipt(originalHash)
cancellationReceipt = getReceipt(cancellationHash)

if (cancellationReceipt) {
// Cancellation succeeded!
break
} else if (originalReceipt) {
// Original went through, cancellation failed
break
}

wait(5000) // Check every 5 seconds
}

When Cancellation Fails

Cancellation may fail if:

  • Original transaction confirms before cancellation
  • Gas price increase wasn't enough
  • Network very congested
  • Cancellation transaction has errors

If cancellation fails:

  • Transaction will eventually confirm or drop
  • Can try again with even higher gas price
  • Wait for original to confirm and handle accordingly

Alternative: Speed Up Transaction

Instead of cancelling, you can speed up a transaction:

  • Use Increase Gas node instead
  • Sends same transaction with higher gas
  • Preferred when you still want the transaction

Cost Considerations

Costs involved:

  • Gas for cancellation transaction (21,000 gas)
  • Higher gas price premium
  • Loss of original transaction's gas if it confirms

Example cost:

gasLimit = 21000
gasPrice = 65 GWEI
cost = 21000 * 65 = 1,365,000 GWEI = 0.001365 ETH

When to cancel:

  • Potential loss from original > cancellation cost
  • Original transaction no longer needed
  • Original has wrong parameters
  • Original gas price extremely low

Best Practices

Before cancelling:

  1. Verify transaction is still pending
  2. Calculate cost of cancellation
  3. Ensure higher gas price is sufficient
  4. Check wallet has enough balance
  5. Consider if speed-up is better option

After cancelling:

  1. Monitor both transactions
  2. Be prepared for either outcome
  3. Don't rely on cancellation success
  4. Have contingency plans

Prevention:

  • Always estimate gas before sending
  • Use appropriate gas prices initially
  • Double-check transaction parameters
  • Test with small amounts first
warning

Cancellation is not guaranteed. The original transaction may confirm before the cancellation, especially if the gas price difference is small or network is congested.

danger

Both the original transaction and cancellation cost gas fees. If cancellation fails, you pay gas for both transactions. Only cancel when necessary.

tip

For a higher chance of successful cancellation, use a gas price at least 30% higher than the original, and send the cancellation as soon as possible after the original.

Troubleshooting

"Transaction not found"

  • Hash is incorrect
  • Transaction already confirmed
  • Transaction was dropped from mempool

"Cannot send cancellation"

  • Insufficient balance for gas
  • Gas price too low
  • Nonce conflict

Cancellation didn't work

  • Original confirmed first
  • Gas price increase was insufficient
  • Try again with higher gas price

Transaction Lifecycle

States:

  1. Pending - Can be cancelled
  2. Confirmed - Cannot be cancelled
  3. Dropped - No longer needed to cancel
  4. Replaced - Successfully cancelled

Always check the current state before attempting cancellation.