扩展功能
什么是代币扩展功能?
Token Extensions Program(Token 2022)通过称为扩展的额外指令提供更多功能。扩展是您可以为代币 mint 或代币账户添加的可选功能。您可以在 Token Extensions Program 的 源代码中找到这些扩展指令的实现。
每个扩展都会添加特定的状态,通常在创建 mint 或代币账户时初始化。在初始化任一账户时,您可以同时启用特定扩展以实现不同的功能。大多数扩展在账户初始化后无法添加。这是设计代币时需要考虑的重要因素,因为您需要提前规划希望代币支持哪些功能。每个扩展的集成指南可在 Token Extensions 开发者文档中找到。
某些扩展彼此不兼容,您无法在同一个代币 mint
或代币账户上同时启用它们。例如,您无法将 NonTransferable
扩展与
TransferFeeConfig
扩展结合使用,因为它们的行为存在冲突。
Token Extensions Program 定义了一个
ExtensionType
enum,列出了您可以为代币 mint 或代币账户添加的所有可用扩展。每个变体代表一个具有独特功能的不同扩展。
ExtensionType
enum 定义如下:
Token Extensions
/// Extensions that can be applied to mints or accounts. Mint extensions must/// only be applied to mint accounts, and account extensions must only be/// applied to token holding accounts.#[repr(u16)]#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]#[cfg_attr(feature = "serde-traits", serde(rename_all = "camelCase"))]#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive, IntoPrimitive)]pub enum ExtensionType {/// Used as padding if the account size would otherwise be 355, same as a/// multisigUninitialized,/// Includes transfer fee rate info and accompanying authorities to withdraw/// and set the feeTransferFeeConfig,/// Includes withheld transfer feesTransferFeeAmount,/// Includes an optional mint close authorityMintCloseAuthority,/// Auditor configuration for confidential transfersConfidentialTransferMint,/// State for confidential transfersConfidentialTransferAccount,/// Specifies the default Account::state for new AccountsDefaultAccountState,/// Indicates that the Account owner authority cannot be changedImmutableOwner,/// Require inbound transfers to have memoMemoTransfer,/// Indicates that the tokens from this mint can't be transferredNonTransferable,/// Tokens accrue interest over time,InterestBearingConfig,/// Locks privileged token operations from happening via CPICpiGuard,/// Includes an optional permanent delegatePermanentDelegate,/// Indicates that the tokens in this account belong to a non-transferable/// mintNonTransferableAccount,/// Mint requires a CPI to a program implementing the "transfer hook"/// interfaceTransferHook,/// Indicates that the tokens in this account belong to a mint with a/// transfer hookTransferHookAccount,/// Includes encrypted withheld fees and the encryption public that they are/// encrypted underConfidentialTransferFeeConfig,/// Includes confidential withheld transfer feesConfidentialTransferFeeAmount,/// Mint contains a pointer to another account (or the same account) that/// holds metadataMetadataPointer,/// Mint contains token-metadataTokenMetadata,/// Mint contains a pointer to another account (or the same account) that/// holds group configurationsGroupPointer,/// Mint contains token group configurationsTokenGroup,/// Mint contains a pointer to another account (or the same account) that/// holds group member configurationsGroupMemberPointer,/// Mint contains token group member configurationsTokenGroupMember,/// Mint allowing the minting and burning of confidential tokensConfidentialMintBurn,/// Tokens whose UI amount is scaled by a given amountScaledUiAmount,/// Tokens where minting / burning / transferring can be pausedPausable,/// Indicates that the account belongs to a pausable mintPausableAccount,/// Test variable-length mint extension#[cfg(test)]VariableLenMintTest = u16::MAX - 2,/// Padding extension used to make an account exactly Multisig::LEN, used/// for testing#[cfg(test)]AccountPaddingTest,/// Padding extension used to make a mint exactly Multisig::LEN, used for/// testing#[cfg(test)]MintPaddingTest,}
每个扩展通过向 mint 或代币账户添加额外状态来提供专门的功能。所有扩展特定的状态都存储在
tlv_data
字段中,该字段遵循基本账户数据类型。您必须根据为该账户启用的特定扩展类型进一步反序列化
tlv_data
(包含扩展状态)。
Token Extensions
/// Encapsulates immutable base state data (mint or account) with possible/// extensions, where the base state is Pod for zero-copy serde.#[derive(Debug, PartialEq)]pub struct PodStateWithExtensions<'data, S: BaseState + Pod> {/// Unpacked base datapub base: &'data S,/// Slice of data containing all TLV data, deserialized on demandtlv_data: &'data [u8],}
Is this page helpful?