Applied Blockchain Center (ABC)
  • System requirements for ABC
  • Hedera
    • 🅰️About Hedera
    • 🖥️Environment Setup
    • Tutorials
      • Accounts
        • Get My Account Info
        • Get Other Account Info
        • Transfer HBAR
      • Consensus Service
        • Create Topic
        • Subscribe to a topic
        • Submit a message
      • File Service
        • Create a File
        • Retrieve File
      • Scheduled_TX
        • Create Scheduled Transaction
        • Scheduled Transaction Info
        • Delete Scheduled Transaction
        • Submit Signature
      • SmartContract Service
        • Deploy to Hedera
        • Seal Document
        • Get Seal
        • Check Seal Revocation Status
        • Revoke Document
      • Token Service
        • Fungible Token
        • Non-fungible Token
  • Hyperledger Fabric
    • 📑About Hyperledger Fabric
    • 💻Prerequisite Installation
    • Fabric Installation
      • 🧪Installation & Test
      • ✈️Launch Network
      • ⛓️Channel Creation
      • 🚚Chaincode Deployment
      • ▶️Chaincode Execution
  • Hyperledger Besu
    • Besu Network Set
Powered by GitBook
On this page
  • Summary
  • Step 1: Imports and Environment Setup
  • Step 2: Environment Variables Retrieval and Validation
  • Step 3: Client Setup
  • Step 4: Main Function
  • Step 5: Execution

Was this helpful?

  1. Hedera
  2. Tutorials
  3. Scheduled_TX

Create Scheduled Transaction

Summary

This script sets up a scheduled transaction to transfer cryptocurrency between two accounts on the Hedera network. It demonstrates the process of creating a scheduled transaction, setting up the scheduled transaction parameters, executing the schedule transaction, and obtaining information about the created schedule.

Step 1: Imports and Environment Setup

  • The script imports necessary modules from the Hedera JavaScript SDK (@hashgraph/sdk): TransferTransaction, Client, ScheduleCreateTransaction, PrivateKey, Hbar, and ScheduleInfoQuery.

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

const {
    TransferTransaction,
    Client,
    ScheduleCreateTransaction,
    PrivateKey,
    Hbar,
    ScheduleInfoQuery
} = 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 IDs and private keys 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 otherAccountId = process.env.OTHER_ACCOUNT_ID;
const otherAccountId2 = process.env.OTHER_ACCOUNT_ID_2;
const otherPrivateKey = PrivateKey.fromString(process.env.OTHER_PRIVATE_KEY);

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

  • This is the main function named main().

  • It creates a TransferTransaction instance to transfer cryptocurrency between two accounts.

  • It transfers 100 Hbars from myAccountId to otherAccountId and transfers 100 Hbars from myAccountId to otherAccountId2.

async function main() {
    const transaction = new TransferTransaction()
        .addHbarTransfer(otherAccountId, Hbar.fromTinybars(-100))
        .addHbarTransfer(otherAccountId2, Hbar.fromTinybars(100));
  • It creates a ScheduleCreateTransaction instance to schedule the previously created transfer transaction.

  • It sets the scheduled transaction to the transfer transaction created earlier, sets a memo for the schedule, and sets the admin key (private key) for the schedule.

  • The schedule transaction is then executed using the client.

const scheduleTransaction = await new ScheduleCreateTransaction()
    .setScheduledTransaction(transaction)
    .setScheduleMemo("Scheduled TX!")
    .setAdminKey(myPrivateKey)
    .execute(client);
  • t retrieves the receipt of the schedule transaction to obtain information about the created schedule.

  • It extracts the schedule ID and the ID of the scheduled transaction from the receipt.

  • The script then logs the schedule ID and the scheduled transaction ID to the console.

const receipt = await scheduleTransaction.getReceipt(client);

const scheduleId = receipt.scheduleId;
console.log("The schedule ID is " +scheduleId);

const scheduledTxId = receipt.scheduledTransactionId;
console.log("The scheduled transaction ID is " +scheduledTxId);

process.exit();

Step 5: Execution

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

main();
PreviousScheduled_TXNextScheduled Transaction Info

Last updated 1 year ago

Was this helpful?