我已经使用JavaScript SDK在HTS上创建了一个新令牌:

// 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}`);
}

然后try 使用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}`);
}

但是,我得到以下错误:

ReceiptStatusError: receipt for transaction 0.0.565763@1692941232.156398491 contained error status TOKEN_NOT_ASSOCIATED_TO_ACCOUNT

为什么有必要在Hedera上"关联"一个令牌?

我如何才能做到这一点?

推荐答案

在Hedera上,HTS允许你创建行为类似于ERC20令牌的可替换令牌,但它们并不完全匹配.其中一个主要区别是,帐户需要显式地" Select 加入"特定令牌,才能与其交互.这被称为"令牌关联".

注意,如果帐户不再希望与该令牌交互,则该帐户随后可以将其自身与其先前与其相关联的令牌分离.

我如何才能做到这一点?

关联到令牌的第一步是构建一个TokenAssociateTransaction():

// Build a tokenAssociateTransaction to associate an account to a token 
// and freeze the unsigned transaction for signing
  const associateTransaction = await new TokenAssociateTransaction()
    .setAccountId(accountId)
    .setTokenIds([tokenId])
    .freezeWith(client);

一旦建立了交易,您将需要使用与令牌相关联的帐户的私钥来签署交易.

const associateTransactionSigned = await associateTransaction.sign(accountKey);

接下来,使用execute()方法将签名的事务提交到Hedera网络

const associateTransactionResponse = await associateTransactionSigned.execute(client);

最后,从事务响应中获取收据,并获取IF的状态

// Get the receipt of the transaction
  const receipt = await associateTransactionResponse.getReceipt(client);

  // Get the transaction consensus status
  const associateTransactionStatus = receipt.status;

要取消帐户与HTS令牌的关联,您需要构建一个TokenDissociateTransaction()

? The account is required to have a zero balance of the token you wish to dissociate. If a token balance is present, you will receive a `TRANSACTION_REQUIRES_ZERO_TOKEN_BALANCES` error.
async function dissociateAccountFromToken() {
  // Dissociate an account from a token and freeze the unsigned transaction for signing
  const dissociateTransaction = await new TokenDissociateTransaction()
    .setAccountId(accountId)
    .setTokenIds([tokenId])
    .freezeWith(client);

  // sign using the private key of the account that is dissociating from the token
  const dissociateTransactionSigned = await dissociateTransaction.sign(accountKey);

  // submit the transaction to a Hedera network
  const dissociateTransactionResponse = await dissociateTransactionSigned.execute(client);

  // Get the receipt of the transaction
  const receipt = await dissociateTransactionResponse.getReceipt(client);

  // Get the transaction consensus status
  const dissociateTransactionStatus = receipt.status;
}

Javascript相关问答推荐

有条件的悲剧

如何从一个列表中创建一个2列的表?

PrivateRoute不是路由组件错误

查找最长的子序列-无法重置数组

在react JS中映射数组对象的嵌套数据

不能将空字符串传递给cy.containes()

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

更新动态数据中对象或数组中的所有值字符串

NG/Express API路由处理程序停止工作

如何在DYGRAPS中更改鼠标事件和键盘输入

为什么可选参数的顺序会导致问题?

使用父标签中的Find函数查找元素

不同表的条件API端点Reaction-redux

JWT Cookie安全性

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

如何在Reaction中设置缺省值, Select 下拉列表,动态追加剩余值?

JSX/React -如何在组件props 中循环遍历数组

将Windows XP转换为原始数据以在html前端中显示

相对于具有选定类的不同SVG组放置自定义工具提示

如何从嵌套的json中获取最大值