In use cases like DeFi, contracts often need to mint, burn, or transfer 0 units of a token due to integer rounding. The implementation of HIP-564 in release 0.31 of Hedera Services enables smart contract developers to specify a zero value for contract functions performing token operations.
The benefit is that transactions with 0 tokens go through successfully without having to implement if statements or qualification logic to check for 0 values. This also facilitates migrating existing applications to Hedera even more as the behavior when passing 0 tokens to contract functions is the same as that of other chains. Previously, passing 0 units of a token to a contract function was not possible. Those transactions would fail while reporting an invalid token amount.
In this article, you will learn how to pass zero token values to smart contract operations on Hedera.
This example guides you through the following steps:
After completing all steps, your console should look something like this:
There are four entities in this scenario: Operator, Alice, a contract, and a fungible HTS token. Your testnet credentials from the Hedera portal should be used for the operator variables, which are used to initialize the Hedera client that submits transactions to the network and gets confirmations.
// STEP 1 =================================== console.log(`\nSTEP 1 ===================================\n`); console.log(`- Creating Hedera accounts, contract, and HTS token...\n`); // Accounts const initBalance = new Hbar(10); const aliceKey = PrivateKey.generateED25519(); const [aliceSt, aliceId] = await accountCreateFcn(aliceKey, initBalance, client); console.log(`- Alice's account: https://hashscan.io/#/testnet/account/${aliceId}`); // Contract let gasLim = 4000000; const bytecode = contract.object; const constructorParams = new ContractFunctionParameters().addAddress(operatorId.toSolidityAddress()).addAddress(aliceId.toSolidityAddress()); const [contractId, contractAddress] = await contracts.deployContractFcn(bytecode, constructorParams, gasLim, client); console.log(`\n- Contract ID: ${contractId}`); console.log(`- Contract ID in Solidity address format: ${contractAddress}`); // Token const tokenCreateRec = await contracts.executeContractPayableFcn(contractId, "createHtsToken", gasLim, client); console.log(`\n- Contract call for token creation: ${tokenCreateRec.receipt.status}`); const [tokenCreateInfo, tokenCreateExpUrl] = await queries.mirrorTxQueryFcn(tokenCreateRec.transactionId); console.log(`- Token ID: ${tokenCreateInfo.data.transactions[1].entity_id}`); console.log(`- See details: ${tokenCreateExpUrl}`);
// SPDX-License-Identifier: Apache-2.0 pragma solidity >=0.5.0 <0.9.0; pragma experimental ABIEncoderV2; import "../hts-precompiles/ExpiryHelper.sol"; // To alter the behavior of the SolidityPrecompileExample, re-compile this solidity file // (you will also need the other files in this directory) // and copy the outputted json file to ./PrecompileExample.json contract ZeroTokenOperations is ExpiryHelper { address payable owner; address payable aliceAccount; address fungibleToken; address nftToken; constructor(address payable _owner, address payable _aliceAccount) { owner = _owner; aliceAccount = _aliceAccount; } // In order for some functions (such as createFungibleToken) to work, the contract must possess the funds for // the function call. We are using ContractExecuteTransaction.setPayableAmount() to transfer some Hbar // to the contract's account at each step (which means this function must be payable), and then transferring // the excess Hbar back to the owner at the end of each step. function createHtsToken() external payable returns (int responseCode) { require(msg.sender == owner); IHederaTokenService.TokenKey[] memory keys = new IHederaTokenService.TokenKey[](1); // Set the admin key, supply key, pause key, and freeze key to the key of the account that executed function (INHERIT_ACCOUNT_KEY). keys[0] = createSingleKey( ADMIN_KEY_TYPE | SUPPLY_KEY_TYPE | PAUSE_KEY_TYPE | FREEZE_KEY_TYPE | WIPE_KEY_TYPE, INHERIT_ACCOUNT_KEY, bytes("") ); (responseCode, fungibleToken) = createFungibleToken( IHederaTokenService.HederaToken( "Example Fungible token", // name "E", // symbol address(this), // treasury "memo", true, // supply type, false -> INFINITE, true -> FINITE 1000, // max supply false, // freeze default (setting to false means that this token will not be initially frozen on creation) keys, // the keys for the new token // auto-renew fee paid by aliceAccount every 7,000,000 seconds (approx. 81 days). // This is the minimum auto renew period. createAutoRenewExpiry(aliceAccount, 7000000) ), 100, // initial supply 0 // decimals ); // send any excess Hbar back to the owner owner.transfer(address(this).balance); } function associateHtsToken() external returns (int responseCode) { require(msg.sender == owner); responseCode = associateToken(aliceAccount, fungibleToken); } function transferHtsToken() external returns (int responseCode) { require(msg.sender == owner); responseCode = transferToken( fungibleToken, address(this), // sender aliceAccount, // receiver 0 // amount to transfer ); } function mintHtsToken() external returns (int responseCode) { require(msg.sender == owner); uint64 newTotalSupply; int64[] memory mintedSerials; // applicable to NFT tokens only (responseCode, newTotalSupply, mintedSerials) = mintToken( fungibleToken, 0, // amount (applicable to fungible tokens only) new bytes[](0) // metadatas (applicable to NFT tokens only) ); require(newTotalSupply == 100 + 0); } function burnHtsToken() external returns (int responseCode) { require(msg.sender == owner); uint64 newTotalSupply; int64[] memory mintedSerials; // applicable to NFT tokens only (responseCode, newTotalSupply) = burnToken( fungibleToken, 0, // amount (applicable to fungible tokens only) mintedSerials // metadatas (applicable to NFT tokens only) ); require(newTotalSupply == 100 + 0); } function wipeHtsToken() external returns (int responseCode) { require(msg.sender == owner); responseCode = wipeTokenAccount( fungibleToken, aliceAccount, // owner of tokens to wipe from 0 // amount to transfer ); } }
Helper Functions
Using accountCreateFcn simplifies the account creation process and is reusable in case you need to create more accounts in the future. This function uses the AccountCreateTransaction() class of the SDK. We’ll use this modular approach throughout the article.
Keep in mind that on Hedera an account and a token must be associated with each other before that account can transact the token. Notice that during creation, the account is set to have 10 automatic token associations. This way, no manual association is needed for the first 10 tokens the account wishes to transact. Visit this documentation page to learn more about token associations.
The function contracts.deployContractFcn uses ContractCreateFlow() to store the bytecode and deploy the contract on Hedera. This single call handles for you the operations FileCreateTransaction(), FileAppendTransaction(), and ContractCreateTransaction().
The function contracts.executeContractPayableFcn uses ContractExecuteTransaction() in the SDK to call the specified contract function. Note that specifying .setPayableAmount() to 20 HBAR provides sufficient funds to the contract to complete some operations like creating a fungible HTS token.
Lastly, the helper function queries.mirrorTxQueryFcn gets transaction information for the mirror nodes. The function introduces a delay of 10 seconds to allow for the propagation of information to the mirror nodes. It then formats the transaction ID and performs string operations to return a mirror REST API query and a mirror node explorer URL.
async function accountCreateFcn(pvKey, iBal, client) { const response = await new AccountCreateTransaction() .setInitialBalance(iBal) .setKey(pvKey.publicKey) .setMaxAutomaticTokenAssociations(10) .execute(client); const receipt = await response.getReceipt(client); return [receipt.status, receipt.accountId]; }
export async function deployContractFcn(bytecode, params, gasLim, client) { const contractCreateTx = new ContractCreateFlow().setBytecode(bytecode).setGas(gasLim).setConstructorParameters(params); const contractCreateSubmit = await contractCreateTx.execute(client); const contractCreateRx = await contractCreateSubmit.getReceipt(client); const contractId = contractCreateRx.contractId; const contractAddress = contractId.toSolidityAddress(); return [contractId, contractAddress]; }
export async function executeContractPayableFcn(cId, fcnName, gasLim, client) { const contractExecuteTx = new ContractExecuteTransaction().setContractId(cId).setGas(gasLim).setFunction(fcnName).setPayableAmount(new Hbar(20)); const contractExecuteSubmit = await contractExecuteTx.execute(client); const contractExecuteRec = await contractExecuteSubmit.getRecord(client); return contractExecuteRec; }
export async function mirrorTxQueryFcn(txIdRaw) { // Query a mirror node for information about the transaction const delay = (ms) => new Promise((res) => setTimeout(res, ms)); await delay(10000); // Wait for 10 seconds before querying a mirror node const txIdPretty = prettify(txIdRaw.toString()); const mirrorNodeExplorerUrl = `https://hashscan.io/testnet/transaction/${txIdPretty}`; const mirrorNodeRestApi = `https://testnet.mirrornode.hedera.com/api/v1/transactions/${txIdPretty}`; const mQuery = await axios.get(mirrorNodeRestApi); return [mQuery, mirrorNodeExplorerUrl]; } function prettify(txIdRaw) { const a = txIdRaw.split("@"); const b = a[1].split("."); return `${a[0]}-${b[0]}-${b[1]}`; }
Console Output:
STEP 1 ===================================
- Creating Hedera accounts, contract, and HTS token...
- Alice's account: https://hashscan.io/#/testnet/account/0.0.48760462
- Contract ID: 0.0.48760464
- Contract ID in Solidity address format: 0000000000000000000000000000000002e80690
- Contract call for token creation: SUCCESS
- Token ID: 0.0.48760465
- See details: https://hashscan.io/testnet/transaction/0.0.2520793-1666975193-540118792
From the Solidity code in the previous step, the fungible token created has an initial supply of 100 units and the contract is the treasury for the token.
// STEP 2 =================================== console.log(`\nSTEP 2 ===================================\n`); console.log(`- Transferring zero tokens from contract to Alice...\n`); const transferFtRec = await contracts.executeContractFcn(contractId, "transferHtsToken", gasLim, client); console.log(`- Contract call for token transfer: ${transferFtRec.receipt.status} \n`); const [transferFtInfo, transferExpUrl] = await queries.mirrorTxQueryFcn(transferFtRec.transactionId); console.log(`- See details: ${transferExpUrl}`);
The function contracts.executeContractFcn also uses ContractExecuteTransaction() in the SDK to call the specified contract function. However, this helper function does not set a payable amount of HBAR.
export async function executeContractFcn(cId, fcnName, gasLim, client) { const contractExecuteTx = new ContractExecuteTransaction().setContractId(cId).setGas(gasLim).setFunction(fcnName); const contractExecuteSubmit = await contractExecuteTx.execute(client); const contractExecuteRec = await contractExecuteSubmit.getRecord(client); return contractExecuteRec; }
STEP 2 ===================================
- Transferring zero tokens from contract to Alice...
- Contract call for token transfer: SUCCESS
- See details: https://hashscan.io/testnet/transaction/0.0.2520793-1666975205-711024658
In this step:
// STEP 3 =================================== console.log(`\nSTEP 3 ===================================\n`); console.log(`- Minting (or burning) zero tokens...\n`); const mintFtRec = await contracts.executeContractFcn(contractId, "mintHtsToken", gasLim, client); console.log(`- Contract call for minting zero tokens: ${mintFtRec.receipt.status} \n`); const [mintFtInfo, mintExpUrl] = await queries.mirrorTxQueryFcn(mintFtRec.transactionId); console.log(`- See details: ${mintExpUrl}`); // const burnFtRec = await contracts.executeContractFcn(contractId, "burnHtsToken", gasLim, client); // console.log(`\n - Contract call for burning zero tokens: ${burnFtRec.receipt.status} \n`); // const [burnFtInfo, burnExpUrl] = await queries.mirrorTxQueryFcn(burnFtRec.transactionId); // console.log(`- See details: ${burnExpUrl}`);
STEP 3 ===================================
- Minting (or burning) zero tokens...
- Contract call for minting zero tokens: SUCCESS
- See details: https://hashscan.io/testnet/transaction/0.0.2520793-1666975218-508125238
Finally, in this step:
The last step is to join the Hedera Developer Discord!
// STEP 4 =================================== console.log(`\nSTEP 4 ===================================\n`); console.log(`- Wiping zero tokens...\n`); const wipeFtRec = await contracts.executeContractFcn(contractId, "wipeHtsToken", gasLim, client); console.log(`- Contract call for wiping zero tokens: ${wipeFtRec.receipt.status} \n`); const [wipeFtInfo, wipeExpUrl] = await queries.mirrorTxQueryFcn(wipeFtRec.transactionId); console.log(`- See details: ${wipeExpUrl}`); console.log(` ==================================================== THE END - NOW JOIN: https://hedera.com/discord ====================================================\n`);
STEP 4 ===================================
- Wiping zero tokens...
- Contract call for wiping zero tokens: SUCCESS
- See details: https://hashscan.io/testnet/transaction/0.0.2520793-1666975228-509195333
====================================================
🎉🎉 THE END - NOW JOIN: https://hedera.com/discord
Now you know how to perform contract actions with zero token units on Hedera using the JavaScript SDK and Solidity. You can try this example with the other officially supported SDKs for Java, Go, and Swift.
Keep in mind that zero token operations can also be performed with the SDK. For instance, a token transfer as follows completes successfully:
//Transfer token const tokenTransferTx = new TransferTransaction() .addTokenTransfer(tokenId, operatorId, 0) // deduct 0 tokens .addTokenTransfer(tokenId, aliceId, 0) // increase balance by 0 .freezeWith(client); const tokenTransferSign = await tokenTransferTx.sign(operatorKey); const tokenTransferSubmit = await tokenTransferSign.execute(client); //Verify the transaction reached consensus const tokenTransferRec = await tokenTransferSubmit.getRecord(client);