This repository demonstrates how to perform HBAR allowances and atomic crypto transfers (HBAR, HTS fungible tokens, and HTS NFTs in the same transfer via a contract transaction). It uses a Hardhat project to test the flow of creating HTS tokens, approving allowances (HBAR & HTS), associating HTS tokens to Hedera accounts, and performing the atomic crypto transfer.
- Defines a new function to the Hedera Token Service system contract that allows for the atomic transfer of HBAR, fungible tokens and non-fungible tokens.
- Function
cryptoTransfer(TransferList transferList,TokenTransferList[] tokenTransfer)
- Exposes an existing HAPI call via smart contracts.
- Transfer honors granted allowances.
Benefits:
- Enables native royalty support on the EVM since native $hbar can now be transferred using spending allowances
- Direct interaction with HBAR and HTS tokens
- Eliminates the need for token wrapping.
- Enhances efficiency and reduces complexity.
- Cuts costs by removing intermediary steps i.e., wrapping assets to interact with them.
- Enables native royalty support on the EVM since native HBAR can now be transferred using spending allowances
- Introduces a new Hedera Account Service system contract.
- Enables querying and granting approval of HBAR to a spender account from smart contracts code
hbarAllowance
,hbarApprove
- Developers do not have to context switch out of smart contract code
Benefits:
- Introduces new account proxy contract for HBAR allowances
- Enables grant, retrieve, and manage HBAR allowances within smart contracts
- Developers do not have to context switch out of smart contracts code
- Simplifies workflows and enhances security
- Expands potential use cases, especially for DeFi and token marketplaces
- Node.js
- npm
- A Hedera account with ECDSA credentials
- Clone the repository:
git clone https://github.com/ed-marquez/hedera-example-hbar-allowances-and-atomic-transfers.git cd hedera-example-hbar-allowances-and-atomic-transfers
- Rename the
example.env
file to.env
and enter the ECDSA credentials for the 3 accounts needed:mv example.env .env
- Install dependencies:
npm install
- Run the test script::
npx hardhat test
.env
: Environment variables file (be sure to renameexample.env
to.env
and fill in your credentials).constants.js
: Contains constants and network configurations.hardhat.config.js
: Hardhat configuration file.atomicTransfer.test.js
: Contains the test script for performing the atomic transfers.
- Initialize Environment Variables:
- Rename
example.env
to.env
and fill in your ECDSA account credentials.
- Rename
- Use Environment Variables:
- The credentials from
.env
are used inconstants.js
.
- The credentials from
- Configure Hardhat:
- The constants from
constants.js
are used inhardhat.config.js
for network and account configuration.
- The constants from
- Run Tests:
- Tests in
atomicTransfer.test.js
create tokens, approve allowances, associate HTS tokens to Hedera accounts, and perform the atomic crypto transfer.
- Tests in