- Create the Hedera accounts needed for testing and deploy a smart contract on the Testnet
- Move HBAR to the contract using fallback and receive functions, a payable function, and the SDK
- Move HBAR from the contract to Alice using the transfer, send, and call methods
Prerequisites
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.- Get a Hedera testnet account.
- Set up your environment here.
Table of Contents
- Create Accounts and Deploy a Contract
- Get HBAR to ➡ Contract
- Get HBAR from ⬅ Contract
- Summary
- Additional Resources
Create Accounts and Deploy a Contract
This example involves 3 Hedera accounts, 1 contract, and 1 Hedera Token Service (HTS) token. The Operator account (your Testnet account credentials) is used to build the Hedera client to submit transactions to the Hedera network – that’s the first account. The Treasury and Alice are new accounts (created by the Operator) to represent additional parties in your test – those are the second and third accounts respectively. A portion of the application file (index.js) and the entire Solidity contract (hbarToAndFromContract.sol) are shown in the tabs below. The Solidity file has functions for getting HBAR to the contract (receive, fallback, tokenAssociate), getting HBAR from the contract (transferHbar, sendHbar, callHbar), and checking the HBAR balance of the contract (getBalance). This portion of index.js configures and creates the accounts, deploys the contract, and stores the HTS token ID. The functions accountCreatorFcn and contractDeployFcn create new accounts and deploy the contract to the network, respectively. These functions simplify the account creation and contract deployment process and are reusable in case you need them in the future. This modular approach is used throughout the tutorial.Console Output ✅
Console Output ✅
- Creating accounts… * Created Treasury account 0.0.47938602 that has a balance of 100 ℏ * Created Alice’s account 0.0.47938603 that has a balance of 100 ℏ * Deploying contract… * The smart contract ID is: 0.0.47938605
- The smart contract ID in Solidity format is: 0000000000000000000000000000000002db7c2d * Token ID (for association with contract later): 0.0.47931765
Getting HBAR to the Contract
The receive/fallback Functions
In this scenario, you (Operator) transfer 10 HBAR to the contract by triggering either the receive or fallback functions of the contract. As described in this Solidity by Example page, the receive function is called when msg.data is empty, otherwise the fallback function is called. In this case, the helper function contractExecuteNoFcn pays HBAR to the contract by using ContractExecuteTransaction() and specifying a .setPayableAmount() without calling any specific contract function – thus triggering fallback. Note from the Solidity code that receive and fallback are external and payable functions. The helper function contractCallQueryFcn checks the HBAR balance of the contract by calling the getBalance function of the contract – this call is done using ContractCallQuery().Console Output ✅
Console Output ✅
====================================================GETTING HBAR TO THE CONTRACT====================================================
- Caller (Operator) PAYS 10 ℏ to contract (fallback/receive)…
- Contract balance (getBalance fcn): 10 ℏ
Executing a Payable Function
Now, you (Operator) transfer 21 HBAR to the contract by calling a specific contract function (tokenAssociate) that is payable using the ContractExecuteTransaction() class and specifying a .setPayableAmount(). This is done with the helper function contractExecuteFcn. In this scenario, contractParamsBuilderFcn is used to build the parameters that will be passed to the contract function – that is, the contract and token IDs which are then converted to Solidity addresses. From the Solidity code, note that the tokenAssociate function associates the contract to the HTS token from the first step, and requires more than 20 HBAR to execute (just for fun).Console Output ✅
Console Output ✅
- Caller (Operator) PAYS 21 ℏ to contract (payable function)… * Contract balance (getBalance fcn): 31 ℏ
Using TransferTransaction in the SDK
Lastly in this scenario, the Treasury transfers 30 HBAR to the contract using TransferTransaction(). This is done with the helper function hbar2ContractSdkFcn. This scenario is just a quick recap and reminder of Part 1 of the series, so be sure to give that a read for more details.
Console Output ✅
Console Output ✅
- 30 ℏ from Treasury to contract (via SDK): SUCCESS * Contract balance (getBalance fcn): 61 ℏ
Getting HBAR from the Contract
In this section the contract transfers HBAR to Alice using three different methods: transfer, send, call. Each transfer is of 20 HBAR, so by the end the contract should have 1 HBAR left in its balance. This tutorial focuses on implementation. For additional background and details of these Solidity methods, check out Solidity by Example and this external article – just remember that on Hedera, the native cryptocurrency transacted is HBAR, not ETH. One thing worth noting from those resources is that call is currently the recommended method to use.Contract Transfers HBAR to Alice
The helper function contractExecuteFcn executes the transferHbar function of the contract. The helper function contractParamsBuilderFcn now builds the contract function parameters from the receiver ID (Alice’s) and the amount of HBAR to be sent. Also note from the previous section that the contract function is executed with a gasLimit of only 50,000 gas.Console Output ✅
Console Output ✅
====================================================GETTING HBAR FROM THE CONTRACT====================================================
- Contract TRANSFERS 20 ℏ to Alice…
- Contract balance (ContractInfoQuery SDK): 41 ℏ
Contract Sends HBAR to Alice
The same helper function from before now executes the sendHbar function of the contract.Console Output ✅
Console Output ✅
- Contract SENDS 20 ℏ to Alice… * Contract balance (ContractInfoQuery SDK): 21 ℏ
Contract Calls HBAR to Alice
Just like above, the helper function contractExecuteFcn executes the sendHbar function of the contract. Examine the transaction history for the contract and the operator in the mirror node explorer, HashScan. You can also obtain additional information of interest using the mirror node REST API. Additional context for that API is provided in this blog post. The last step is to join the Hedera Developer Discord!Console Output ✅
Console Output ✅
- Contract CALLS 20 ℏ to Alice…
- Contract balance (ContractInfoQuery SDK): 1 ℏ
- SEE THE TRANSACTION HISTORY IN HASHSCAN (FOR CONTRACT AND OPERATOR):
Summary
If you run the entire example successfully, your console should look something like: