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
  • Prerequisites
  • Step 1 : Imports
  • Step 2 : Environment Variables Retrieval
  • Step 3 : Main Function
  • Step 4: Execution

Was this helpful?

  1. Hedera
  2. Tutorials
  3. Accounts

Transfer HBAR

In this section, you will learn how to transfer HBAR from your account to another on the Hedera test network.

Prerequisites

  • Completed the Introduction step.

  • Completed the Environment Setup step.

  • Completed the Created an Account step.

Step 1 : Imports

  • The script imports necessary modules from the Hedera JavaScript SDK: Client, AccountBalanceQuery, TransferTransaction, Hbar, and PrivateKey.

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

const {
    Client,
    AccountBalanceQuery,
    TransferTransaction,
    Hbar,
    PrivateKey 
} = require("@hashgraph/sdk");
require('dotenv').config({ path: 'Account/.env' });

Step 2 : Environment Variables Retrieval

These lines retrieve environment variables such as MY_ACCOUNT_ID, MY_PRIVATE_KEY, and OTHER_ACCOUNT_ID from the .env file using process.env.

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

Step 3 : Main Function

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

  • It creates a TransferTransaction object to transfer HBars between accounts.

  • The transaction subtracts 10 HBars from the operator account (myAccountId) and adds 10 HBars to the other account (otherAccountId).

  • The transaction is executed, and the resulting transaction ID (txId) is obtained.

  • The receipt of the transaction is retrieved, and the transaction consensus status is obtained from the receipt.

  • Queries are created to retrieve the account balances of both the operator account and the other account.

  • The queries are executed, and the account balances are logged to the console.

async function main() {
    const client = Client.forTestnet(); // Create a client for the Hedera testnet
    client.setOperator(myAccountId, myPrivateKey); // Set the operator account for the client

    const transaction = new TransferTransaction() // Create a transfer transaction
    .addHbarTransfer(myAccountId, new Hbar(-10)) // Subtract 10 HBars from the operator account
    .addHbarTransfer(otherAccountId, new Hbar(10)); // Add 10 HBars to the other account

    console.log(`Doing transfer from ${myAccountId} to ${otherAccountId}`);

    const txId = await transaction.execute(client); // Execute the transfer transaction
    const receipt = await txId.getReceipt(client); // Get the receipt of the transaction
    const transactionStatus = receipt.status; // Get the transaction consensus status

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

    const queryMine = new AccountBalanceQuery().setAccountId(myAccountId); // Create a query for operator account balance
    const queryOther = new AccountBalanceQuery().setAccountId(otherAccountId); // Create a query for other account balance

    const accountBalanceMine = await queryMine.execute(client); // Execute the query for operator account balance
    const accountBalanceOther = await queryOther.execute(client); // Execute the query for other account balance

    console.log(`My account balance ${accountBalanceMine.hbars} HBar, other account balance ${accountBalanceOther.hbars}`);
}

Step 4: Execution

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

main();
PreviousGet Other Account InfoNextConsensus Service

Last updated 1 year ago

Was this helpful?