Έγκριση Αντιπροσώπου
Πώς να εγκρίνετε έναν αντιπρόσωπο
Η εντολή
ApproveChecked
παραχωρεί σε έναν άλλο λογαριασμό (τον αντιπρόσωπο) την άδεια να μεταφέρει ένα
συγκεκριμένο ποσό tokens από το token account σας. Ο αντιπρόσωπος μπορεί να
μεταφέρει tokens μέχρι το εγκεκριμένο ποσό, αλλά δεν μπορεί να το υπερβεί. Μόνο
ο ιδιοκτήτης του token account μπορεί να εγκρίνει έναν αντιπρόσωπο, και κάθε
token account μπορεί να έχει μόνο έναν αντιπρόσωπο κάθε φορά. Κάθε νέα έγκριση
αντικαθιστά την προηγούμενη.
Το Token Program και το Token Extension Program μοιράζονται παρόμοιες υλοποιήσεις για την επίτευξη της ίδιας λειτουργικότητας.
Typescript
import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners} from "@solana/kit";import { getCreateAccountInstruction } from "@solana-program/system";import {getCreateAssociatedTokenInstructionAsync,getInitializeMintInstruction,getMintSize,TOKEN_PROGRAM_ADDRESS,findAssociatedTokenPda,getMintToInstruction,getApproveCheckedInstruction} from "@solana-program/token";// Create Connection, local validator in this exampleconst rpc = createSolanaRpc("http://localhost:8899");const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");// Generate keypairs for fee payer and delegateconst feePayer = await generateKeyPairSigner();const delegate = await generateKeyPairSigner();// Fund fee payerawait airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: feePayer.address,lamports: lamports(1_000_000_000n),commitment: "confirmed"});// Generate keypair to use as address of mintconst mint = await generateKeyPairSigner();// Get default mint account size (in bytes), no extensions enabledconst space = BigInt(getMintSize());// 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 createAccountInstruction = getCreateAccountInstruction({payer: feePayer,newAccount: mint,lamports: rent,space,programAddress: TOKEN_PROGRAM_ADDRESS});// Instruction to initialize mint account data// Invokes the token programconst initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: 9,mintAuthority: feePayer.address});// Use findAssociatedTokenPda to derive the ATA addressconst [associatedTokenAddress] = await findAssociatedTokenPda({mint: mint.address,owner: feePayer.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});// Create instruction to create the associated token accountconst createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({payer: feePayer,mint: mint.address,owner: feePayer.address});// Create instruction to mint tokensconst mintToInstruction = getMintToInstruction({mint: mint.address,token: associatedTokenAddress,mintAuthority: feePayer.address,amount: 1000_000_000_000n // 1000.0 tokens with 9 decimals});const instructions = [createAccountInstruction,initializeMintInstruction,createAtaInstruction,mintToInstruction];// Create transaction messageconst transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(feePayer, 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" });// Get transaction signatureconst transactionSignature = getSignatureFromTransaction(signedTransaction);console.log("Mint Address:", mint.address.toString());console.log("Associated Token Account Address:",associatedTokenAddress.toString());console.log("Transaction Signature:", transactionSignature);// Get a fresh blockhash for the approve transactionconst { value: approveBlockhash } = await rpc.getLatestBlockhash().send();// Create instruction to approve delegateconst approveInstruction = getApproveCheckedInstruction({source: associatedTokenAddress,mint: mint.address,delegate: delegate.address,owner: feePayer,amount: 1_000_000_000n, // 1.0 tokens with 9 decimalsdecimals: 9});// Create transaction message for approvalconst approveTxMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(approveBlockhash, tx),(tx) => appendTransactionMessageInstructions([approveInstruction], tx));// Sign transaction message with all required signersconst signedApproveTx =await signTransactionMessageWithSigners(approveTxMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedApproveTx,{ commitment: "confirmed" });// Get transaction signatureconst transactionSignature2 = getSignatureFromTransaction(signedApproveTx);console.log("\nDelegate Address:", delegate.address.toString());console.log("Successfully approved delegate to transfer 1.0 tokens");console.log("Transaction Signature:", transactionSignature2);
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::{program_pack::Pack,signature::{Keypair, Signer},transaction::Transaction,};use solana_system_interface::instruction::create_account;use spl_associated_token_account_interface::{address::get_associated_token_address, instruction::create_associated_token_account,};use spl_token_interface::{id as token_program_id,instruction::{approve_checked, initialize_mint, mint_to},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();// Generate a keypair for the delegatelet delegate = Keypair::new();// Airdrop 1 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 1_000_000_000).await?;client.confirm_transaction(&airdrop_signature).await?;loop {let confirmed = client.confirm_transaction(&airdrop_signature).await?;if confirmed {break;}}// Generate keypair to use as address of mintlet mint = Keypair::new();// Number of decimals for the mintlet decimals = 2;// Get default mint account size (in bytes), no extensions enabledlet mint_space = Mint::LEN;let mint_rent = client.get_minimum_balance_for_rent_exemption(mint_space).await?;// Instruction to create new account for mint (token program)let create_account_instruction = create_account(&fee_payer.pubkey(), // payer&mint.pubkey(), // new account (mint)mint_rent, // lamportsmint_space as u64, // space&token_program_id(), // program id);// Instruction to initialize mint account datalet initialize_mint_instruction = initialize_mint(&token_program_id(),&mint.pubkey(), // mint&fee_payer.pubkey(), // mint authoritySome(&fee_payer.pubkey()), // freeze authoritydecimals, // decimals)?;// Calculate the associated token account address for fee_payerlet 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(), // funding address&fee_payer.pubkey(), // wallet address&mint.pubkey(), // mint address&token_program_id(), // program id);// Amount of tokens to mint (100 tokens with 2 decimals)let amount = 100_00;// Create mint_to instruction to mint tokens to the associated token accountlet mint_to_instruction = mint_to(&token_program_id(),&mint.pubkey(), // mint&associated_token_address, // destination&fee_payer.pubkey(), // authority&[&fee_payer.pubkey()], // signeramount, // amount)?;// Create transaction and add instructionslet transaction = Transaction::new_signed_with_payer(&[create_account_instruction,initialize_mint_instruction,create_ata_instruction,mint_to_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint],latest_blockhash,);// Send and confirm transactionclient.send_and_confirm_transaction(&transaction).await?;// Amount of tokens to approve (1 token with 2 decimals)let approve_amount = 1_00;// Create approve_checked instructionlet approve_instruction = approve_checked(&token_program_id(), // program id&associated_token_address, // source token account&mint.pubkey(), // mint&delegate.pubkey(), // delegate&fee_payer.pubkey(), // owner&[&fee_payer.pubkey()], // signersapprove_amount, // amountdecimals, // decimals)?;// Create transaction for approving delegatelet transaction = Transaction::new_signed_with_payer(&[approve_instruction],Some(&fee_payer.pubkey()),&[&fee_payer],latest_blockhash,);// Send and confirm transactionlet transaction_signature = client.send_and_confirm_transaction(&transaction).await?;let token = client.get_token_account(&associated_token_address).await?;println!("Successfully approved delegate for 1.0 token");println!("\nDelegate Address: {}", delegate.pubkey());println!("\nToken Account Address: {}", associated_token_address);if let Some(token) = token {println!("{:#?}", token);}println!("Transaction Signature: {}", transaction_signature);Ok(())}
Console
Click to execute the code.
Python
Python
#!/usr/bin/env python3import asynciofrom solana.rpc.async_api import AsyncClientfrom solders.keypair import Keypairfrom solders.transaction import VersionedTransactionfrom solders.message import MessageV0from solders.system_program import create_account, CreateAccountParamsfrom spl.token.instructions import (initialize_mint, InitializeMintParams,create_associated_token_account,mint_to_checked, MintToCheckedParams,approve_checked, ApproveCheckedParams,get_associated_token_address)from spl.token.constants import TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID# ConstantsDECIMALS = 9async def setup(rpc, mint_authority):"""The setup function initializes the mint and associated token accounts,and mints tokens to said associated token account"""mint = Keypair()space = 82 # getMintSize() equivalent for SPL Token# Get minimum balance for rent exemptionrent = await rpc.get_minimum_balance_for_rent_exemption(space)# Create & initialize mint accountcreate_account_instruction = create_account(CreateAccountParams(from_pubkey=mint_authority.pubkey(),to_pubkey=mint.pubkey(),lamports=rent.value,space=space,owner=TOKEN_PROGRAM_ID))initialize_mint_instruction = initialize_mint(InitializeMintParams(program_id=TOKEN_PROGRAM_ID,mint=mint.pubkey(),decimals=DECIMALS,mint_authority=mint_authority.pubkey(),freeze_authority=None))# Create associated token accountauthority_ata = get_associated_token_address(owner=mint_authority.pubkey(),mint=mint.pubkey(),token_program_id=TOKEN_PROGRAM_ID)create_authority_ata_instruction = create_associated_token_account(payer=mint_authority.pubkey(),owner=mint_authority.pubkey(),mint=mint.pubkey())# Mint to token accountmint_to_instruction = mint_to_checked(MintToCheckedParams(program_id=TOKEN_PROGRAM_ID,mint=mint.pubkey(),dest=authority_ata,mint_authority=mint_authority.pubkey(),amount=1_000_000_000_000, # 1000 tokensdecimals=DECIMALS))instructions = [create_account_instruction,initialize_mint_instruction,create_authority_ata_instruction,mint_to_instruction]# Get latest blockhashlatest_blockhash = await rpc.get_latest_blockhash()# Create messagetransaction_message = MessageV0.try_compile(payer=mint_authority.pubkey(),instructions=instructions,address_lookup_table_accounts=[],recent_blockhash=latest_blockhash.value.blockhash)# Create and sign transactionsigned_transaction = VersionedTransaction(transaction_message, [mint_authority, mint])print("Setup transaction created successfully")print(f"Mint: {mint.pubkey()}")print(f"Authority ATA: {authority_ata}")return {"mint": mint.pubkey(),"authority_ata": authority_ata}async def main():rpc = AsyncClient("http://localhost:8899")# Constantsmint_authority = Keypair()delegate = Keypair()async with rpc:# Get latest blockhashlatest_blockhash = await rpc.get_latest_blockhash()# Setup mint and associated token accountssetup_result = await setup(rpc, mint_authority)mint = setup_result["mint"]authority_ata = setup_result["authority_ata"]# Create delegate instructiondelegate_instruction = approve_checked(ApproveCheckedParams(program_id=TOKEN_PROGRAM_ID,source=authority_ata,mint=mint,delegate=delegate.pubkey(),owner=mint_authority.pubkey(),amount=1_000_000_000, # 1 tokendecimals=DECIMALS))# Create transaction messagetransaction_message = MessageV0.try_compile(payer=mint_authority.pubkey(),instructions=[delegate_instruction],address_lookup_table_accounts=[],recent_blockhash=latest_blockhash.value.blockhash)# Create and sign transactionsigned_transaction = VersionedTransaction(transaction_message, [mint_authority])print(f"\nDelegate Transaction:")print(f"Source: {authority_ata}")print(f"Mint: {mint}")print(f"Delegate: {delegate.pubkey()}")print(f"Owner: {mint_authority.pubkey()}")print(f"Amount: 1_000_000_000 (1 token)")print(f"Decimals: {DECIMALS}")print(f"Delegation transaction created successfully")if __name__ == "__main__":asyncio.run(main())
Console
Click to execute the code.
Is this page helpful?