Aktarılamaz Tokenlar

InitializeNonTransferableMint uzantısını etkinleştirme

InitializeNonTransferableMint tokenların basıldıktan sonra token account'lar arasında transfer edilmesini engeller. Bir kez basıldıktan sonra, tokenlar kalıcı olarak token account'ta kalır. Ancak, token account sahibi yine de tokenları yakabilir ve bakiye sıfıra ulaştığında token account'ı kapatabilir.

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 example
const 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 payer
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: authority.address,
lamports: lamports(5_000_000_000n), // 5 SOL
commitment: "confirmed"
});
// Generate keypair to use as address of mint
const mint = await generateKeyPairSigner();
// Define the non-transferrable extension
const nonTransferableExtension = extension("NonTransferable", {});
// Get mint account size with non-transferrable extension
const space = BigInt(getMintSize([nonTransferableExtension]));
// Get minimum balance for rent exemption
const rent = await rpc.getMinimumBalanceForRentExemption(space).send();
// Get latest blockhash to include in transaction
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
// Instruction to create new account for mint (token program)
// Invokes the system program
const createMintAccountInstruction = getCreateAccountInstruction({
payer: authority,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_2022_PROGRAM_ADDRESS
});
// Instruction to initialize non-transferrable extension
const initializeNonTransferableInstruction =
getInitializeNonTransferableMintInstruction({
mint: mint.address
});
// Instruction to initialize mint account data
// Invokes the token22 program
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 0, // Often 0 for non-transferrable tokens like certificates/badges
mintAuthority: authority.address,
freezeAuthority: authority.address
});
// Use findAssociatedTokenPda to derive the ATA address
const [associatedTokenAddress] = await findAssociatedTokenPda({
mint: mint.address,
owner: authority.address,
tokenProgram: TOKEN_2022_PROGRAM_ADDRESS
});
// instruction to create the associated token account for the authority
const createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({
payer: authority,
mint: mint.address,
owner: authority.address
});
const instructions = [
createMintAccountInstruction,
initializeNonTransferableInstruction,
initializeMintInstruction,
createAtaInstruction
];
// Create transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(authority, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
(tx) => appendTransactionMessageInstructions(instructions, tx)
);
// Sign transaction message with all required signers
const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed", skipPreflight: true }
);
// Get transaction signature
const 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

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 validator
let 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 payer
let fee_payer = Keypair::new();
// Airdrop 5 SOL to fee payer
let airdrop_signature = client
.request_airdrop(&fee_payer.pubkey(), 5_000_000_000)
.await?;
// Ensure the airdrop is confirmed
loop {
let confirmed = client.confirm_transaction(&airdrop_signature).await?;
if confirmed {
break;
}
}
// Generate keypair to use as address of mint
let mint = Keypair::new();
// Calculate mint account size with non-transferrable extension
let 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, // lamports
mint_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 data
let initialize_mint_instruction = initialize_mint(
&TOKEN_2022_PROGRAM_ID, // program id
&mint.pubkey(), // mint
&fee_payer.pubkey(), // mint authority
Some(&fee_payer.pubkey()), // freeze authority
0, // decimals - often 0 for non-transferrable tokens like certificates/badges
)?;
// Instruction to create associated token account
let 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 extension
let 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 account
let mint_account = client.get_account(&mint.pubkey()).await?;
// Deserialize the mint account with extensions
let mint_state = StateWithExtensions::<Mint>::unpack(&mint_account.data)?;
// Get all extension types enabled on this mint
let extension_types = mint_state.get_extension_types()?;
println!("\nExtensions enabled: {:?}", extension_types);
// Deserialize the NonTransferable extension data
let non_transferable = mint_state.get_extension::<NonTransferable>()?;
println!("\n{:#?}", non_transferable);
// Get associated token account address
let 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 account
let 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
&[], // signers
1, // amount
0, // 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?

İçindekiler

Sayfayı Düzenle

Yönetici

© 2025 Solana Vakfı.
Tüm hakları saklıdır.
Bağlanın