Permanente gedelegeerde
Hoe de InitializePermanentDelegate extensie te implementeren
De
InitializePermanentDelegate
wijst een onherroepelijke gedelegeerde toe met autoriteit over alle token
accounts voor een mint. In tegenstelling tot reguliere gedelegeerden die token
account eigenaren kunnen intrekken, kan een permanente gedelegeerde niet worden
gewijzigd of verwijderd. De permanente gedelegeerde kan tokens overdragen,
verbranden en bevriezen in elk token account van die mint.
Typescript
import { getCreateAccountInstruction } from "@solana-program/system";import {extension,getInitializeAccountInstruction,getInitializeMintInstruction,getInitializePermanentDelegateInstruction,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 permanent delegate config extensionconst permanentDelegateExtension = extension("PermanentDelegate", {delegate: authority.address // this account will always be able to transfer/burn tokens});// Get mint account size with permanent delegate extensionconst space = BigInt(getMintSize([permanentDelegateExtension]));// 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)const createMintAccountInstruction = getCreateAccountInstruction({payer: authority,newAccount: mint,lamports: rent,space,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Instruction to initialize permanent delegate extensionconst initializePermanentDelegateInstruction =getInitializePermanentDelegateInstruction({mint: mint.address,delegate: authority.address});// Instruction to initialize mint account dataconst initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: 6,mintAuthority: authority.address,freezeAuthority: authority.address});// Generate keypair to use as address of token accountconst tokenAccount = await generateKeyPairSigner();// get token account size (with extension awareness)const tokenAccountLen = BigInt(getTokenSize());// Get minimum balance for rent exemptionconst tokenAccountRent = await rpc.getMinimumBalanceForRentExemption(tokenAccountLen).send();// Instruction to create new account for the token accountconst createTokenAccountInstruction = getCreateAccountInstruction({payer: authority,newAccount: tokenAccount,lamports: tokenAccountRent,space: tokenAccountLen,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Instruction to initialize the created token accountconst initializeTokenAccountInstruction = getInitializeAccountInstruction({account: tokenAccount.address,mint: mint.address,owner: authority.address});// Collect all instructionsconst instructions = [createMintAccountInstruction,initializePermanentDelegateInstruction,initializeMintInstruction,createTokenAccountInstruction,initializeTokenAccountInstruction];// 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 Permanent Delegate Extension:",mint.address.toString());console.log("Token Account:", tokenAccount.address.toString());console.log("Permanent Delegate:", authority.address.toString());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_token_2022_interface::{extension::{permanent_delegate::PermanentDelegate, BaseStateWithExtensions, ExtensionType,StateWithExtensions,},instruction::{initialize_mint, initialize_permanent_delegate},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();// Generate a new keypair for the delegatelet delegate = Keypair::new();// Airdrop 5 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 5_000_000_000).await?;loop {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 PermanentDelegate extensionlet mint_space =ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::PermanentDelegate])?;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);// Instruction to initialize PermanentDelegate extensionlet initialize_permanent_delegate_ix = initialize_permanent_delegate(&TOKEN_2022_PROGRAM_ID,&mint.pubkey(),&delegate.pubkey(), // permanent delegate authority)?;// 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 authority3, // decimals)?;// Construct transaction with PermanentDelegate extensionlet transaction = Transaction::new_signed_with_payer(&[create_mint_account_instruction,initialize_permanent_delegate_ix,initialize_mint_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint],latest_blockhash,);// Send and confirm transactionclient.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 mint_extension_types = mint_state.get_extension_types()?;println!("\nMint extensions enabled: {:?}", mint_extension_types);// Deserialize the PermanentDelegate extension datalet permanent_delegate = mint_state.get_extension::<PermanentDelegate>()?;println!("\n{:#?}", permanent_delegate);Ok(())}
Console
Click to execute the code.
Is this page helpful?