Delete Scheduled Transaction

Summary:

This script deletes a scheduled transaction on the Hedera network using its schedule ID. It demonstrates how to use the ScheduleDeleteTransaction to create a transaction for deleting a scheduled transaction, sign it with the admin key, execute it on the Hedera network, and retrieve the transaction status to confirm the deletion operation

Step 1: Imports and Environment Setup

const {
    ScheduleDeleteTransaction,
    Client,
    PrivateKey
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'Scheduled_TX/.env' });
  • The script imports necessary modules from the Hedera JavaScript SDK (@hashgraph/sdk): ScheduleDeleteTransaction, Client, and PrivateKey.

  • It also imports the dotenv module to load environment variables from a .env file located in the Scheduled_TX directory.

Step 2: Environment Variables Retrieval and Validation

const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);

const scheduleId = process.env.SCHEDULE_ID;

if (myAccountId == null ||
    myPrivateKey == null ) {
    throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
  • The code retrieves necessary environment variables such as account ID and private key from the .env file.

  • It validates that the required environment variables (MY_ACCOUNT_ID and MY_PRIVATE_KEY) are present. If not, it throws an error.

Step 3: Client Setup

const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
  • It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.

Step 4: Main Function

async function main() {
    const transaction = await new ScheduleDeleteTransaction()
        .setScheduleId(scheduleId)
        .freezeWith(client)
        .sign(myPrivateKey);

    const txResponse = await transaction.execute(client);

    const receipt = await txResponse.getReceipt(client);

    const transactionStatus = receipt.status;
    console.log("The transaction consensus status is " +transactionStatus);

    process.exit();
}
  • This is the main function named main().

  • It creates a ScheduleDeleteTransaction instance to delete the scheduled transaction using the provided schedule ID.

  • The transaction is frozen with the client, signed with the admin key (private key), and executed using the client.

  • The script then retrieves the receipt of the transaction to obtain its status, which indicates the consensus status of the deletion operation.

  • The transaction consensus status is logged to the console.

Step 5: Execution:

main();
  • This line calls the main() function to start the execution of the script.

Last updated