Hesabı Çöz

Hesabı Çöz

ThawAccount talimatı, dondurma işlemini tersine çevirerek, önceden dondurulmuş bir token account'a tam işlevselliğini geri kazandırır. Çözüldükten sonra, hesap tekrar normal şekilde token gönderip alabilir. Yalnızca token'ın mint'inin dondurma yetkisine sahip kişi hesapları çözebilir.

Token Program ve Token Extension Program aynı işlevselliği sağlamak için benzer uygulamalara sahiptir.

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,
getFreezeAccountInstruction,
getThawAccountInstruction
} from "@solana-program/token";
// Create Connection, local validator in this example
const rpc = createSolanaRpc("http://localhost:8899");
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");
// Generate keypairs for fee payer
const feePayer = await generateKeyPairSigner();
// Fund fee payer
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: feePayer.address,
lamports: lamports(1_000_000_000n),
commitment: "confirmed"
});
// Generate keypair to use as address of mint
const mint = await generateKeyPairSigner();
// Get default mint account size (in bytes), no extensions enabled
const space = BigInt(getMintSize());
// 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 createAccountInstruction = getCreateAccountInstruction({
payer: feePayer,
newAccount: mint,
lamports: rent,
space,
programAddress: TOKEN_PROGRAM_ADDRESS
});
// Instruction to initialize mint account data
// Invokes the token program
const initializeMintInstruction = getInitializeMintInstruction({
mint: mint.address,
decimals: 9,
mintAuthority: feePayer.address,
freezeAuthority: feePayer.address
});
// Use findAssociatedTokenPda to derive the ATA address
const [associatedTokenAddress] = await findAssociatedTokenPda({
mint: mint.address,
owner: feePayer.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS
});
// Create instruction to create the associated token account
const createAtaInstruction = await getCreateAssociatedTokenInstructionAsync({
payer: feePayer,
mint: mint.address,
owner: feePayer.address
});
// Create instruction to mint tokens
const mintToInstruction = getMintToInstruction({
mint: mint.address,
token: associatedTokenAddress,
mintAuthority: feePayer.address,
amount: 1000_000_000_000n // 1000.0 tokens with 9 decimals
});
// Create instruction to freeze the token account
const freezeInstruction = getFreezeAccountInstruction({
account: associatedTokenAddress,
mint: mint.address,
owner: feePayer.address
});
const instructions = [
createAccountInstruction,
initializeMintInstruction,
createAtaInstruction,
mintToInstruction,
freezeInstruction
];
// Create transaction message
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, 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" }
);
// Get transaction signature
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Mint Address:", mint.address.toString());
console.log(
"Associated Token Account Address:",
associatedTokenAddress.toString()
);
console.log("Transaction Signature:", transactionSignature);
console.log("Successfully minted 1000.0 tokens and frozen the account");
// Get a fresh blockhash for the thaw transaction
const { value: thawBlockhash } = await rpc.getLatestBlockhash().send();
// Create instruction to thaw the frozen token account
const thawInstruction = getThawAccountInstruction({
account: associatedTokenAddress,
mint: mint.address,
owner: feePayer.address
});
// Create transaction message for thawing
const thawTxMessage = pipe(
createTransactionMessage({ version: 0 }),
(tx) => setTransactionMessageFeePayerSigner(feePayer, tx),
(tx) => setTransactionMessageLifetimeUsingBlockhash(thawBlockhash, tx),
(tx) => appendTransactionMessageInstructions([thawInstruction], tx)
);
// Sign transaction message with all required signers
const signedThawTx = await signTransactionMessageWithSigners(thawTxMessage);
// Send and confirm transaction
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedThawTx,
{ commitment: "confirmed" }
);
// Get transaction signature
const transactionSignature2 = getSignatureFromTransaction(signedThawTx);
console.log("Successfully thawed the frozen token account");
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::{freeze_account, initialize_mint, mint_to, thaw_account},
state::Mint,
};
#[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 1 SOL to fee payer
let 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 mint
let mint = Keypair::new();
// Number of decimals for the mint
let decimals = 2;
// Get default mint account size (in bytes), no extensions enabled
let 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, // lamports
mint_space as u64, // space
&token_program_id(), // program id
);
// Instruction to initialize mint account data
let initialize_mint_instruction = initialize_mint(
&token_program_id(),
&mint.pubkey(), // mint
&fee_payer.pubkey(), // mint authority
Some(&fee_payer.pubkey()), // freeze authority
decimals, // decimals
)?;
// Get the associated token account address for fee_payer
let associated_token_address = get_associated_token_address(
&fee_payer.pubkey(), // owner
&mint.pubkey(), // mint
);
// Instruction to create associated token account
let 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 account
let mint_to_instruction = mint_to(
&token_program_id(),
&mint.pubkey(), // mint
&associated_token_address, // destination
&fee_payer.pubkey(), // authority
&[&fee_payer.pubkey()], // signer
amount, // amount
)?;
// Create freeze account instruction
let freeze_instruction = freeze_account(
&token_program_id(), // program id
&associated_token_address, // token account to freeze
&mint.pubkey(), // mint
&fee_payer.pubkey(), // freeze authority
&[&fee_payer.pubkey()], // signers
)?;
// Create transaction and add instructions
let transaction = Transaction::new_signed_with_payer(
&[
create_account_instruction,
initialize_mint_instruction,
create_ata_instruction,
mint_to_instruction,
freeze_instruction,
],
Some(&fee_payer.pubkey()),
&[&fee_payer, &mint],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
let token_account = client.get_token_account(&associated_token_address).await?;
println!("Token Account Address: {}", associated_token_address);
if let Some(token_account) = token_account {
println!("\nToken Account Frozen: {:#?}", token_account.state);
println!("{:#?}", token_account);
}
// Create thaw account instruction
let thaw_instruction = thaw_account(
&token_program_id(), // program id
&associated_token_address, // token account to thaw
&mint.pubkey(), // mint
&fee_payer.pubkey(), // freeze authority
&[&fee_payer.pubkey()], // signers
)?;
// Create transaction for thawing token account
let transaction = Transaction::new_signed_with_payer(
&[thaw_instruction],
Some(&fee_payer.pubkey()),
&[&fee_payer],
latest_blockhash,
);
// Send and confirm transaction
client.send_and_confirm_transaction(&transaction).await?;
let token_account = client.get_token_account(&associated_token_address).await?;
if let Some(token_account) = token_account {
println!(
"\nToken Account Thawed (Unfrozen): {:#?}",
token_account.state
);
println!("{:#?}", token_account);
}
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