In this article, you will learn how to use a keyless Solidity smart contract to manage your Hedera tokens. Specifically, you will create a token that has a contract as the treasury account and auto-renew account.
Having a keyless contract ensures a transparent and trustless workflow, where the contract itself is responsible for managing various aspects of the token.
You can get your contract bytecode directly from this Codesandbox or by compiling your contract on Remix IDE or using solc.
Now you can create the contract that will be the treasury of your fungible token using the ContractCreateFlow() method and specifying your adminKey. For now, you need to switch the operator using client.setOperator() to your admin account as you need to sign the transaction using the adminKey.
const bytecode = fs.readFileSync("./TreasuryContract_sol_TreasuryContract.bin");
// Switch operator to admin to sign transactions
client.setOperator(adminId, adminKey);
// Create contract with adminKey
const contractCreate = new ContractCreateFlow()
.setGas(100000)
.setBytecode(bytecode)
.setAdminKey(adminKey);
const contractCreateTx = await contractCreate.execute(client);
const contractCreateRx = await contractCreateTx.getReceipt(client);
const contractId = contractCreateRx.contractId;
console.log("Contract created with ID: " + contractId);
Console Output:
It’s time to create your fungible token, if you are not familiar with token creation you can check out this article.
// Create fungible token using contract as treasury
const tokenCreate = new TokenCreateTransaction()
.setTokenName("USD Bar")
.setTokenSymbol("USDB")
.setTreasuryAccountId(AccountId.fromString(contractId))
.setInitialSupply(10000)
.setDecimals(2)
.setAutoRenewAccountId(contractId)
.setAutoRenewPeriod(7000000)
.setMaxTransactionFee(new Hbar(30))
.freezeWith(client);
const signedTokenCreate = await tokenCreate.sign(adminKey);
const tokenCreateTx = await signedTokenCreate.execute(client);
const tokenCreateRx = await tokenCreateTx.getReceipt(client);
const tokenId = tokenCreateRx.tokenId;
console.log("Token created with ID: " + tokenId);
Console Output:
After creating your token, you need to remove the admin key from your contract. To proceed you need to execute a ContractUpdateTransaction() and set as new admin key an empty KeyList().
Done! Now your keyless contract is successfully managing the HTS token.
Setup a Contract as Treasury Using Solidity
Setting the contract as token treasury is straightforward when creating a token using a smart contract function.
Let’s start by creating a contract with a createFungible method responsible for token creation. Notice how inside this function, the token treasury and token auto-renew account is the contract itself ( address(this) ).
Make sure to include these files in your working directory by downloading them from the contracts folder here:
And to create your token with your contract as treasury you just need to execute your contract function.
// Create FT using precompile function
const createToken = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(300000) // Increase if revert
.setPayableAmount(20) // Increase if revert
.setFunction("createFungible",
new ContractFunctionParameters()
.addString("USD Bar") // FT name
.addString("USDB") // FT symbol
.addUint256(10000) // FT initial supply
.addUint256(2) // FT decimals
.addUint32(7000000)); // auto renew period
const createTokenTx = await createToken.execute(client);
const createTokenRx = await createTokenTx.getRecord(client);
const tokenIdSolidityAddr = createTokenRx.contractFunctionResult.getAddress(0);
const tokenId = AccountId.fromSolidityAddress(tokenIdSolidityAddr);
console.log("Token created with ID: " + tokenId);
Console Output:
Done! You just learned a completely trustless way to manage Hedera tokens using a smart contract.
For further explanation on this article, please contact us on Hedera Discord Server.