Transfer HBAR
Summary
In this section, you will learn how to transfer HBAR from your account to another on the Hedera test network.
Prerequisites:
Step 1. Create a transfer transaction
Use your new account created in the "Create an account" section and transfer 1,000 tinybars from your account to the new account. The account sending the HBAR needs to sign the transaction using its private keys to authorize the transfer. Since you are transferring from the account associated with the client, you do not need to explicitly sign the transaction as the operator account(account transferring the HBAR) signs all transactions to authorize the payment of the transaction fee.
//System.out.println("The new account balance is: " +accountBalance.hbars);
//-----------------------<enter code below>--------------------------------------
//Transfer HBAR
TransactionResponse sendHbar = new TransferTransaction()
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)) //Sending account
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)) //Receiving account
.execute(client);//console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
//-----------------------<enter code below>--------------------------------------
//Create the transfer transaction
const sendHbar = await new TransferTransaction()
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)) //Sending account
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)) //Receiving account
.execute(client);//Print the balance of tinybars
//fmt.Println("The account balance for the new account is ", accountBalance.Hbars.AsTinybar())
//-----------------------<enter code below>--------------------------------------
//Transfer hbar from your testnet account to the new account
transaction := hedera.NewTransferTransaction().
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
AddHbarTransfer(newAccountId,hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
//Submit the transaction to a Hedera network
txResponse, err := transaction.Execute(client)
if err != nil {
panic(err)
}Step 2. 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 status will let you know if the transaction was successful (reached consensus) or not.
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);//Verify the transaction reached consensus
const transactionReceipt = await sendHbar.getReceipt(client);
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());//Request the receipt of the transaction
transferReceipt, err := txResponse.GetReceipt(client)
if err != nil {
panic(err)
}
//Get the transaction consensus status
transactionStatus := transferReceipt.Status
fmt.Printf("The transaction consensus status is %v\n", transactionStatus)Step 3. Get the account balance
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
Hbar queryCost = new AccountBalanceQuery()
.setAccountId(newAccountId)
.getCost(client);
System.out.println("The cost of this query is: " +queryCost);//Request the cost of the query
const queryCost = await new AccountBalanceQuery()
.setAccountId(newAccountId)
.getCost(client);
console.log("The cost of query is: " +queryCost);//Create the query that you want to submit
balanceQuery := hedera.NewAccountBalanceQuery().
SetAccountID(newAccountId)
//Get the cost of the query
cost, err := balanceQuery.GetCost(client)
if err != nil {
panic(err)
}
println("The account balance query cost is:", cost.String())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 tinybars) plus the transfer amount (1,000 tinybars) and equal to 2,000 tinybars.
//Check the new account's balance
AccountBalance accountBalanceNew = new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
System.out.println("The new account balance is: " +accountBalanceNew.hbars);//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.hbars.toTinybars() +" tinybar.")//Check the new account's balance
newAccountBalancequery := hedera.NewAccountBalanceQuery().
SetAccountID(newAccountId)
//Sign with client operator private key and submit the query to a Hedera network
newAccountBalance, err := newAccountBalancequery.Execute(client)
if err != nil {
panic(err)
}
//Print the balance of tinybars
fmt.Println("The hbar account balance for this account is", newAccountBalance.Hbars.AsTinybar())⭐ Congratulations! You have successfully transferred HBAR 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 environment to submit transactions and queries.
Created an account.
Transferred HBAR to another account.
Do you want to keep learning? Visit our the SDKs & APIs section to take your learning experience to the next level. You can also find additional Java SDK examples here.
Code Check ✅
Your complete code file should look something like this:
Sample output:
The new account ID is: 0.0.4065563
The new account balance is: 1000 tinybars.
The transfer transaction from my account to the new account was: SUCCESS
The cost of query is: 0 tℏ
The account balance after the transfer is: 2000 tinybars.Last updated
Was this helpful?