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
+6 -13
View File
@@ -1,11 +1,7 @@
use anyhow::anyhow;
use solana_sdk::{
instruction::Instruction,
signature::Keypair,
signer::Signer,
system_instruction,
};
use solana_hash::Hash;
use solana_sdk::{instruction::Instruction, signature::Keypair, signer::Signer};
use solana_system_interface::instruction::advance_nonce_account;
use crate::common::nonce_cache::NonceCache;
@@ -15,8 +11,8 @@ use crate::common::nonce_cache::NonceCache;
/// 如果nonce被锁定、已使用或未准备好,将返回错误
/// 成功时会锁定并标记nonce为已使用
pub fn add_nonce_instruction(
instructions: &mut Vec<Instruction>,
payer: &Keypair
instructions: &mut Vec<Instruction>,
payer: &Keypair,
) -> Result<(), anyhow::Error> {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
@@ -40,10 +36,7 @@ pub fn add_nonce_instruction(
// nonce_cache.lock();
// 创建Solana系统nonce推进指令 - 使用系统程序ID
let nonce_advance_ix = system_instruction::advance_nonce_account(
&nonce_pubkey,
&payer.pubkey(),
);
let nonce_advance_ix = advance_nonce_account(&nonce_pubkey, &payer.pubkey());
instructions.push(nonce_advance_ix);
}
@@ -69,4 +62,4 @@ pub fn is_using_nonce() -> bool {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
nonce_info.nonce_account.is_some()
}
}
+8 -20
View File
@@ -2,13 +2,13 @@ use solana_hash::Hash;
use solana_sdk::{
instruction::Instruction,
message::{v0, VersionedMessage},
native_token::sol_to_lamports,
native_token::sol_str_to_lamports,
pubkey::Pubkey,
signature::Keypair,
signer::Signer,
system_instruction,
transaction::VersionedTransaction,
};
use solana_system_interface::instruction::transfer;
use std::sync::Arc;
use super::{
@@ -54,13 +54,7 @@ pub async fn build_rpc_transaction(
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
blockhash,
)
.await
build_versioned_transaction(payer, instructions, address_lookup_table_accounts, blockhash).await
}
/// 构建带小费的交易
@@ -88,10 +82,10 @@ pub async fn build_tip_transaction(
instructions.extend(business_instructions);
// 添加小费转账指令
instructions.push(system_instruction::transfer(
instructions.push(transfer(
&payer.pubkey(),
tip_account,
sol_to_lamports(tip_amount),
sol_str_to_lamports(tip_amount.to_string().as_str()).unwrap_or(0),
));
// 获取交易使用的blockhash
@@ -101,13 +95,7 @@ pub async fn build_tip_transaction(
let address_lookup_table_accounts = get_address_lookup_table_accounts(lookup_table_key).await;
// 构建交易
build_versioned_transaction(
payer,
instructions,
address_lookup_table_accounts,
blockhash,
)
.await
build_versioned_transaction(payer, instructions, address_lookup_table_accounts, blockhash).await
}
/// 构建版本化交易的底层函数
@@ -200,10 +188,10 @@ pub async fn build_sell_tip_transaction(
instructions.extend(business_instructions);
// 添加小费转账指令
instructions.push(system_instruction::transfer(
instructions.push(transfer(
&payer.pubkey(),
tip_account,
sol_to_lamports(tip_amount),
sol_str_to_lamports(tip_amount.to_string().as_str()).unwrap_or(0),
));
// 获取地址查找表账户
+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?;