Deploy a Contract Using the Hedera Token Service
In this example, you will learn how to create a Solidity contract that interacts with the Hedera Token Service (HTS). The initial release of this feature supports token mint, burn, associate, dissociate, and transfer transactions.
The example does not cover the environment setup or creating certain variables that may be seen in the code blocks. The full coding example can be found at the end of the page.
Smart contract entity auto renewal and expiry will be enabled in a future release. Please check out HIP-16 for more information.
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.
In this example, you will associate a token to an account and transfer tokens to the associated account by interacting with the HTS contract deployed to Hedera. The HTS contract has three functions that allow you to associate, transfer, and dissociate tokens from a Hedera account.
tokenAssociate
tokenTransfer
tokenDissociate
The HTS.sol will serve as a reference to the contract that was compiled. The HTS.json file contains the
data.bytecode.object
field that will be used to store the contract bytecode in a file on the Hedera network.To write a contract using HTS, you will need to add the HTS Solidity support libraries to your project and import them into your contract. Please see the HTS.sol example for reference. The IHederaTokenService.sol will need to be in the same directory as the other two files. An explanation of the functions can be found here.
HTS.sol
HTS.json
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.6.12;
import "./HederaTokenService.sol";
import "./HederaResponseCodes.sol";
contract HTS is HederaTokenService {
function tokenAssociate(address sender, address tokenAddress) external {
int response = HederaTokenService.associateToken(sender, tokenAddress);
if (response != HederaResponseCodes.SUCCESS) {
revert ("Associate Failed");
}
}