Token không thể chuyển nhượng
Cách kích hoạt tiện ích mở rộng InitializeNonTransferableMint
Tiện ích mở rộng
InitializeNonTransferableMint
ngăn chặn việc chuyển token giữa các token account sau khi đúc. Sau khi được
đúc, token sẽ vĩnh viễn nằm trong token account. Tuy nhiên, chủ sở hữu token
account vẫn có thể đốt token và đóng token account khi số dư về không.
Typescript
import { getCreateAccountInstruction } from "@solana-program/system";import {extension,findAssociatedTokenPda,getCreateAssociatedTokenInstructionAsync,getInitializeMintInstruction,getInitializeNonTransferableMintInstruction,getMintSize,getTokenSize,TOKEN_2022_PROGRAM_ADDRESS} from "@solana-program/token-2022";import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners} from "@solana/kit";// Create Connection, local validator in this exampleconst rpc = createSolanaRpc("http://localhost:8899");const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");// Generate the authority for the mint (also acts as fee payer)const authority = await generateKeyPairSigner();// Fund authority/fee payerawait airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: authority.address,lamports: lamports(5_000_000_000n), // 5 SOLcommitment: "confirmed"});// Generate keypair to use as address of mintconst mint = await generateKeyPairSigner();// Define the non-transferrable extensionconst nonTransferableExtension = extension("NonTransferable", {});// Get mint account size with non-transferrable extensionconst space = BigInt(getMintSize([nonTransferableExtension]));// Get minimum balance for rent exemptionconst rent = await rpc.getMinimumBalanceForRentExemption(space).send();// Get latest blockhash to include in transactionconst { value: latestBlockhash } = await rpc.getLatestBlockhash().send();// Instruction to create new account for mint (token program)// Invokes the system programconst createMintAccountInstruction = getCreateAccountInstruction({payer: authority,newAccount: mint,lamports: rent,space,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Instruction to initialize non-transferrable extensionconst initializeNonTransferableInstruction =getInitializeNonTransferableMintInstruction({mint: mint.address});// Instruction to initialize mint account data// Invokes the token22 programconst initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: 0, // Often 0 for non-transferrable tokens like certificates/badgesmintAuthority: authority.address,freezeAuthority: authority.address});// Use findAssociatedTokenPda to derive the ATA addressconst [associatedTokenAddress] = await findAssociatedTokenPda({mint: mint.address,owner: authority.address,tokenProgram: TOKEN_2022_PROGRAM_ADDRESS});// instruction to create the associated token account for the authorityconst createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({payer: authority,mint: mint.address,owner: authority.address});const instructions = [createMintAccountInstruction,initializeNonTransferableInstruction,initializeMintInstruction,createAtaInstruction];// Create transaction messageconst transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(authority, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions(instructions, tx));// Sign transaction message with all required signersconst signedTransaction =await signTransactionMessageWithSigners(transactionMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction,{ commitment: "confirmed", skipPreflight: true });// Get transaction signatureconst transactionSignature = getSignatureFromTransaction(signedTransaction);console.log("Mint Address with Non-Transferrable Extension:", mint.address);console.log("ATA Address:", associatedTokenAddress);console.log("Note: Tokens minted from this mint cannot be transferred between accounts");console.log("Mint Authority:", authority.address);console.log("Transaction Signature:", transactionSignature);
Console
Click to execute the code.
Rust
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_sdk::{commitment_config::CommitmentConfig,signature::{Keypair, Signer},system_instruction::create_account,transaction::Transaction,};use spl_associated_token_account::get_associated_token_address;use spl_associated_token_account::instruction::create_associated_token_account;use spl_token_2022::{ID as TOKEN_2022_PROGRAM_ID,extension::ExtensionType,instruction::{initialize_mint, initialize_non_transferable_mint},state::Mint,};#[tokio::main]async fn main() -> Result<()> {// Create connection to local validatorlet client = RpcClient::new_with_commitment(String::from("http://localhost:8899"),CommitmentConfig::confirmed(),);let latest_blockhash = client.get_latest_blockhash().await?;// Generate a new keypair for the fee payerlet fee_payer = Keypair::new();// Airdrop 5 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 5_000_000_000).await?;client.confirm_transaction(&airdrop_signature).await?;// Ensure the airdrop is confirmedloop {let confirmed = client.confirm_transaction(&airdrop_signature).await?;if confirmed {break;}}// Generate keypair to use as address of mintlet mint = Keypair::new();// Calculate mint account size with non-transferrable extensionlet mint_space =ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::NonTransferable])?;let mint_rent = client.get_minimum_balance_for_rent_exemption(mint_space).await?;// Instruction to create new account for mint (token22)let create_mint_account_instruction = create_account(&fee_payer.pubkey(), // payer&mint.pubkey(), // new account (mint)mint_rent, // lamportsmint_space as u64, // space&TOKEN_2022_PROGRAM_ID, // program id);let initialize_non_transferable_instruction =initialize_non_transferable_mint(&TOKEN_2022_PROGRAM_ID, &mint.pubkey())?;// Instruction to initialize mint account datalet initialize_mint_instruction = initialize_mint(&TOKEN_2022_PROGRAM_ID, // program id&mint.pubkey(), // mint&fee_payer.pubkey(), // mint authoritySome(&fee_payer.pubkey()), // freeze authority0, // decimals - often 0 for non-transferrable tokens like certificates/badges)?;// Get associated token account addresslet associated_token_address = get_associated_token_address(&fee_payer.pubkey(), // owner&mint.pubkey(), // mint);// Instruction to create associated token accountlet create_ata_instruction = create_associated_token_account(&fee_payer.pubkey(), // payer&fee_payer.pubkey(), // owner&mint.pubkey(), // mint&TOKEN_2022_PROGRAM_ID, // token program);// Construct transaction with non-transferrable mint extensionlet transaction = Transaction::new_signed_with_payer(&[create_mint_account_instruction,initialize_non_transferable_instruction,initialize_mint_instruction,create_ata_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint],latest_blockhash,);// Send and confirm transactionlet transaction_signature = client.send_and_confirm_transaction(&transaction).await?;println!("Mint Address with Non-Transferrable Extension: {}",mint.pubkey());println!("ATA Address: {}", associated_token_address);println!("Note: Tokens minted from this mint cannot be transferred between accounts");println!("Mint Authority: {}", fee_payer.pubkey());println!("Transaction Signature: {}", transaction_signature);Ok(())}
Console
Click to execute the code.
Is this page helpful?