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

Pre-requisites:

Note: This example uses Hedera Go SDK v2.0. The latest Hedera Go SDK version can be found here, however may not be compatible with this tutorial.

Step 1. Create a transfer transaction

You should already have a new account ID from the account you created in the "Create an account" section. You will transfer 1,000 tinybars from your testnet account to the new account. The sender account's private key is required to sign the transaction. The sender account is your testnet account so the client is already set-up to sign with your testnet account's private key to authorize the transfer.
1
//Print the balance of tinybars
2
//fmt.Println("The account balance for the new account is ", accountBalance.Hbars.AsTinybar())
3
//-----------------------<enter code below>--------------------------------------
4
5
//Transfer hbar from your testnet account to the new account
6
transaction := hedera.NewTransferTransaction().
7
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
8
AddHbarTransfer(newAccountId,hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
9
10
//Submit the transaction to a Hedera network
11
txResponse, err := transaction.Execute(client)
12
13
if err != nil {
14
panic(err)
15
}
Copied!
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 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 transfer transaction. The receipt will let you know if the transaction was successful or not.
1
//Request the receipt of the transaction
2
transferReceipt, err := txResponse.GetReceipt(client)
3
4
if err != nil {
5
panic(err)
6
}
7
8
//Get the transaction consensus status
9
transactionStatus := transferReceipt.Status
10
11
fmt.Printf("The transaction consensus status is %v\n", transactionStatus)
Copied!

Step 3. Get the account balance

3.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.
1
//Create the query that you want to submit
2
balanceQuery := hedera.NewAccountBalanceQuery().
3
SetAccountID(newAccountId)
4
5
//Get the cost of the query
6
cost, err := balanceQuery.GetCost(client)
7
8
if err != nil {
9
panic(err)
10
}
11
12
println("The account balance query cost is:", cost.String())
Copied!

3.2 Get the account balance

You will verify the account balance was updated for the new account by submitting 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.
1
//Check the new account's balance
2
newAccountBalancequery := hedera.NewAccountBalanceQuery().
3
SetAccountID(newAccountId)
4
5
//Sign with client operator private key and submit the query to a Hedera network
6
newAccountBalance, err := newAccountBalancequery.Execute(client)
7
if err != nil {
8
panic(err)
9
}
10
11
//Print the balance of tinybars
12
fmt.Println("The hbar account balance for this account is", newAccountBalance.Hbars.AsTinybar())
Copied!
⭐ 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 environment to submit transactions and queries
    Created an account
    Transferred hbars to another account
Do you want to keep learning? Visit our "Resources" and "Documentation" sections to take your learning experience to the next level. You can also find additional Hedera Go SDK examples here.

Code Check ✅

Your complete code file should look something like this:
1
package main
2
3
import (
4
"fmt"
5
"os"
6
7
"github.com/hashgraph/hedera-sdk-go/v2"
8
"github.com/joho/godotenv"
9
)
10
11
func main() {
12
13
//Loads the .env file and throws an error if it cannot load the variables from that file correctly
14
err := godotenv.Load(".env")
15
if err != nil {
16
panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err))
17
}
18
19
//Grab your testnet account ID and private key from the .env file
20
myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
21
if err != nil {
22
panic(err)
23
}
24
25
myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
26
if err != nil {
27
panic(err)
28
}
29
30
//Print your testnet account ID and private key to the console to make sure there was no error
31
fmt.Printf("The account ID is = %v\n", myAccountId)
32
fmt.Printf("The private key is = %v\n", myPrivateKey)
33
34
//Create your testnet client
35
client := hedera.ClientForTestnet()
36
client.SetOperator(myAccountId, myPrivateKey)
37
38
//Generate new keys for the account you will create
39
newAccountPrivateKey, err := hedera.GeneratePrivateKey()
40
if err != nil {
41
panic(err)
42
}
43
44
newAccountPublicKey := newAccountPrivateKey.PublicKey()
45
46
//Create new account and assign the public key
47
newAccount, err := hedera.NewAccountCreateTransaction().
48
SetKey(newAccountPublicKey).
49
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
50
Execute(client)
51
52
//Request the receipt of the transaction
53
receipt, err := newAccount.GetReceipt(client)
54
if err != nil {
55
panic(err)
56
}
57
58
//Get the new account ID from the receipt
59
newAccountId := *receipt.AccountID
60
61
//Print the new account ID to the console
62
fmt.Printf("The new account ID is %v\n", newAccountId)
63
64
//Create the account balance query
65
query := hedera.NewAccountBalanceQuery().
66
SetAccountID(newAccountId)
67
68
//Sign with client operator private key and submit the query to a Hedera network
69
accountBalance, err := query.Execute(client)
70
if err != nil {
71
panic(err)
72
}
73
74
//Print the balance of tinybars
75
fmt.Println("The account balance for the new account is", accountBalance.Hbars.AsTinybar())
76
77
//Transfer hbar from your testnet account to the new account
78
transaction := hedera.NewTransferTransaction().
79
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
80
AddHbarTransfer(newAccountId, hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
81
82
//Submit the transaction to a Hedera network
83
txResponse, err := transaction.Execute(client)
84
85
if err != nil {
86
panic(err)
87
}
88
89
//Request the receipt of the transaction
90
transferReceipt, err := txResponse.GetReceipt(client)
91
92
if err != nil {
93
panic(err)
94
}
95
96
//Get the transaction consensus status
97
transactionStatus := transferReceipt.Status
98
99
fmt.Printf("The transaction consensus status is %v\n", transactionStatus)
100
101
//Create the query that you want to submit
102
balanceQuery := hedera.NewAccountBalanceQuery().
103
SetAccountID(newAccountId)
104
105
//Get the cost of the query
106
cost, err := balanceQuery.GetCost(client)
107
108
if err != nil {
109
panic(err)
110
}
111
112
fmt.Println("The account balance query cost is:", cost.String())
113
114
//Check the new account's balance
115
newAccountBalancequery := hedera.NewAccountBalanceQuery().
116
SetAccountID(newAccountId)
117
118
//Sign with client operator private key and submit the query to a Hedera network
119
newAccountBalance, err := newAccountBalancequery.Execute(client)
120
if err != nil {
121
panic(err)
122
}
123
124
//Print the balance of tinybars
125
fmt.Println("The hbar account balance for this account is", newAccountBalance.Hbars.AsTinybar())
126
}
Copied!
Last modified 3mo ago