الرموز غير القابلة للتحويل
كيفية تمكين امتداد InitializeNonTransferableMint
يمنع
InitializeNonTransferableMint
الرموز من التحويل بين حسابات الرموز بعد سكها. بمجرد سكها، تبقى الرموز بشكل دائم
في حساب الرمز. ومع ذلك، لا يزال بإمكان مالك حساب الرمز حرق الرموز وإغلاق حساب
الرمز عندما يصل الرصيد إلى صفر.
تايبسكريبت
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_commitment_config::CommitmentConfig;use solana_sdk::{signature::{Keypair, Signer},transaction::Transaction,};use solana_system_interface::instruction::create_account;use spl_associated_token_account_interface::{address::get_associated_token_address_with_program_id,instruction::create_associated_token_account,};use spl_token_2022_interface::{extension::{non_transferable::NonTransferable, BaseStateWithExtensions, ExtensionType,StateWithExtensions,},instruction::{initialize_mint, initialize_non_transferable_mint, mint_to, transfer_checked},state::Mint,ID as TOKEN_2022_PROGRAM_ID,};#[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?;// 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)?;// 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,);client.send_and_confirm_transaction(&transaction).await?;println!("Mint Address: {}", mint.pubkey());// Fetch mint accountlet mint_account = client.get_account(&mint.pubkey()).await?;// Deserialize the mint account with extensionslet mint_state = StateWithExtensions::<Mint>::unpack(&mint_account.data)?;// Get all extension types enabled on this mintlet extension_types = mint_state.get_extension_types()?;println!("\nExtensions enabled: {:?}", extension_types);// Deserialize the NonTransferable extension datalet non_transferable = mint_state.get_extension::<NonTransferable>()?;println!("\n{:#?}", non_transferable);// Get associated token account addresslet associated_token_address = get_associated_token_address_with_program_id(&fee_payer.pubkey(), // owner&mint.pubkey(), // minti&TOKEN_2022_PROGRAM_ID, // token program);// Mint 1 token to the associated token accountlet mint_to_instruction = mint_to(&TOKEN_2022_PROGRAM_ID,&mint.pubkey(),&associated_token_address,&fee_payer.pubkey(),&[],1,)?;let mint_transaction = Transaction::new_signed_with_payer(&[mint_to_instruction],Some(&fee_payer.pubkey()),&[&fee_payer],latest_blockhash,);client.send_and_confirm_transaction(&mint_transaction).await?;// Attempt to transfer token (should fail with non-transferable error)println!("\nAttempting to transfer non-transferable token");let transfer_instruction = transfer_checked(&TOKEN_2022_PROGRAM_ID,&associated_token_address, // source&mint.pubkey(), // mint&associated_token_address, // destination (same as source)&fee_payer.pubkey(), // owner&[], // signers1, // amount0, // decimals)?;let transfer_transaction = Transaction::new_signed_with_payer(&[transfer_instruction],Some(&fee_payer.pubkey()),&[&fee_payer],latest_blockhash,);match client.send_and_confirm_transaction(&transfer_transaction).await{Ok(sig) => println!("Transfer succeeded unexpectedly: {}", sig),Err(e) => println!("Transfer failed as expected:\n{:#?}", e),}Ok(())}
Console
Click to execute the code.
Is this page helpful?