cancelPrivateTransaction - SDK

Stops the provided private transaction from being submitted for future blocks. A transaction can only be canceled if the same key signs the request as the sendPrivateTransaction call submitting the transaction in the first place.

Don’t have an API key?

Start using this method in your app today.

Description

Stops the provided private transaction from being submitted for future blocks. A transaction can only be canceled if the same key signs the request as the sendPrivateTransaction call submitting the transaction in the first place.

Please note that fast mode transactions cannot be canceled using this method.

Returns a boolean indicating whether the cancellation was successful.

Parameters

NameTypeDescription
transactionHashstringTransaction hash of private transaction to be cancelled.

Response

PropertyTypeDescription
Promise<boolean>booleanReturns the transaction cancellation status.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

npm install alchemy-sdk
yarn add alchemy-sdk

Request

// Imports the Alchemy SDK
const { Alchemy, Network, Wallet, Utils } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {
    // define the transaction hash
    const txHash = "0x2e8dff1ae477808ec0682c27fbdd250a2e628090fe4e901e644c942628113b37"

    //Call the method to cancel the transaction based on the transaction hash
    const response = await alchemy.transact.cancelPrivateTransaction(txHash)

    //Logging the response to the console
    console.log(response)
}

main();

Response

true

Use Cases

Here are some potential use cases for the cancelPrivateTransaction method:

  • Error in transaction parameters: If the sender realizes that they have made an error in the transaction parameters, such as sending the wrong amount or specifying the wrong recipient, they can cancel the transaction before it is processed.

  • Privacy concerns: If the sender realizes that the transaction includes sensitive information that they do not want to be visible on the blockchain, they can cancel the transaction before it is processed.

  • High gas price: If the gas price is too high, the sender may want to cancel the transaction and resubmit it with a lower gas price to save on transaction fees.

Related Methods

Here are the methods related to cancelPrivateTransaction:

  • sendPrivateTransaction: Used to send a single transaction to Flashbots. Flashbots will attempt to send the transaction to miners for the next 25 blocks.