Overview
On Ethereum, sending ETH to a contract address automatically triggers thereceive() or fallback() functions, allowing contracts to process incoming funds. On Hedera, these functions also exist but require HBAR to be explicitly sent via contractCall for them to execute. Direct HBAR transfers to a contract’s Hedera account won’t trigger any logic unless additional steps are taken.
The core Solidity patterns, transfer(), send(), and call(), work on Hedera as they do on Ethereum, with one difference: amounts are in tinybars (8 decimals), not wei (18 decimals). This guide highlights these mechanisms, details the key Hedera-specific considerations, and provides examples to help you handle HBAR transfers in your smart contracts
Sending to Contract
In Solidity, there are three ways to transfer value to and from contracts:transfer(): Sends a fixed amount of gas and reverts on failure.send(): Sends a fixed amount of gas and returnsfalseon failure instead of reverting.call(): A low-level function for sending value that allows specifying gas and includes additional data payloads.
Key Considerations
- Fallback and Receive Functions: When sending HBAR to a contract address via
contractCall, Hedera behaves like Ethereum. Ifreceive()orfallback()functions are defined in the contract, they will be triggered upon receipt of HBAR. - Important Note: Directly transferring HBAR to a contract’s Hedera account (not via
contractCall) will not trigger these functions. To execute logic upon receipt, ensure transfers occur within the EVM environment. - Amounts Are in Tinybars: Inside a contract,
msg.value,address(this).balance, and the amount passed totransfer(),send(), orcall{value: ...}are in tinybars (1 HBAR = 100,000,000 tinybars). An Ethereum literal such as1 ether(10¹⁸) is 10¹⁰ HBAR on Hedera. See Decimal Handling.
Example Contract Functions for HBAR Transfers
Below is an example of contract functions and how HBAR transfers are handled using Solidity. These patterns are the same as those used for ETH on the EVM, with amounts in tinybars:Suggested TutorialFor developers newer to Solidity, we recommend exploring Solidity courses to gain a deeper understanding of handling value transfers. A detailed tutorial on sending and receiving HBAR using Solidity smart contracts on Hedera can be found here.