---
title: "How to Send and Receive HBAR Using Smart Contracts – Part 2: Using Solidity"
id: "15851"
type: "post"
slug: "how-to-send-and-receive-hbar-using-smart-contracts-part-2-solidity"
published_at: "2022-08-19T08:00:00+00:00"
modified_at: "2025-12-10T05:10:13+00:00"
url: "https://hedera.com/blog/how-to-send-and-receive-hbar-using-smart-contracts-part-2-solidity/"
markdown_url: "https://hedera.com/blog/how-to-send-and-receive-hbar-using-smart-contracts-part-2-solidity.md"
excerpt: "Learn how to send and receive HBAR to and from Hedera contracts. Part 1 of the series focused on using the Hedera SDKs. This second part goes over transferring HBAR to and from contracts using Solidity."
taxonomy_category:
  - "Uncategorized"
taxonomy_post_tag:
  - "technical"
---

[Skip to content](#content)
blog

# How to Send and Receive HBAR Using Smart Contracts – Part 2: Using Solidity

August 19, 2022

![Ed Marquez](https://hedera.com//wp-content/uploads/2025/12/Headshot.jpeg)

Ed Marquez

Head of Developer Relations

Smart contracts on Hedera can hold and exchange value in the form of HBAR, Hedera Token Service (HTS) tokens, and even ERC tokens. This is fundamental for building decentralized applications that rely on contracts in areas like DeFi, ESG, NFT marketplaces, DAOs, and more.

Let’s learn how to send and receive HBAR to and from Hedera contracts. [Part 1](https://hedera.com/blog/how-to-send-and-receive-hbar-using-smart-contracts-part-1-using-the-sdk)
 of the series focused on using the Hedera SDKs. This second part goes over transferring HBAR to and from contracts using Solidity.

Follow these main 3 steps:

1. Create the Hedera accounts needed for testing and deploy a smart contract on the Testnet
2. Move HBAR to the contract using ***fallback*** and ***receive*** functions, a ***payable*** function, and the **SDK**
3. Move HBAR from the contract to Alice using the ***transfer***, ***send***, and ***call*** methods

Throughout the tutorial, you also learn how to check the HBAR balance of the contract by calling a function of the contract itself and by using the SDK query. The last step is to review the transaction history for the contract and the operator account in a mirror node explorer, like [HashScan](https://hashscan.io/#/mainnet/dashboard)
.

#### **Try It Yourself**

- Get a [Hedera testnet account](https://portal.hedera.com/register)
- Use [this Codesandbox](https://codesandbox.io/s/hedera-example-transfer-hbar2contracts-solidity-swsfmn?file=/index.js) to try the example
  - Fork the sandbox
  - Remember to provide your testnet account credentials in the .env file
  - Open a new terminal to execute index.js

- Get the [example code from GitHub](https://github.com/ed-marquez/hedera-smart-contracts/tree/examples/examples/transfer-hbar2contracts-solidity)

#### **1. 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](https://portal.hedera.com/register)
) 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.

These helper functions in ***index.js*** use the **[AccountCreateTransaction()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/cryptocurrency/create-an-account)**  
 and **[ContractCreateFlow()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/create-a-smart-contract#contractcreateflow)**  
 classes of the Hedera SDK. **[ContractCreateFlow()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/create-a-smart-contract#contractcreateflow)**  
 stores the bytecode and deploys the contract on Hedera. This single call handles for you the operations **[FileCreateTransaction()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/file-storage/create-a-file)**, **[FileAppendTransaction()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/file-storage/append-to-a-file)**, and **[ContractCreateTransaction()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/create-a-smart-contract#contractcreatetransaction)**.

**Helper Functions:**

**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*

#### **2. Getting HBAR to the Contract**

#### **2.1 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](https://solidity-by-example.org/sending-ether/)
 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()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/call-a-smart-contract-function)**  
 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()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/call-a-smart-contract-function-1)**.

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	console.log(`
====================================================
GETTING HBAR TO THE CONTRACT
====================================================`);

	// Transfer HBAR to the contract using .setPayableAmount WITHOUT specifying a function (fallback/receive triggered)
	let payableAmt = 10;
	console.log(`- Caller (Operator) PAYS ${payableAmt} ℏ to contract (fallback/receive)...`);
	const toContractRx = await contractExecuteNoFcn(contractId, gasLimit, payableAmt);

	// Get contract HBAR balance by calling the getBalance function in the contract AND/OR using ContractInfoQuery in the SDK
	await contractCallQueryFcn(contractId, gasLimit, "getBalance"); // Outputs the contract balance in the console
```

**Helper Functions:**

**Console Output:**

*====================================================*

*GETTING HBAR TO THE CONTRACT*

*====================================================*

- *Caller (Operator) PAYS 10 ℏ to contract (fallback/receive)…*
- *Contract balance (getBalance fcn): 10 ℏ*

#### **2.2 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()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/smart-contracts/call-a-smart-contract-function)**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).

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	// Transfer HBAR to the contract using .setPayableAmount SPECIFYING a contract function (tokenAssociate)
	payableAmt = 21;
	gasLimit = 800000;
	console.log(`n- Caller (Operator) PAYS ${payableAmt} ℏ to contract (payable function)...`);
	const Params = await contractParamsBuilderFcn(contractId, [], 2, tokenId);
	const Rx = await contractExecuteFcn(contractId, gasLimit, "tokenAssociate", Params, payableAmt);

	gasLimit = 50000;
	await contractCallQueryFcn(contractId, gasLimit, "getBalance"); // Outputs the contract balance in the console
```

**Console Output:**

- *Caller (Operator) PAYS 21 ℏ to contract (payable function)…*
- *Contract balance (getBalance fcn): 31 ℏ*

#### **2.3 Using *TransferTransaction* in the SDK**

Lastly in this scenario, the Treasury transfers 30 HBAR to the contract using **[TransferTransaction()](https://docs.hedera.com/hedera/sdks-and-apis/sdks/cryptocurrency/transfer-cryptocurrency)
.**This is done with the helper function ***hbar2ContractSdkFcn***. This scenario is just a quick recap and reminder of [Part 1 of the series](https://hedera.com/blog/how-to-send-and-receive-hbar-using-smart-contracts-part-1-using-the-sdk)
, so be sure to give that a read for more details.

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	// Transfer HBAR from the Treasury to the contract deployed using the SDK
	let moveAmt = 30;
	const transferSdkRx = await hbar2ContractSdkFcn(treasuryId, contractId, moveAmt, treasuryKey);
	console.log(`n- ${moveAmt} ℏ from Treasury to contract (via SDK): ${transferSdkRx.status}`);

	await contractCallQueryFcn(contractId, gasLimit, "getBalance"); // Outputs the contract balance in the console
```

**Helper Functions:**

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
async function hbar2ContractSdkFcn(sender, receiver, amount, pKey) {
    const transferTx = new TransferTransaction()
        .addHbarTransfer(sender, -amount)
        .addHbarTransfer(receiver, amount)
        .freezeWith(client);
    const transferSign = await transferTx.sign(pKey);
    const transferSubmit = await transferSign.execute(client);
    const transferRx = await transferSubmit.getReceipt(client);
    return transferRx;
}
```

**Console Output:**

- *30 ℏ from Treasury to contract (via SDK): SUCCESS*
- *Contract balance (getBalance fcn): 61 ℏ*

#### **3. 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](https://solidity-by-example.org/sending-ether/)
 and [this external article](https://medium.com/daox/three-methods-to-transfer-funds-in-ethereum-by-means-of-solidity-5719944ed6e9)
 – 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.

#### **3.1****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.

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	console.log(`
====================================================
GETTING HBAR FROM THE CONTRACT
====================================================`);

	payableAmt = 0;
	moveAmt = 20;

	console.log(`- Contract TRANSFERS ${moveAmt} ℏ to Alice...`);
	const tParams = await contractParamsBuilderFcn(aliceId, moveAmt, 3, []);
	const tRx = await contractExecuteFcn(contractId, gasLimit, "transferHbar", tParams, payableAmt);

	// Get contract HBAR balance by calling the getBalance function in the contract AND/OR using ContractInfoQuery in the SDK
	await showContractBalanceFcn(contractId); // Outputs the contract balance in the console
```

**Helper Functions:**

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
async function showContractBalanceFcn(cId) {
    const info = await new ContractInfoQuery().setContractId(cId).execute(client);
    console.log(`- Contract balance (ContractInfoQuery SDK): ${info.balance.toString()}`);
}
```

**Console Output:**

*====================================================*

*GETTING HBAR FROM THE CONTRACT*

*====================================================*

- *Contract TRANSFERS 20 ℏ to Alice…*
- *Contract balance (ContractInfoQuery SDK): 41 ℏ*

#### **3.2****Contract Sends HBAR to Alice**

The same helper function from before now executes the ***sendHbar*** function of the contract.

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	console.log(`n- Contract SENDS ${moveAmt} ℏ to Alice...`);
	const sParams = await contractParamsBuilderFcn(aliceId, moveAmt, 3, []);
	const sRx = await contractExecuteFcn(contractId, gasLimit, "sendHbar", sParams, payableAmt);

	await showContractBalanceFcn(contractId); // Outputs the contract balance in the console
```

**Console Output:**

- *Contract SENDS 20 ℏ to Alice…*
- *Contract balance (ContractInfoQuery SDK): 21 ℏ*

#### ****3.3. 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](https://hashscan.io/#/mainnet/dashboard)
. You can also obtain additional information of interest using the [mirror node REST API](https://docs.hedera.com/hedera/sdks-and-apis/rest-api)
. Additional context for that API is provided in [this blog post](https://hedera.com/blog/how-to-look-up-transaction-history-on-hedera-using-mirror-nodes-back-to-the-basics)
.

The last step is to **[join the Hedera Developer Discord!](https://hedera.com/discord)**

![code window background](https://hedera.com/wp-content/uploads/2025/12/CodeSnippetBackground-scaled.jpg)

```
	console.log(`n- Contract CALLS ${moveAmt} ℏ to Alice...`);
	const cParams = await contractParamsBuilderFcn(aliceId, moveAmt, 3, []);
	const cRx = await contractExecuteFcn(contractId, gasLimit, "callHbar", cParams, payableAmt);

	await showContractBalanceFcn(contractId); // Outputs the contract balance in the console

	console.log(`n- SEE THE TRANSACTION HISTORY IN HASHSCAN (FOR CONTRACT AND OPERATOR): 
https://hashscan.io/#/testnet/contract/${contractId}
https://hashscan.io/#/testnet/account/${operatorId}`);

	console.log(`
====================================================
 THE END - NOW JOIN: https://hedera.com/discord
====================================================n`);
}
```

**Console Output:**

- *Contract CALLS 20 ℏ to Alice…*
- *Contract balance (ContractInfoQuery SDK): 1 ℏ*
- *SEE THE TRANSACTION HISTORY IN HASHSCAN (FOR CONTRACT AND OPERATOR):*

[https://hashscan.io/#/testnet/contract/0.0.47938605](https://hashscan.io/#/testnet/contract/0.0.47938605)

[https://hashscan.io/#/testnet/account/0.0.2520793](https://hashscan.io/#/testnet/account/0.0.2520793)

*====================================================*

*THE END – NOW JOIN: [https://hedera.com/discord](https://hedera.com/discord)*

*====================================================*

#### **Summary**

If you run the entire example successfully, your console should look something like:

![Image](https://hedera.com/wp-content/uploads/2025/12/2022-How-to-Send-and-Receive-HBAR-Using-Smart-Contracts-2-Image-1_2022-08-19-160324_onge.png)

Now you know how to send HBAR to and from a contract on Hedera using both the SDK and Solidity!

This tutorial used the Hedera JavaScript SDK. However, you can try this with the other officially supported SDKs for Java and Go.

#### **Continue Learning**

- [Open a Testnet Account](https://portal.hedera.com/register)
- [Try Examples](https://docs.hedera.com/hedera/tutorials) and [Tutorials](https://docs.hedera.com/hedera/tutorials)
- [Join the Developer Discord](http://hedera.com/discord)
- [Read the Learning Center](https://hedera.com/learning)

[Back to Blog](/blog)

discover

See more articles

[View All](/blog)

![Image](https://hedera.com/wp-content/uploads/2026/08/Regulation_Blog-1024x576.png)

August 5, 2026

### Details decide. What digital asset policy did in July 2026

Halfway through 2026, the regulatory picture is coming into focus. Hedera Chief Policy Officer Nilmini Rubin and VP Global Policy Isadora Arredondo break down what moved in June across the

[Read More](https://hedera.com/blog/details-decide-what-digital-asset-policy-did-in-july-2026/)

![Deploy Multichain Dapps](https://hedera.com/wp-content/uploads/2026/07/HH600301_DeployMultichainDapps_Final-1024x576.png)

July 17, 2026

### Deploy Multichain Dapps on Hedera in 60 Seconds with scaffold-hbar

Deploying multichain dapps on Hedera has never been easier! With just one command, you can spin up a fully functional dapp using Next.js, Hardhat or Foundry, and AI agent context…

[Read More](https://hedera.com/blog/deploy-multichain-dapps-on-hedera-in-60-seconds-with-scaffold-hbar/)

![Regulation is finding its form](https://hedera.com/wp-content/uploads/2026/07/Regulation_Blog-1-1024x576.png)

July 13, 2026

### Regulation is finding its form in summer 2026

Halfway through 2026, the regulatory picture is coming into focus. Hedera Chief Policy Officer Nilmini Rubin and VP Global Policy Isadora Arredondo break down what moved in June across the

[Read More](https://hedera.com/blog/regulation-is-finding-its-form-in-summer-2026/)

## Ready to get started?

Discover why Hedera is the trusted institutional-grade network powering the new digital economy.

[Start Building](/start-building)

[Contact](/contact)

We use cookies to deliver the best experience on our website and to analyze traffic. By continuing to use this site, you consent to our cookie policy.

Review our [Privacy Policy](/privacy)
 to understand how Hedera collects and uses information.

Accept All CookiesAccept Necessary Cookies
