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, andScheduleInfoQuery.It also imports the
dotenvmodule to load environment variables from a.envfile located in theScheduled_TXdirectory.
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
.envfile.It validates that the required environment variables (
MY_ACCOUNT_IDandMY_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
TransferTransactioninstance to transfer cryptocurrency between two accounts.It transfers 100 Hbars from
myAccountIdtootherAccountIdand transfers 100 Hbars frommyAccountIdtootherAccountId2.
async function main() {
const transaction = new TransferTransaction()
.addHbarTransfer(otherAccountId, Hbar.fromTinybars(-100))
.addHbarTransfer(otherAccountId2, Hbar.fromTinybars(100));
It creates a
ScheduleCreateTransactioninstance 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();Last updated
Was this helpful?