mcpField
H
HTS System Contract Skill

HTS System Contract Skill

github.com/hedera-dev开发2891 次安装8 周前更新
代码仓库

关于 HTS System Contract Skill

HTS System Contract Skill 为在 Solidity 合约中使用 Hedera Token Service 的开发者提供参考,涵盖代币操作、合规控制、费用、响应码及常见实现模式。

主要功能

  1. 01`0x167` HTS 预编译调用
  2. 02同质化代币与 NFT 模式
  3. 03代币密钥构建指南
  4. 04KYC、冻结与暂停控制
  5. 05自定义费用参考
  6. 06响应码与错误处理

适用场景

  1. 01在 Solidity 中创建 Hedera 原生代币
  2. 02为代币合约添加合规控制
  3. 03处理代币费用与响应码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/hedera-dev/hedera-skills --skill hts-system-contract-skill

安装后在 Claude Code 中输入/hts-system-contract-skill确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
response-codes.md
compliance.md
api.md
keys.md
fees.md
troubleshooting.md
structs.md

详细介绍

Hedera Token Service(HTS)系统合约

0x167 处的 HTS 预编译合约支持 Solidity 合约创建和管理 Hedera 原生代币,并内置合规控制(KYC、冻结、暂停)和自定义费用。

快速参考

导入:

import {HederaTokenService} from "@hashgraph/smart-contracts/contracts/system-contracts/hedera-token-service/HederaTokenService.sol";
import {KeyHelper} from "@hashgraph/smart-contracts/contracts/system-contracts/hedera-token-service/KeyHelper.sol";
import {ExpiryHelper} from "@hashgraph/smart-contracts/contracts/system-contracts/hedera-token-service/ExpiryHelper.sol";
import {HederaResponseCodes} from "@hashgraph/smart-contracts/contracts/system-contracts/hedera-token-service/HederaResponseCodes.sol";

安全变体(失败时自动回滚):SafeHTS.sol、SafeViewHTS.sol

重要:需要支付 HBAR

创建代币需要显式支付 HBAR(不仅仅是 Gas):

// ❌ WRONG - fails with INSUFFICIENT_TX_FEE
(int rc, address token) = createNonFungibleToken(token);

// ✅ CORRECT
(int rc, address token) = createNonFungibleToken{value: msg.value}(token);

从 TypeScript 调用:

await contract.createToken(name, symbol, {
  gasLimit: 350_000,
  value: ethers.parseEther("15"), // ~$1-2 USD of HBAR
});

代币密钥系统

七种密钥类型用于控制代币操作(keyType 字段的位位置):

密钥位值控制操作
ADMIN01更新代币、密钥和删除代币
KYC12授予/撤销 KYC
FREEZE24冻结/解冻账户
WIPE38清除余额
SUPPLY416铸造/销毁
FEE532更新费用
PAUSE664暂停所有操作

使用 KeyHelper 构造密钥:

keys[0] = getSingleKey(KeyType.SUPPLY, KeyValueType.CONTRACT_ID, address(this));

有关密钥值类型和 JSON 元组格式,请参阅 references/keys.md。

关联模型

账户必须先与代币关联,才能接收代币:

int rc = associateToken(accountAddress, tokenAddress);
require(
    rc == HederaResponseCodes.SUCCESS ||
    rc == HederaResponseCodes.TOKEN_ALREADY_ASSOCIATED_TO_ACCOUNT,
    "Association failed"
);

常见模式

创建可替代代币
function createToken() external payable {
    IHederaTokenService.HederaToken memory token;
    token.name = "My Token";
    token.symbol = "MTK";
    token.treasury = address(this);
    token.expiry = createAutoRenewExpiry(address(this), 7776000); // 90 days

    (int rc, address created) = createFungibleToken{value: msg.value}(
        token, 1000000, 18  // initialSupply, decimals
    );
    require(rc == HederaResponseCodes.SUCCESS, "Create failed");
}
可铸造的 NFT 系列
function createNFT() external payable {
    IHederaTokenService.HederaToken memory token;
    token.name = "My NFT";
    token.symbol = "MNFT";
    token.treasury = address(this);
    token.tokenSupplyType = true;  // FINITE
    token.maxSupply = 10000;

    IHederaTokenService.TokenKey[] memory keys = new IHederaTokenService.TokenKey[](1);
    keys[0] = getSingleKey(KeyType.SUPPLY, KeyValueType.CONTRACT_ID, address(this));
    token.tokenKeys = keys;
    token.expiry = createAutoRenewExpiry(address(this), 7776000);

    (int rc, address created) = createNonFungibleToken{value: msg.value}(token);
    require(rc == HederaResponseCodes.SUCCESS, "Create failed");
}

function mintNFT(bytes memory metadata) external {
    bytes[] memory metas = new bytes[](1);
    metas[0] = metadata;
    (int rc, , int64[] memory serials) = mintToken(tokenAddress, 0, metas);
    require(rc == HederaResponseCodes.SUCCESS, "Mint failed");
}
启用 KYC 的代币

创建后,财库账户必须自行授予 KYC:

// After token creation with KYC key
int kycRc = grantTokenKyc(tokenAddress, address(this));
require(kycRc == HederaResponseCodes.SUCCESS, "Self-KYC failed");

响应代码处理

始终检查响应代码。SUCCESS = 22。

require(responseCode == HederaResponseCodes.SUCCESS, "Operation failed");

常见代码:请参阅 references/response-codes.md

更多参考资料

评论

支持 Markdown