Before I can receive an HTS token I need to first associate with the token ID.
If I were using the JS SDK I know to do the following where the accountId
is the account that I want to associate with the token.
const associateTransaction = await new TokenAssociateTransaction()
.setAccountId(accountId)
.setTokenIds([tokenId])
.freezeWith(client);
However, how do you Associate/Dissociate an HTS token using an EVM transaction?
I’ve attempted to do the following in my smart contract
function mintNft(
address token,
bytes[] memory metadata
) public payable returns(int64){
(int response, , int64[] memory serial) = HederaTokenService.mintToken(token, 0, metadata);
if(response != HederaResponseCodes.SUCCESS){
revert("Failed to mint non-fungible token");
}
int res = HederaTokenService.associateToken(
address(msg.sender),
token
);
if(res != HederaResponseCodes.SUCCESS){
revert("Failed to associate non-fungible token");
}
return serial[0];
}
However, when I try to transfer the contract reverts with the following error:
TOKEN_NOT_ASSOCIATED_TO_ACCOUNT
I’d like to be able to do so within a smart contract deployed to HSCS, preferably. If that is not possible, is there another way to do so?