Transfer hbar

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

Pre-requisites:

Note: This example uses Hedera JavaScript SDK 1.1.2. The latest version may not be compatible with the provided examples. View Hedera Hashgraph's JavaScript SDK here.

Step 1: Import the following modules

You will likely already have a few of these modules listed from the previous sections.

index.js
index.js
// Import the following modules
const { Client, Ed25519PrivateKey, AccountCreateTransaction, AccountBalanceQuery, CryptoTransferTransaction, Hbar } = require("@hashgraph/sdk");
require("dotenv").config();

Step 2: Create a transfer transaction

You should already have a new account ID from the account you created from the Create an account section. You will transfer 1,000 tinybars from your account to the new account. The account sending hbars is the signature that is required for this transaction to be processed.

const transferTransactionId = await new CryptoTransferTransaction()
.addSender(myAccountId, 1000)
.addRecipient(newAccountId, 1000)
.execute(client);

The net value of the transfer must equal zero (total number of hbars sent by the sender must equal the total number of hbars received by the recipient).

Step 3: Verify the transfer transaction reached consensus

To verify the transfer transaction reached consensus by the network, you will submit a request to obtain the receipt of the transaction. The receipt will let you know if the transaction was successful (reached consensus) or not.

const transactionReceipt = await transferTransactionId.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status);

Step 4: Get the updated account balance

4.1 Get the cost of requesting the query

You can request the cost of a query prior to submitting the query to the Hedera network. Checking an account balance is free of charge today. You can verify that by the method below.

//Request the cost of the query
const getBalanceCost = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.getCost(client);
console.log(`The cost of query is: ${getBalanceCost}`);

4.2 Get the account balance

You will verify the account balance was updated for the new account by requesting a get account balance query. The current account balance should be the sum of the initial balance (1,000 tinybar) plus the transfer amount (1,000 tinybar) and equal to 2,000 tinybars.

//Check the new account's balance
const getNewBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The account balance after the transfer is: " +getNewBalance +" tinybar.")

Congratulations! You have successfully transferred hbars to another account on the Hedera testnet! If you have followed the tutorial from the beginning, you have completed the following thus far:

  • Set-up your Hedera envrionment to submit transactions and queries

  • Created an account

  • Transferred hbars to another account

Do you want to keep learning? Visit our advanced section to take your learning experience to the next level. You can also find additional JavaScript SDK examples here.

Code Check

Your complete index.js file should look something like this:

const { Client, Ed25519PrivateKey, AccountCreateTransaction, AccountBalanceQuery, CryptoTransferTransaction } = require("@hashgraph/sdk");
require("dotenv").config();
async function main() {
//Grab your Hedera testnet account ID and private key
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = process.env.MY_PRIVATE_KEY;
// If we weren't able to grab it, we should throw a new error
if (myAccountId == null ||
myPrivateKey == null ) {
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
}
// Create our connection to the Hedera network
// The Hedera JS SDK makes this reallyyy easy!
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
//Create new keys
const newAccountPrivateKey = await Ed25519PrivateKey.generate();
const newAccountPublicKey = newAccountPrivateKey.publicKey;
//Create a new account with 1,000 tinybar starting balance
const newAccountTransactionId = await new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance(1000)
.execute(client);
//Get the account ID
const getReceipt = await newAccountTransactionId.getReceipt(client);
const newAccountId = getReceipt.getAccountId();
console.log("The new account ID is: " +newAccountId);
//Verify the account balance
const accountBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The new account balance is: " +accountBalance +" tinybar.");
//Create the transfer transaction
const transferTransactionId = await new CryptoTransferTransaction()
.addSender(myAccountId, 1000)
.addRecipient(newAccountId, 1000)
.execute(client);
//Verify the transaction reached consensus
const transactionReceipt = await transferTransactionId.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status);
//Request the cost of the query
const getBalanceCost = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.getCost(client);
console.log(`The cost of query is: ${getBalanceCost}`);
//Check the new account balance
const getNewBalance = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
console.log("The account balance after the transfer is: " +getNewBalance +" tinybar.")
}
main();