feat: upgrade to v0.2.12 and refactor system instructions

This commit is contained in:
ysq
2025-08-03 22:51:11 +08:00
parent 45140ae789
commit e976af6935
11 changed files with 62 additions and 126 deletions
+7 -17
View File
@@ -1,7 +1,5 @@
use solana_sdk::{
pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction,
transaction::Transaction,
};
use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer, transaction::Transaction};
use solana_system_interface::instruction::transfer;
use spl_associated_token_account::get_associated_token_address;
use spl_token::instruction::close_account;
@@ -16,10 +14,8 @@ pub async fn get_token_balance(
) -> Result<u64, anyhow::Error> {
let ata = get_associated_token_address(payer, mint);
let balance = rpc.get_token_account_balance(&ata).await?;
let balance_u64 = balance
.amount
.parse::<u64>()
.map_err(|_| anyhow!("Failed to parse token balance"))?;
let balance_u64 =
balance.amount.parse::<u64>().map_err(|_| anyhow!("Failed to parse token balance"))?;
Ok(balance_u64)
}
@@ -63,8 +59,7 @@ pub async fn transfer_sol(
return Err(anyhow!("Insufficient balance"));
}
let transfer_instruction =
system_instruction::transfer(&payer.pubkey(), receive_wallet, amount);
let transfer_instruction = transfer(&payer.pubkey(), receive_wallet, amount);
let recent_blockhash = rpc.get_latest_blockhash().await?;
@@ -108,13 +103,8 @@ pub async fn close_token_account(
}
// 构建关闭账户指令
let close_account_ix = close_account(
&spl_token::ID,
&ata,
&payer.pubkey(),
&payer.pubkey(),
&[&payer.pubkey()],
)?;
let close_account_ix =
close_account(&spl_token::ID, &ata, &payer.pubkey(), &payer.pubkey(), &[&payer.pubkey()])?;
// 构建交易
let recent_blockhash = rpc.get_latest_blockhash().await?;