I have created a new token on HTS using the JavaScript SDK:
// Create an HTS Fungible Token
async function createFungibleToken(
client,
treasuryAccountId,
treasuryAccountPrivateKey,
) {
// Generate supply key
const supplyKeyForFT = PrivateKey.generateED25519();
// Confgiure the token
const createFTokenTxn = await new TokenCreateTransaction()
.setTokenName('PowerToken')
.setTokenSymbol('PT')
.setTokenType(TokenType.FungibleCommon)
.setDecimals(1)
.setInitialSupply(100)
.setTreasuryAccountId(treasuryAccountId)
.setSupplyKey(supplyKeyForFT)
.setMaxTransactionFee(new Hbar(30))
.freezeWith(client);
// Sign the transaction with the treasury account private key
const createFTokenTxnSigned = await
createFTokenTxn.sign(treasuryAccountPrivateKey);
const createFTokenTxnResponse = await
createFTokenTxnSigned.execute(client);
const createFtTokenTxnReceipt = await createFTokenTxnResponse.getReceipt(client);
const fungibleTokenId = createFtTokenTxnReceipt.tokenId;
console.log(`Fungible token ID: ${fungibleTokenId}`);
}
And then attempted to send it to an account using a TransferTransaction():
async function transferHtsToken(tokenId, senderAccountId, receiverAccoundId, myPrivateKey) {
const transferTransaction = new TransferTransaction()
.addTokenTransfer(tokenId, senderAccountId, -1)
.addTokenTransfer(tokenId, receiverAccoundId, 1)
.freezeWith(client);
const signTx = await transferTransaction.sign(myPrivateKey);
const txResponse = await signTx.execute(client);
const receipt = await txResponse.getReceipt(client);
const txStatus = receipt.status;
console.log(`Transaction status ${txStatus}`);
}
However, I get the following error:
ReceiptStatusError: receipt for transaction 0.0.565763@1692941232.156398491 contained error status TOKEN_NOT_ASSOCIATED_TO_ACCOUNT
Why is it necessary to “associate” a token on Hedera?
And how can I do so?