feat: Add address lookup, caching system and Jito GRPC integration
- Implement address lookup tables and multi-level caching - Add GRPC stream processing and YellowStone connections - Extend PumpFun functionality including token creation - Integrate Jito GRPC services (auth, block engine, relayer) - Optimize log processing and event system
This commit is contained in:
+309
-63
@@ -1,73 +1,248 @@
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::{
|
||||
compute_budget::ComputeBudgetInstruction, instruction::Instruction, message::{v0, VersionedMessage}, native_token::sol_to_lamports, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::{Transaction, VersionedTransaction}
|
||||
compute_budget::ComputeBudgetInstruction, instruction::Instruction, message::{v0, AddressLookupTableAccount, VersionedMessage}, native_token::sol_to_lamports, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::{Transaction, VersionedTransaction}
|
||||
};
|
||||
use solana_hash::Hash;
|
||||
use spl_associated_token_account::instruction::create_associated_token_account;
|
||||
use tokio::task::JoinHandle;
|
||||
use std::{str::FromStr, time::Instant, sync::Arc};
|
||||
|
||||
use crate::{common::{PriorityFee, SolanaRpcClient}, constants::{self, global_constants::FEE_RECIPIENT}, instruction, swqos::FeeClient};
|
||||
use crate::{
|
||||
common::{
|
||||
address_lookup_cache::get_address_lookup_table_account,
|
||||
nonce_cache:: NonceCache,
|
||||
tip_cache::TipCache,
|
||||
PriorityFee,
|
||||
SolanaRpcClient
|
||||
},
|
||||
constants::{self, global_constants::FEE_RECIPIENT},
|
||||
instruction,
|
||||
swqos::{ClientType, FeeClient, TradeType}
|
||||
};
|
||||
|
||||
const MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT: u32 = 250000;
|
||||
|
||||
use super::common::{calculate_with_slippage_buy, get_bonding_curve_account, get_buy_token_amount_from_sol_amount, get_creator_vault_pda};
|
||||
use super::common::{calculate_with_slippage_buy, get_buy_token_amount_from_sol_amount, init_bonding_curve_account};
|
||||
|
||||
/// 添加nonce消费指令到指令集合中
|
||||
///
|
||||
/// 只有提供了nonce_pubkey时才使用nonce功能
|
||||
/// 如果nonce被锁定、已使用或未准备好,将返回错误
|
||||
/// 成功时会锁定并标记nonce为已使用
|
||||
fn add_nonce_instruction(instructions: &mut Vec<Instruction>, payer: &Keypair) -> Result<(), anyhow::Error> {
|
||||
let nonce_cache = NonceCache::get_instance();
|
||||
let nonce_info = nonce_cache.get_nonce_info();
|
||||
|
||||
// 只检查nonce_account是否存在
|
||||
if let Some(nonce_pubkey) = nonce_info.nonce_account {
|
||||
// 暂不加锁
|
||||
// if nonce_info.lock {
|
||||
// return Err(anyhow!("Nonce is locked"));
|
||||
// }
|
||||
if nonce_info.used {
|
||||
return Err(anyhow!("Nonce is used"));
|
||||
}
|
||||
if nonce_info.current_nonce == Hash::default() {
|
||||
return Err(anyhow!("Nonce is not ready"));
|
||||
}
|
||||
// if nonce_info.next_buy_time == 0 || chrono::Utc::now().timestamp() < nonce_info.next_buy_time {
|
||||
// return Err(anyhow!("Nonce is not ready"));
|
||||
// }
|
||||
// 加锁 - 暂不加锁
|
||||
// nonce_cache.lock();
|
||||
|
||||
// 创建Solana系统nonce推进指令 - 使用系统程序ID
|
||||
let nonce_advance_ix = system_instruction::advance_nonce_account(
|
||||
&nonce_pubkey,
|
||||
&payer.pubkey(),
|
||||
);
|
||||
|
||||
|
||||
instructions.push(nonce_advance_ix);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn buy(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_sol: u64,
|
||||
creator: Pubkey,
|
||||
dev_buy_token: u64,
|
||||
dev_sol_cost: u64,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let transaction = build_buy_transaction(rpc.clone(), payer.clone(), mint.clone(), amount_sol, slippage_basis_points, priority_fee.clone()).await?;
|
||||
let start_time = Instant::now();
|
||||
let mint = Arc::new(mint.clone());
|
||||
let instructions = build_buy_instructions(payer.clone(), mint.clone(), creator, dev_buy_token, dev_sol_cost, buy_sol_cost, slippage_basis_points).await?;
|
||||
println!(" 买入交易指令: {:?}", start_time.elapsed());
|
||||
|
||||
let start_time = Instant::now();
|
||||
let transaction = build_buy_transaction(
|
||||
payer.clone(),
|
||||
priority_fee.clone(),
|
||||
instructions,
|
||||
lookup_table_key,
|
||||
recent_blockhash,
|
||||
).await?;
|
||||
println!(" 买入交易签名: {:?}", start_time.elapsed());
|
||||
|
||||
let start_time = Instant::now();
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
println!(" 买入交易确认: {:?}", start_time.elapsed());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Buy tokens using Jito
|
||||
// pub async fn buy_with_tip(
|
||||
// fee_clients: Vec<Arc<FeeClient>>,
|
||||
// payer: Arc<Keypair>,
|
||||
// mint: Pubkey,
|
||||
// creator: Pubkey,
|
||||
// dev_buy_token: u64,
|
||||
// dev_sol_cost: u64,
|
||||
// buy_sol_cost: u64,
|
||||
// slippage_basis_points: Option<u64>,
|
||||
// priority_fee: PriorityFee,
|
||||
// lookup_table_key: Option<Pubkey>,
|
||||
// recent_blockhash: Hash,
|
||||
// ) -> Result<(), anyhow::Error> {
|
||||
// let start_time = Instant::now();
|
||||
// let mint = Arc::new(mint.clone());
|
||||
// let instructions = build_buy_instructions(payer.clone(), mint.clone(), creator, dev_buy_token, dev_sol_cost, buy_sol_cost, slippage_basis_points).await?;
|
||||
// println!(" 买入交易指令: {:?}", start_time.elapsed());
|
||||
|
||||
// let start_time = Instant::now();
|
||||
// let mut transactions = vec![];
|
||||
|
||||
// for fee_client in fee_clients.clone() {
|
||||
// if fee_client.get_client_type() == ClientType::Rpc {
|
||||
// let transaction = build_buy_transaction(
|
||||
// payer.clone(),
|
||||
// priority_fee.clone(),
|
||||
// instructions.clone(),
|
||||
// lookup_table_key,
|
||||
// recent_blockhash,
|
||||
// ).await?;
|
||||
|
||||
// transactions.push(transaction);
|
||||
// } else {
|
||||
// let tip_account = fee_client.get_tip_account()?;
|
||||
// let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
|
||||
// let transaction = build_buy_transaction_with_tip(
|
||||
// tip_account,
|
||||
// payer.clone(),
|
||||
// priority_fee.clone(),
|
||||
// instructions.clone(),
|
||||
// lookup_table_key,
|
||||
// recent_blockhash,
|
||||
// ).await?;
|
||||
|
||||
// transactions.push(transaction);
|
||||
// }
|
||||
// }
|
||||
|
||||
// println!(" 买入交易签名: {:?}", start_time.elapsed());
|
||||
|
||||
// let cores = core_affinity::get_core_ids().unwrap();
|
||||
// let mut handles: Vec<JoinHandle<Result<(), anyhow::Error>>> = vec![];
|
||||
// for i in 0..fee_clients.len() {
|
||||
// let fee_client = fee_clients[i].clone();
|
||||
// let transactions = transactions.clone();
|
||||
// let transaction = transactions[i].clone();
|
||||
|
||||
// let core_id = cores[i % cores.len()];
|
||||
// let handle = tokio::spawn(async move {
|
||||
// core_affinity::set_for_current(core_id);
|
||||
// fee_client.send_transaction(TradeType::Buy, &transaction).await?;
|
||||
// Ok::<(), anyhow::Error>(())
|
||||
// });
|
||||
|
||||
// handles.push(handle);
|
||||
// }
|
||||
|
||||
// for handle in handles {
|
||||
// match handle.await {
|
||||
// Ok(Ok(_)) => (),
|
||||
// Ok(Err(e)) => println!("Error in task: {}", e),
|
||||
// Err(e) => println!("Task join error: {}", e),
|
||||
// }
|
||||
// }
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
pub async fn buy_with_tip(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
fee_clients: Vec<Arc<FeeClient>>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_sol: u64,
|
||||
creator: Pubkey,
|
||||
dev_buy_token: u64,
|
||||
dev_sol_cost: u64,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let start_time = Instant::now();
|
||||
|
||||
let mint = Arc::new(mint.clone());
|
||||
let instructions = build_buy_instructions(rpc.clone(), payer.clone(), mint.clone(), amount_sol, slippage_basis_points).await?;
|
||||
|
||||
let mut transactions = vec![];
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
for fee_client in fee_clients.clone() {
|
||||
let payer = payer.clone();
|
||||
let priority_fee = priority_fee.clone();
|
||||
let tip_account = fee_client.get_tip_account().await.map_err(|e| anyhow!(e.to_string()))?;
|
||||
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
|
||||
let transaction = build_buy_transaction_with_tip(tip_account, payer, priority_fee, instructions.clone(), recent_blockhash).await?;
|
||||
transactions.push(transaction);
|
||||
}
|
||||
let instructions = build_buy_instructions(payer.clone(), mint.clone(), creator, dev_buy_token, dev_sol_cost, buy_sol_cost, slippage_basis_points).await?;
|
||||
println!(" 买入交易指令: {:?}", start_time.elapsed());
|
||||
|
||||
let start_time = Instant::now();
|
||||
let cores = core_affinity::get_core_ids().unwrap();
|
||||
let mut handles: Vec<JoinHandle<Result<(), anyhow::Error>>> = vec![];
|
||||
|
||||
for i in 0..fee_clients.len() {
|
||||
let fee_client = fee_clients[i].clone();
|
||||
let transactions = transactions.clone();
|
||||
let start_time = start_time.clone();
|
||||
let transaction = transactions[i].clone();
|
||||
let payer = payer.clone();
|
||||
let instructions = instructions.clone();
|
||||
let mut priority_fee = priority_fee.clone();
|
||||
let core_id = cores[i % cores.len()];
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
fee_client.send_transaction(&transaction).await?;
|
||||
println!("index: {}, Total Jito buy operation time: {:?}ms", i, start_time.elapsed().as_millis());
|
||||
core_affinity::set_for_current(core_id);
|
||||
|
||||
let transaction = if fee_client.get_client_type() == ClientType::Rpc {
|
||||
build_buy_transaction(
|
||||
payer.clone(),
|
||||
priority_fee.clone(),
|
||||
instructions.clone(),
|
||||
lookup_table_key,
|
||||
recent_blockhash,
|
||||
).await?
|
||||
} else {
|
||||
let tip_account = fee_client.get_tip_account()?;
|
||||
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
priority_fee.buy_tip_fee = priority_fee.buy_tip_fees[i];
|
||||
// println!(" 买入交易小费: {:?}", priority_fee.buy_tip_fee);
|
||||
build_buy_transaction_with_tip(
|
||||
tip_account,
|
||||
payer.clone(),
|
||||
priority_fee.clone(),
|
||||
instructions.clone(),
|
||||
lookup_table_key,
|
||||
recent_blockhash,
|
||||
).await?
|
||||
};
|
||||
|
||||
fee_client.send_transaction(TradeType::Buy, &transaction).await?;
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
||||
handles.push(handle);
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
println!(" 买入交易签名: {:?}", start_time.elapsed());
|
||||
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(Ok(_)) => (),
|
||||
@@ -80,29 +255,44 @@ pub async fn buy_with_tip(
|
||||
}
|
||||
|
||||
pub async fn build_buy_transaction(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_sol: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
) -> Result<Transaction, anyhow::Error> {
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT),
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
];
|
||||
build_instructions: Vec<Instruction>,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![];
|
||||
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
let build_instructions = build_buy_instructions(rpc.clone(), payer.clone(), Arc::new(mint), amount_sol, slippage_basis_points).await?;
|
||||
// 添加计算预算指令
|
||||
instructions.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_price( priority_fee.rpc_unit_price ));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit( priority_fee.rpc_unit_limit ));
|
||||
instructions.extend(build_instructions);
|
||||
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
let transaction = Transaction::new_signed_with_payer(
|
||||
&instructions,
|
||||
Some(&payer.pubkey()),
|
||||
&[payer],
|
||||
recent_blockhash,
|
||||
);
|
||||
let nonce_cache = NonceCache::get_instance();
|
||||
let nonce_info = nonce_cache.get_nonce_info();
|
||||
|
||||
let blockhash = if nonce_info.nonce_account.is_some() && instructions.len() > 0 {
|
||||
nonce_info.current_nonce
|
||||
} else {
|
||||
recent_blockhash
|
||||
};
|
||||
|
||||
let mut address_lookup_table_accounts = vec![];
|
||||
if let Some(lookup_table_key) = lookup_table_key {
|
||||
let account = get_address_lookup_table_account(&lookup_table_key).await;
|
||||
address_lookup_table_accounts.push(account);
|
||||
}
|
||||
|
||||
let v0_message: v0::Message =
|
||||
v0::Message::try_compile(&payer.pubkey(), &instructions, &address_lookup_table_accounts, blockhash)?;
|
||||
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message.clone());
|
||||
let transaction = VersionedTransaction::try_new(versioned_message, &[payer.as_ref()])?;
|
||||
|
||||
// verify_lookup_table_usage(&v0_message, &address_lookup_table_accounts);
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
@@ -112,33 +302,66 @@ pub async fn build_buy_transaction_with_tip(
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: PriorityFee,
|
||||
build_instructions: Vec<Instruction>,
|
||||
blockhash: Hash,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT),
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
system_instruction::transfer(
|
||||
&payer.pubkey(),
|
||||
&tip_account,
|
||||
sol_to_lamports(priority_fee.buy_tip_fee),
|
||||
),
|
||||
];
|
||||
// 从TipCache获取tip金额
|
||||
// let tip_cache = TipCache::get_instance();
|
||||
// let tip_amount = tip_cache.get_tip();
|
||||
// let tip_amount = priority_fee.buy_tip_fee;
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
// 添加nonce消费指令
|
||||
if let Err(e) = add_nonce_instruction(&mut instructions, payer.as_ref()) {
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
// 添加计算预算指令和小费转账指令
|
||||
instructions.push(ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price));
|
||||
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit));
|
||||
instructions.extend(build_instructions);
|
||||
instructions.push(system_instruction::transfer(
|
||||
&payer.pubkey(),
|
||||
&tip_account,
|
||||
sol_to_lamports(priority_fee.buy_tip_fee),
|
||||
));
|
||||
|
||||
let nonce_cache = NonceCache::get_instance();
|
||||
let nonce_info = nonce_cache.get_nonce_info();
|
||||
|
||||
// 如果使用了nonce账户,则使用nonce账户中的blockhash
|
||||
let blockhash_to_use = if nonce_info.nonce_account.is_some() && instructions.len() > 0 {
|
||||
nonce_info.current_nonce
|
||||
} else {
|
||||
recent_blockhash
|
||||
};
|
||||
|
||||
let mut address_lookup_table_accounts = vec![];
|
||||
if let Some(lookup_table_key) = lookup_table_key {
|
||||
let account = get_address_lookup_table_account(&lookup_table_key).await;
|
||||
address_lookup_table_accounts.push(account);
|
||||
}
|
||||
|
||||
let v0_message: v0::Message =
|
||||
v0::Message::try_compile(&payer.pubkey(), &instructions, &[], blockhash)?;
|
||||
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
|
||||
let transaction = VersionedTransaction::try_new(versioned_message, &[&payer])?;
|
||||
v0::Message::try_compile(&payer.pubkey(), &instructions, &address_lookup_table_accounts, blockhash_to_use)?;
|
||||
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message.clone());
|
||||
let transaction = VersionedTransaction::try_new(versioned_message, &[payer.as_ref()])?;
|
||||
|
||||
// nonce_cache.mark_used();
|
||||
// verify_lookup_table_usage(&v0_message, &address_lookup_table_accounts);
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn build_buy_instructions(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
// rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Arc<Pubkey>,
|
||||
creator: Pubkey,
|
||||
dev_buy_token: u64,
|
||||
dev_sol_cost: u64,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
) -> Result<Vec<Instruction>, anyhow::Error> {
|
||||
@@ -146,9 +369,9 @@ pub async fn build_buy_instructions(
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
let (bonding_curve, bonding_curve_pda) = get_bonding_curve_account(&rpc, &mint).await?;
|
||||
let creator_vault_pda = get_creator_vault_pda(&bonding_curve.creator).unwrap();
|
||||
let bonding_curve = init_bonding_curve_account(&mint, dev_buy_token, dev_sol_cost, creator).await?;
|
||||
let max_sol_cost = calculate_with_slippage_buy(buy_sol_cost, slippage_basis_points.unwrap_or(100));
|
||||
let creator_vault_pda = bonding_curve.get_creator_vault_pda();
|
||||
|
||||
let mut buy_token_amount = get_buy_token_amount_from_sol_amount(&bonding_curve, buy_sol_cost);
|
||||
if buy_token_amount <= 100 * 1_000_000_u64 {
|
||||
@@ -170,7 +393,7 @@ pub async fn build_buy_instructions(
|
||||
instructions.push(instruction::buy(
|
||||
payer.as_ref(),
|
||||
&mint,
|
||||
&bonding_curve_pda,
|
||||
&bonding_curve.account,
|
||||
&creator_vault_pda,
|
||||
&FEE_RECIPIENT,
|
||||
instruction::Buy {
|
||||
@@ -180,4 +403,27 @@ pub async fn build_buy_instructions(
|
||||
));
|
||||
|
||||
Ok(instructions)
|
||||
}
|
||||
|
||||
/// 验证地址表是否被成功用于编译后的消息中
|
||||
fn verify_lookup_table_usage(
|
||||
v0_message: &v0::Message,
|
||||
address_lookup_table_accounts: &[AddressLookupTableAccount],
|
||||
) {
|
||||
if !address_lookup_table_accounts.is_empty() {
|
||||
println!("消息已编译,使用了地址表引用");
|
||||
// 如果地址表有地址,但没有被使用,给出警告
|
||||
if v0_message.address_table_lookups.is_empty() {
|
||||
// println!("警告:编译后的消息没有使用地址表引用!");
|
||||
} else {
|
||||
for (i, lookup) in v0_message.address_table_lookups.iter().enumerate() {
|
||||
println!(
|
||||
"使用地址表 {}: 可写索引 {} 个, 只读索引 {} 个",
|
||||
i,
|
||||
lookup.writable_indexes.len(),
|
||||
lookup.readonly_indexes.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+90
-12
@@ -1,13 +1,13 @@
|
||||
use anyhow::anyhow;
|
||||
use spl_token::state::Account;
|
||||
use borsh::BorshDeserialize;
|
||||
use spl_token::instruction::close_account;
|
||||
use tokio::sync::RwLock;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use solana_sdk::{
|
||||
commitment_config::CommitmentConfig, compute_budget::ComputeBudgetInstruction, instruction::Instruction, program_pack::Pack, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::Transaction
|
||||
compute_budget::ComputeBudgetInstruction, instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::Transaction
|
||||
};
|
||||
use spl_associated_token_account::get_associated_token_address;
|
||||
use crate::{accounts::{self, BondingCurveAccount}, common::{logs_data::TradeInfo, PriorityFee, SolanaRpcClient}, constants::{self, global_constants::{CREATOR_FEE, FEE_BASIS_POINTS}, trade::DEFAULT_SLIPPAGE}};
|
||||
use borsh::BorshDeserialize;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref ACCOUNT_CACHE: RwLock<HashMap<Pubkey, Arc<accounts::GlobalAccount>>> = RwLock::new(HashMap::new());
|
||||
@@ -43,6 +43,53 @@ pub async fn transfer_sol(rpc: &SolanaRpcClient, payer: &Keypair, receive_wallet
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 关闭代币账户
|
||||
///
|
||||
/// 此函数用于关闭指定代币的关联代币账户,将账户中的代币余额转移给账户所有者。
|
||||
///
|
||||
/// # 参数
|
||||
///
|
||||
/// * `rpc` - Solana RPC客户端
|
||||
/// * `payer` - 支付交易费用的账户
|
||||
/// * `mint` - 代币的Mint地址
|
||||
///
|
||||
/// # 返回值
|
||||
///
|
||||
/// 返回一个Result,成功时返回(),失败时返回错误
|
||||
pub async fn close_token_account(rpc: &SolanaRpcClient, payer: &Keypair, mint: &Pubkey) -> Result<(), anyhow::Error> {
|
||||
// 获取关联代币账户地址
|
||||
let ata = get_associated_token_address(&payer.pubkey(), mint);
|
||||
|
||||
// 检查账户是否存在
|
||||
let account_exists = rpc.get_account(&ata).await.is_ok();
|
||||
if !account_exists {
|
||||
return Ok(()); // 如果账户不存在,直接返回成功
|
||||
}
|
||||
|
||||
// 构建关闭账户指令
|
||||
let close_account_ix = close_account(
|
||||
&spl_token::ID,
|
||||
&ata,
|
||||
&payer.pubkey(),
|
||||
&payer.pubkey(),
|
||||
&[&payer.pubkey()],
|
||||
)?;
|
||||
|
||||
// 构建交易
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
let transaction = Transaction::new_signed_with_payer(
|
||||
&[close_account_ix],
|
||||
Some(&payer.pubkey()),
|
||||
&[payer],
|
||||
recent_blockhash,
|
||||
);
|
||||
|
||||
// 发送交易
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn create_priority_fee_instructions(priority_fee: PriorityFee) -> Vec<Instruction> {
|
||||
let mut instructions = Vec::with_capacity(2);
|
||||
@@ -137,17 +184,19 @@ pub fn get_metadata_pda(mint: &Pubkey) -> Pubkey {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub async fn get_global_account(rpc: &SolanaRpcClient) -> Result<Arc<accounts::GlobalAccount>, anyhow::Error> {
|
||||
let global = get_global_pda();
|
||||
if let Some(account) = ACCOUNT_CACHE.read().await.get(&global) {
|
||||
return Ok(account.clone());
|
||||
}
|
||||
pub async fn get_global_account(/*rpc: &SolanaRpcClient*/) -> Result<Arc<accounts::GlobalAccount>, anyhow::Error> {
|
||||
// let global = constants::global_constants::GLOBAL_ACCOUNT;
|
||||
// if let Some(account) = ACCOUNT_CACHE.read().await.get(&global) {
|
||||
// return Ok(account.clone());
|
||||
// }
|
||||
|
||||
let account = rpc.get_account(&global).await?;
|
||||
let global_account = bincode::deserialize::<accounts::GlobalAccount>(&account.data)?;
|
||||
let global_account = accounts::GlobalAccount::new();
|
||||
|
||||
// let account = rpc.get_account(&global).await?;
|
||||
// let global_account = bincode::deserialize::<accounts::GlobalAccount>(&account.data)?;
|
||||
let global_account = Arc::new(global_account);
|
||||
|
||||
ACCOUNT_CACHE.write().await.insert(global, global_account.clone());
|
||||
// ACCOUNT_CACHE.write().await.insert(global, global_account.clone());
|
||||
Ok(global_account)
|
||||
}
|
||||
|
||||
@@ -175,9 +224,25 @@ pub async fn get_bonding_curve_account(
|
||||
Ok((bonding_curve, bonding_curve_pda))
|
||||
}
|
||||
|
||||
// #[inline]
|
||||
// pub fn get_buy_token_amount(
|
||||
// mint: &Pubkey,
|
||||
// dev_buy_token: u64,
|
||||
// dev_cost_sol: u64,
|
||||
// bot_cost_sol: u64,
|
||||
// slippage_basis_points: Option<u64>,
|
||||
// ) -> anyhow::Result<(u64, u64)> {
|
||||
// let bonding_curve_account = BondingCurveAccount::new(mint, dev_buy_token, dev_cost_sol);
|
||||
// let buy_token = bonding_curve_account.get_buy_price(bot_cost_sol).map_err(|e| anyhow!(e))?;
|
||||
|
||||
// let max_sol_cost = calculate_with_slippage_buy(bot_cost_sol, slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE));
|
||||
|
||||
// Ok((buy_token, max_sol_cost))
|
||||
// }
|
||||
|
||||
#[inline]
|
||||
pub fn get_buy_token_amount(
|
||||
bonding_curve_account: &Arc<accounts::BondingCurveAccount>,
|
||||
bonding_curve_account: &BondingCurveAccount,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
) -> anyhow::Result<(u64, u64)> {
|
||||
@@ -231,6 +296,19 @@ pub fn get_buy_token_amount_from_sol_amount(
|
||||
tokens_received.min(real_token_reserves) as u64
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub async fn init_bonding_curve_account(
|
||||
mint: &Pubkey,
|
||||
dev_buy_token: u64,
|
||||
dev_sol_cost: u64,
|
||||
creator: Pubkey,
|
||||
) -> Result<Arc<BondingCurveAccount>, anyhow::Error> {
|
||||
let bonding_curve = BondingCurveAccount::new(mint, dev_buy_token, dev_sol_cost, creator);
|
||||
let bonding_curve = Arc::new(bonding_curve);
|
||||
Ok(bonding_curve)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_buy_amount_with_slippage(amount_sol: u64, slippage_basis_points: Option<u64>) -> u64 {
|
||||
let slippage = slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE);
|
||||
|
||||
Executable
+238
@@ -0,0 +1,238 @@
|
||||
use std::{str::FromStr, time::Instant, sync::Arc};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::{
|
||||
compute_budget::ComputeBudgetInstruction,
|
||||
instruction::Instruction, message::{v0, VersionedMessage},
|
||||
pubkey::Pubkey,
|
||||
native_token::sol_to_lamports,
|
||||
signature::Keypair,
|
||||
signer::Signer,
|
||||
system_instruction,
|
||||
transaction::{Transaction, VersionedTransaction}
|
||||
};
|
||||
use spl_associated_token_account::instruction::create_associated_token_account;
|
||||
|
||||
use crate::{
|
||||
common::{PriorityFee, SolanaRpcClient}, constants, instruction,
|
||||
ipfs::TokenMetadataIPFS, swqos::{FeeClient, TradeType},
|
||||
};
|
||||
|
||||
use crate::pumpfun::common::{
|
||||
create_priority_fee_instructions,
|
||||
get_buy_amount_with_slippage, get_global_account
|
||||
};
|
||||
|
||||
use crate::common::tip_cache::TipCache;
|
||||
|
||||
use super::common::{get_bonding_curve_account, get_buy_token_amount, get_creator_vault_pda};
|
||||
|
||||
/// Create a new token
|
||||
pub async fn create(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Keypair,
|
||||
ipfs: TokenMetadataIPFS,
|
||||
priority_fee: PriorityFee,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let mut instructions = create_priority_fee_instructions(priority_fee);
|
||||
|
||||
instructions.push(instruction::create(
|
||||
payer.as_ref(),
|
||||
&mint,
|
||||
instruction::Create {
|
||||
_name: ipfs.metadata.name,
|
||||
_symbol: ipfs.metadata.symbol,
|
||||
_uri: ipfs.metadata_uri,
|
||||
_creator: payer.pubkey(),
|
||||
},
|
||||
));
|
||||
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
let transaction = Transaction::new_signed_with_payer(
|
||||
&instructions,
|
||||
Some(&payer.pubkey()),
|
||||
&[payer.as_ref(), &mint],
|
||||
recent_blockhash,
|
||||
);
|
||||
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create and buy tokens in one transaction
|
||||
pub async fn create_and_buy(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Keypair,
|
||||
ipfs: TokenMetadataIPFS,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
if buy_sol_cost == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
let mint = Arc::new(mint);
|
||||
let transaction = build_create_and_buy_transaction(rpc.clone(), payer.clone(), mint.clone(), ipfs, buy_sol_cost, slippage_basis_points, priority_fee.clone(), recent_blockhash).await?;
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_and_buy_with_tip(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
fee_clients: Vec<Arc<FeeClient>>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Keypair,
|
||||
ipfs: TokenMetadataIPFS,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let start_time = Instant::now();
|
||||
let mint = Arc::new(mint);
|
||||
let build_instructions = build_create_and_buy_instructions(rpc.clone(), payer.clone(), mint.clone(), ipfs.clone(), buy_sol_cost, slippage_basis_points).await?;
|
||||
let mut handles = vec![];
|
||||
for fee_client in fee_clients {
|
||||
let tip_account = fee_client.get_tip_account()?;
|
||||
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
let transaction = build_create_and_buy_transaction_with_tip(/*rpc.clone(),*/ tip_account, payer.clone(), priority_fee.clone(), build_instructions.clone(), recent_blockhash).await?;
|
||||
let handle = tokio::spawn(async move {
|
||||
fee_client.send_transaction(TradeType::CreateAndBuy, &transaction).await.map_err(|e| anyhow!(e.to_string()))?;
|
||||
println!("Total Jito create and buy operation time: {:?}ms", start_time.elapsed().as_millis());
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(Ok(_)) => (),
|
||||
Ok(Err(e)) => println!("Error in task: {}", e),
|
||||
Err(e) => println!("Task join error: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn build_create_and_buy_transaction(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Arc<Keypair>,
|
||||
ipfs: TokenMetadataIPFS,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
priority_fee: PriorityFee,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<Transaction, anyhow::Error> {
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
];
|
||||
|
||||
let build_instructions = build_create_and_buy_instructions(rpc.clone(), payer.clone(), mint.clone(), ipfs, buy_sol_cost, slippage_basis_points).await?;
|
||||
instructions.extend(build_instructions);
|
||||
|
||||
// let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
// let recent_blockhash = Hash::default();
|
||||
let transaction = Transaction::new_signed_with_payer(
|
||||
&instructions,
|
||||
Some(&payer.pubkey()),
|
||||
&[payer.as_ref(), mint.as_ref()],
|
||||
recent_blockhash,
|
||||
);
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn build_create_and_buy_transaction_with_tip(
|
||||
// rpc: Arc<SolanaRpcClient>,
|
||||
tip_account: Arc<Pubkey>,
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: PriorityFee,
|
||||
build_instructions: Vec<Instruction>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let tip_cache = TipCache::get_instance();
|
||||
let tip_amount = tip_cache.get_tip();
|
||||
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
system_instruction::transfer(
|
||||
&payer.pubkey(),
|
||||
&tip_account,
|
||||
sol_to_lamports(tip_amount),
|
||||
),
|
||||
];
|
||||
instructions.extend(build_instructions);
|
||||
|
||||
// let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
// let recent_blockhash = Hash::default();
|
||||
let v0_message: v0::Message =
|
||||
v0::Message::try_compile(&payer.pubkey(), &instructions, &[], recent_blockhash)?;
|
||||
|
||||
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
|
||||
let transaction = VersionedTransaction::try_new(versioned_message, &[&payer])?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn build_create_and_buy_instructions(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Arc<Keypair>,
|
||||
ipfs: TokenMetadataIPFS,
|
||||
buy_sol_cost: u64,
|
||||
slippage_basis_points: Option<u64>,
|
||||
) -> Result<Vec<Instruction>, anyhow::Error> {
|
||||
if buy_sol_cost == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
let (bonding_curve_account, bonding_curve_pda) = get_bonding_curve_account(&rpc, &mint.pubkey()).await?;
|
||||
let creator_vault_pda = get_creator_vault_pda(&bonding_curve_account.creator).unwrap();
|
||||
let (buy_token_amount, max_sol_cost) = get_buy_token_amount(&bonding_curve_account, buy_sol_cost, slippage_basis_points)?;
|
||||
|
||||
let mut instructions = vec![];
|
||||
|
||||
instructions.push(instruction::create(
|
||||
payer.as_ref(),
|
||||
mint.as_ref(),
|
||||
instruction::Create {
|
||||
_name: ipfs.metadata.name.clone(),
|
||||
_symbol: ipfs.metadata.symbol.clone(),
|
||||
_uri: ipfs.metadata_uri.clone(),
|
||||
_creator: payer.pubkey(),
|
||||
},
|
||||
));
|
||||
|
||||
instructions.push(create_associated_token_account(
|
||||
&payer.pubkey(),
|
||||
&payer.pubkey(),
|
||||
&mint.pubkey(),
|
||||
&constants::accounts::TOKEN_PROGRAM,
|
||||
));
|
||||
|
||||
instructions.push(instruction::buy(
|
||||
payer.as_ref(),
|
||||
&mint.pubkey(),
|
||||
&bonding_curve_pda,
|
||||
&creator_vault_pda,
|
||||
&constants::global_constants::FEE_RECIPIENT,
|
||||
instruction::Buy {
|
||||
_amount: buy_token_amount,
|
||||
_max_sol_cost: max_sol_cost,
|
||||
},
|
||||
));
|
||||
|
||||
Ok(instructions)
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod buy;
|
||||
pub mod create;
|
||||
pub mod sell;
|
||||
pub mod common;
|
||||
+123
-82
@@ -1,42 +1,38 @@
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::{
|
||||
compute_budget::ComputeBudgetInstruction, instruction::Instruction, message::{v0, VersionedMessage}, native_token::sol_to_lamports, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::{Transaction, VersionedTransaction}
|
||||
compute_budget::ComputeBudgetInstruction, instruction::Instruction, message::{v0, VersionedMessage}, native_token::sol_to_lamports, pubkey::Pubkey, signature::{Keypair}, signer::Signer, system_instruction, transaction::{VersionedTransaction}
|
||||
};
|
||||
use solana_hash::Hash;
|
||||
use spl_associated_token_account::get_associated_token_address;
|
||||
use spl_token::instruction::close_account;
|
||||
use tokio::task::JoinHandle;
|
||||
use std::{str::FromStr, sync::Arc, time::Instant};
|
||||
|
||||
use std::{str::FromStr, time::Instant, sync::Arc};
|
||||
use crate::{common::{address_lookup_cache::get_address_lookup_table_account, PriorityFee, SolanaRpcClient}, constants::{global_constants::FEE_RECIPIENT}, instruction, swqos::{FeeClient, TradeType, ClientType}};
|
||||
|
||||
use crate::{common::{PriorityFee, SolanaRpcClient}, instruction, swqos::FeeClient};
|
||||
|
||||
use super::common::{get_bonding_curve_account, get_creator_vault_pda, get_global_account};
|
||||
|
||||
async fn get_token_balance(rpc: &SolanaRpcClient, payer: &Keypair, mint: &Pubkey) -> Result<(u64, Pubkey), anyhow::Error> {
|
||||
let ata = get_associated_token_address(&payer.pubkey(), 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"))?;
|
||||
|
||||
if balance_u64 == 0 {
|
||||
return Err(anyhow!("Balance is 0"));
|
||||
}
|
||||
|
||||
Ok((balance_u64, ata))
|
||||
}
|
||||
use super::common::get_creator_vault_pda;
|
||||
|
||||
pub async fn sell(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_token: Option<u64>,
|
||||
creator: Pubkey,
|
||||
amount_token: u64,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let instructions = build_sell_instructions(rpc.clone(), payer.clone(), mint.clone(), amount_token).await?;
|
||||
let transaction = build_sell_transaction(rpc.clone(), payer.clone(), priority_fee, instructions).await?;
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
let start_time = Instant::now();
|
||||
let instructions = build_sell_instructions(payer.clone(), mint.clone(), creator, amount_token).await?;
|
||||
println!(" 卖出交易指令: {:?}", start_time.elapsed());
|
||||
|
||||
let start_time = Instant::now();
|
||||
let transaction = build_sell_transaction(payer.clone(), priority_fee, instructions, lookup_table_key, recent_blockhash).await?;
|
||||
println!(" 卖出交易签名: {:?}", start_time.elapsed());
|
||||
|
||||
let start_time = Instant::now();
|
||||
rpc.send_and_confirm_transaction(&transaction).await?;
|
||||
println!(" 卖出交易确认: {:?}", start_time.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -45,73 +41,101 @@ pub async fn sell_by_percent(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
creator: Pubkey,
|
||||
percent: u64,
|
||||
amount_token: u64,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
if percent == 0 || percent > 100 {
|
||||
return Err(anyhow!("Percentage must be between 1 and 100"));
|
||||
}
|
||||
|
||||
let (balance_u64, _) = get_token_balance(rpc.as_ref(), payer.as_ref(), &mint).await?;
|
||||
let amount = balance_u64 * percent / 100;
|
||||
sell(rpc, payer, mint, Some(amount), priority_fee).await
|
||||
let amount = amount_token * percent / 100;
|
||||
sell(rpc, payer, mint, creator, amount, priority_fee, lookup_table_key, recent_blockhash).await
|
||||
}
|
||||
|
||||
pub async fn sell_by_percent_with_tip(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
fee_clients: Vec<Arc<FeeClient>>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
creator: Pubkey,
|
||||
percent: u64,
|
||||
amount_token: u64,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
if percent == 0 || percent > 100 {
|
||||
return Err(anyhow!("Percentage must be between 1 and 100"));
|
||||
}
|
||||
|
||||
let (balance_u64, _) = get_token_balance(rpc.as_ref(), payer.as_ref(), &mint).await?;
|
||||
let amount = balance_u64 * percent / 100;
|
||||
sell_with_tip(rpc, fee_clients, payer, mint, Some(amount), priority_fee).await
|
||||
let amount = amount_token * percent / 100;
|
||||
sell_with_tip(fee_clients, payer, mint, creator, amount, priority_fee, lookup_table_key, recent_blockhash).await
|
||||
}
|
||||
|
||||
/// Sell tokens using Jito
|
||||
pub async fn sell_with_tip(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
fee_clients: Vec<Arc<FeeClient>>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_token: Option<u64>,
|
||||
creator: Pubkey,
|
||||
amount_token: u64,
|
||||
priority_fee: PriorityFee,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
recent_blockhash: Hash,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let start_time = Instant::now();
|
||||
let mint = Arc::new(mint.clone());
|
||||
let instructions = build_sell_instructions(payer.clone(), *mint, creator, amount_token).await?;
|
||||
println!(" 卖出交易指令: {:?}", start_time.elapsed());
|
||||
|
||||
let mut transactions = vec![];
|
||||
let instructions = build_sell_instructions(rpc.clone(), payer.clone(), mint.clone(), amount_token).await?;
|
||||
let start_time = Instant::now();
|
||||
let cores = core_affinity::get_core_ids().unwrap();
|
||||
let mut handles: Vec<JoinHandle<Result<(), anyhow::Error>>> = vec![];
|
||||
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
for fee_client in fee_clients.clone() {
|
||||
let payer = payer.clone();
|
||||
let priority_fee = priority_fee.clone();
|
||||
let tip_account = fee_client.get_tip_account().await.map_err(|e| anyhow!(e.to_string()))?;
|
||||
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
|
||||
let transaction = build_sell_transaction_with_tip(tip_account, payer, priority_fee, instructions.clone(), recent_blockhash).await?;
|
||||
transactions.push(transaction);
|
||||
}
|
||||
|
||||
let mut handles = vec![];
|
||||
for i in 0..fee_clients.len() {
|
||||
let fee_client = fee_clients[i].clone();
|
||||
let transaction = transactions[i].clone();
|
||||
let handle: JoinHandle<Result<(), anyhow::Error>> = tokio::spawn(async move {
|
||||
fee_client.send_transaction(&transaction).await?;
|
||||
println!("index: {}, Total Jito sell operation time: {:?}ms", i, start_time.elapsed().as_millis());
|
||||
Ok(())
|
||||
let payer = payer.clone();
|
||||
let instructions = instructions.clone();
|
||||
let priority_fee = priority_fee.clone();
|
||||
let core_id = cores[i % cores.len()];
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
core_affinity::set_for_current(core_id);
|
||||
|
||||
let transaction = if fee_client.get_client_type() == ClientType::Rpc {
|
||||
build_sell_transaction(
|
||||
payer.clone(),
|
||||
priority_fee.clone(),
|
||||
instructions.clone(),
|
||||
lookup_table_key,
|
||||
recent_blockhash
|
||||
).await?
|
||||
} else {
|
||||
let tip_account = fee_client.get_tip_account()?;
|
||||
let tip_account = Arc::new(Pubkey::from_str(&tip_account).map_err(|e| anyhow!(e))?);
|
||||
|
||||
build_sell_transaction_with_tip(
|
||||
tip_account,
|
||||
payer.clone(),
|
||||
priority_fee.clone(),
|
||||
instructions.clone(),
|
||||
lookup_table_key,
|
||||
recent_blockhash
|
||||
).await?
|
||||
};
|
||||
|
||||
fee_client.send_transaction(TradeType::Sell, &transaction).await?;
|
||||
Ok::<(), anyhow::Error>(())
|
||||
});
|
||||
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
println!(" 卖出交易签名: {:?}", start_time.elapsed());
|
||||
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(Ok(_)) => (),
|
||||
@@ -120,16 +144,16 @@ pub async fn sell_with_tip(
|
||||
}
|
||||
}
|
||||
|
||||
println!("Total Jito sell operation time: {:?}ms", start_time.elapsed().as_millis());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn build_sell_transaction(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: PriorityFee,
|
||||
build_instructions: Vec<Instruction>
|
||||
) -> Result<Transaction, anyhow::Error> {
|
||||
build_instructions: Vec<Instruction>,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
blockhash: Hash
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
@@ -137,13 +161,21 @@ pub async fn build_sell_transaction(
|
||||
|
||||
instructions.extend(build_instructions);
|
||||
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
let transaction = Transaction::new_signed_with_payer(
|
||||
&instructions,
|
||||
Some(&payer.pubkey()),
|
||||
&[payer.as_ref()],
|
||||
recent_blockhash,
|
||||
);
|
||||
let mut address_lookup_table_accounts = vec![];
|
||||
if let Some(lookup_table_key) = lookup_table_key {
|
||||
let account = get_address_lookup_table_account(&lookup_table_key).await;
|
||||
address_lookup_table_accounts.push(account);
|
||||
}
|
||||
|
||||
let transaction = VersionedTransaction::try_new(
|
||||
VersionedMessage::V0(v0::Message::try_compile(
|
||||
&payer.pubkey(),
|
||||
&instructions,
|
||||
&address_lookup_table_accounts,
|
||||
blockhash,
|
||||
)?),
|
||||
&[payer],
|
||||
)?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
@@ -153,56 +185,65 @@ pub async fn build_sell_transaction_with_tip(
|
||||
payer: Arc<Keypair>,
|
||||
priority_fee: PriorityFee,
|
||||
build_instructions: Vec<Instruction>,
|
||||
lookup_table_key: Option<Pubkey>,
|
||||
blockhash: Hash,
|
||||
) -> Result<VersionedTransaction, anyhow::Error> {
|
||||
let mut instructions = vec![
|
||||
ComputeBudgetInstruction::set_compute_unit_price(priority_fee.unit_price),
|
||||
ComputeBudgetInstruction::set_compute_unit_limit(priority_fee.unit_limit),
|
||||
];
|
||||
|
||||
instructions.extend(build_instructions);
|
||||
|
||||
instructions.push(
|
||||
system_instruction::transfer(
|
||||
&payer.pubkey(),
|
||||
&tip_account,
|
||||
sol_to_lamports(priority_fee.sell_tip_fee),
|
||||
),
|
||||
];
|
||||
);
|
||||
|
||||
instructions.extend(build_instructions);
|
||||
let mut address_lookup_table_accounts = vec![];
|
||||
if let Some(lookup_table_key) = lookup_table_key {
|
||||
let account = get_address_lookup_table_account(&lookup_table_key).await;
|
||||
address_lookup_table_accounts.push(account);
|
||||
}
|
||||
|
||||
let v0_message: v0::Message =
|
||||
v0::Message::try_compile(&payer.pubkey(), &instructions, &[], blockhash)?;
|
||||
let versioned_message: VersionedMessage = VersionedMessage::V0(v0_message);
|
||||
|
||||
let transaction = VersionedTransaction::try_new(versioned_message, &[&payer])?;
|
||||
let transaction = VersionedTransaction::try_new(
|
||||
VersionedMessage::V0(v0::Message::try_compile(
|
||||
&payer.pubkey(),
|
||||
&instructions,
|
||||
&address_lookup_table_accounts,
|
||||
blockhash,
|
||||
)?),
|
||||
&[payer],
|
||||
)?;
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
pub async fn build_sell_instructions(
|
||||
rpc: Arc<SolanaRpcClient>,
|
||||
payer: Arc<Keypair>,
|
||||
mint: Pubkey,
|
||||
amount_token: Option<u64>,
|
||||
creator: Pubkey,
|
||||
amount_token: u64,
|
||||
) -> Result<Vec<Instruction>, anyhow::Error> {
|
||||
let (balance_u64, ata) = get_token_balance(rpc.as_ref(), payer.as_ref(), &mint).await?;
|
||||
let amount = amount_token.unwrap_or(balance_u64);
|
||||
|
||||
if amount == 0 {
|
||||
if amount_token == 0 {
|
||||
return Err(anyhow!("Amount cannot be zero"));
|
||||
}
|
||||
|
||||
let global_account = get_global_account(rpc.as_ref()).await?;
|
||||
let (bonding_curve_account, bonding_curve_pda) = get_bonding_curve_account(&rpc, &mint).await?;
|
||||
let creator_vault_pda = get_creator_vault_pda(&bonding_curve_account.creator).unwrap();
|
||||
|
||||
let creator_vault_pda = get_creator_vault_pda(&creator).unwrap();
|
||||
let ata = get_associated_token_address(&payer.pubkey(), &mint);
|
||||
|
||||
let instructions = vec![
|
||||
instruction::sell(
|
||||
payer.as_ref(),
|
||||
&mint,
|
||||
&bonding_curve_pda,
|
||||
&creator_vault_pda,
|
||||
&global_account.fee_recipient,
|
||||
&FEE_RECIPIENT,
|
||||
instruction::Sell {
|
||||
_amount: amount,
|
||||
_min_sol_output: 0,
|
||||
_amount: amount_token,
|
||||
_min_sol_output: 1,
|
||||
},
|
||||
),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user