Scheduled Transaction Info

Summary

This script retrieves and displays information about a scheduled transaction on the Hedera network using its schedule ID. It demonstrates how to use the ScheduleInfoQuery to query schedule information and extract relevant data such as schedule ID, memo, creator account ID, payer account ID, expiration time, and execution time.

Step 1: Imports and Environment Setup

  • The script imports necessary modules from the Hedera JavaScript SDK (@hashgraph/sdk): Client, PrivateKey, ScheduleInfoQuery, Timestamp, ScheduleId, AccountId, and TransactionId.

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

const {
    Client,
    PrivateKey,
    ScheduleInfoQuery,
    Timestamp,
    ScheduleId,
    AccountId,
    TransactionId
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'Scheduled_TX/.env' });

Step 2 : Environment Variables Retrieval and Validation

  • 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.

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");
}

Step 3: Client Setup

It creates a client instance for the Hedera testnet and sets the operator account using the user's account ID and private key.

const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);

Step 4: Main Function

  • It creates a ScheduleInfoQuery instance to retrieve information about the scheduled transaction using the provided schedule ID.

  • The query is executed with the client to obtain the schedule information.

  • Information about the schedule, such as schedule ID, memo, creator account ID, payer account ID, expiration time, and execution time, is logged to the console.

async function main() {
    const query = new ScheduleInfoQuery()
        .setScheduleId(scheduleId);

    const info = await query.execute(client);
    console.log("The scheduledId you queried for is: ", new ScheduleId(info.scheduleId).toString());
    console.log("The memo for it is: ", info.scheduleMemo);
    console.log("It got created by: ", new AccountId(info.creatorAccountId).toString());
    console.log("It got paid by: ", new AccountId(info.payerAccountId).toString());
    console.log("The expiration time of the scheduled tx is: ", new Timestamp(info.expirationTime).toDate());
    if(new Timestamp(info.executed).toDate().getTime() === new Date("1970-01-01T00:00:00.000Z").getTime()) {
        console.log("The transaction has not been executed yet.");
    } else {
        console.log("The time of execution of the scheduled tx is: ", new Timestamp(info.executed).toDate());
    }

    process.exit();
}

Step 5: Execution

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

main();

Last updated