Đồng bộ Native
Đồng bộ SOL gốc
Wrapped SOL (WSOL) là một tài khoản token theo dõi số lượng lamport trong tài khoản dưới dạng số dư token. WSOL cho phép tích hợp với các giao thức DeFi để chuyển SOL dưới dạng token SPL.
Lệnh
SyncNative
đồng bộ hóa số dư tài khoản token wrapped SOL (WSOL) với số lượng SOL thực tế
(lamport) được lưu trữ trong đó. Khi bạn chuyển SOL gốc vào tài khoản token
WSOL, số dư token không tự động cập nhật. Bạn phải gọi SyncNative để hiển thị
chính xác số dư WSOL.
Token Program và Token Extension Program có cách triển khai tương tự nhau để đạt được cùng một chức năng.
Typescript
import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners,address} from "@solana/kit";import { getTransferSolInstruction } from "@solana-program/system";import {getCreateAssociatedTokenInstructionAsync,TOKEN_PROGRAM_ADDRESS,findAssociatedTokenPda,getSyncNativeInstruction} 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 payerconst feePayer = await generateKeyPairSigner();// Fund fee payerawait airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: feePayer.address,lamports: lamports(2_000_000_000n),commitment: "confirmed"});// Native mint (Wrapped SOL) addressconst NATIVE_MINT = address("So11111111111111111111111111111111111111112");// Use findAssociatedTokenPda to derive the ATA address for WSOLconst [associatedTokenAddress] = await findAssociatedTokenPda({mint: NATIVE_MINT,owner: feePayer.address,tokenProgram: TOKEN_PROGRAM_ADDRESS});// Get latest blockhash to include in transactionconst { value: latestBlockhash } = await rpc.getLatestBlockhash().send();// Create instruction to create the WSOL associated token accountconst createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({payer: feePayer,mint: NATIVE_MINT,owner: feePayer.address});// Amount to wrap (1 SOL = 1,000,000,000 lamports)const amountToSync = 1_000_000_000n;// Create instruction to transfer SOL to the WSOL token accountconst transferSolInstruction = getTransferSolInstruction({source: feePayer,destination: associatedTokenAddress,amount: amountToSync});// Create instruction to sync native SOL balance with WSOL token balanceconst syncNativeInstruction = getSyncNativeInstruction({account: associatedTokenAddress});const instructions = [createAtaInstruction,transferSolInstruction,syncNativeInstruction];// 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("Fee Payer Address:", feePayer.address.toString());console.log("WSOL Token Account Address:", associatedTokenAddress.toString());console.log("Successfully wrapped 1.0 SOL into WSOL");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::transfer as sol_transfer,transaction::Transaction,};use spl_associated_token_account::{get_associated_token_address_with_program_id, instruction::create_associated_token_account,};use spl_token::{id as token_program_id,instruction::sync_native,native_mint::ID as NATIVE_MINT_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 recent_blockhash = client.get_latest_blockhash().await?;// Generate a new keypair for the fee payerlet fee_payer = Keypair::new();// Airdrop 2 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 2_000_000_000).await?;client.confirm_transaction(&airdrop_signature).await?;loop {let confirmed = client.confirm_transaction(&airdrop_signature).await?;if confirmed {break;}}// Calculate the associated token account address for WSOLlet associated_token_address = get_associated_token_address_with_program_id(&fee_payer.pubkey(), // owner&NATIVE_MINT_ID, // mint (Wrapped SOL)&token_program_id(), // program_id);// Instruction to create associated token account for WSOLlet create_ata_instruction = create_associated_token_account(&fee_payer.pubkey(), // funding address&fee_payer.pubkey(), // wallet address&NATIVE_MINT_ID, // mint address&token_program_id(), // program id);// Amount to wrap (1 SOL = 1,000,000,000 lamports)let amount = 1_000_000_000;// Create transfer instruction to send SOL to the WSOL token accountlet transfer_instruction = sol_transfer(&fee_payer.pubkey(),&associated_token_address,amount);// Create sync native instruction to update WSOL balancelet sync_native_instruction = sync_native(&token_program_id(),&associated_token_address)?;// Create transaction and add instructionslet transaction = Transaction::new_signed_with_payer(&[create_ata_instruction,transfer_instruction,sync_native_instruction,],Some(&fee_payer.pubkey()),&[&fee_payer],recent_blockhash,);// Send and confirm transactionlet transactionSignature = client.send_and_confirm_transaction(&transaction).await?;println!("Fee Payer Address: {}", fee_payer.pubkey());println!("WSOL Token Account Address: {}",associated_token_address);println!("Successfully wrapped 1.0 SOL into WSOL");println!("Transaction Signature: {}", transactionSignature);Ok(())}
Console
Click to execute the code.
Is this page helpful?