哈希图官方SDK支持Java、JS、Go语言进行开发,本文使用Go进行开发测试。
1.创建转移交易
从"创建帐户"部分创建的帐户中获取新的账户ID。您将从测试网帐户中将 1000 个HBAR转移到新帐户。在交易上签名需要发件人帐户的私人密钥。发件人帐户是您的测试网帐户,因此客户端已经设置为使用测试网帐户的私人密钥进行签名以授权转账。
//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)
}
转账的净值必须等于零(发件人发送的 hbar 总数必须等于收件人收到的 hbar 总数)。
2.验证转让交易达成共识
要验证网络达成的转让交易,您将提交获取转让交易收据的请求。收据将让您知道交易是否成功。
//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)
3.获取帐户余额
3.1请求查询费用
//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())
3.2获取账户余额
//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())
转移完成!