change priority_fee

This commit is contained in:
William
2025-02-21 22:32:43 +08:00
parent 8df2841b17
commit ecb2ffa747
6 changed files with 87 additions and 95 deletions
+2 -2
View File
@@ -55,6 +55,6 @@ pub mod accounts {
pub mod trade { pub mod trade {
pub const JITO_TIP_AMOUNT: f64 = 0.0001; pub const JITO_TIP_AMOUNT: f64 = 0.0001;
pub const DEFAULT_SLIPPAGE: u64 = 3000; // 30% pub const DEFAULT_SLIPPAGE: u64 = 3000; // 30%
pub const DEFAULT_COMPUTE_UNIT_LIMIT: u32 = 78000; pub const DEFAULT_COMPUTE_UNIT_LIMIT: f64 = 78000.0;
pub const DEFAULT_COMPUTE_UNIT_PRICE: u64 = 500000; pub const DEFAULT_COMPUTE_UNIT_PRICE: f64 = 500000.0;
} }
+18 -18
View File
@@ -71,7 +71,7 @@ impl PumpFun {
&self, &self,
mint: &Keypair, mint: &Keypair,
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::create::create( trade::create::create(
&self.rpc, &self.rpc,
@@ -88,7 +88,7 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::create::create_and_buy( trade::create::create_and_buy(
&self.rpc, &self.rpc,
@@ -108,7 +108,7 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
trade::create::create_and_buy_list_with_jito( trade::create::create_and_buy_list_with_jito(
&self.rpc, &self.rpc,
@@ -118,7 +118,7 @@ impl PumpFun {
ipfs, ipfs,
amount_sols, amount_sols,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
@@ -129,7 +129,7 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
trade::create::create_and_buy_with_jito( trade::create::create_and_buy_with_jito(
&self.rpc, &self.rpc,
@@ -139,7 +139,7 @@ impl PumpFun {
ipfs, ipfs,
amount_sol, amount_sol,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
/// Buy tokens /// Buy tokens
@@ -148,7 +148,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::buy::buy( trade::buy::buy(
&self.rpc, &self.rpc,
@@ -166,7 +166,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
trade::buy::buy_with_jito( trade::buy::buy_with_jito(
&self.rpc, &self.rpc,
@@ -175,7 +175,7 @@ impl PumpFun {
mint, mint,
amount_sol, amount_sol,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
@@ -185,7 +185,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
trade::buy::buy_list_with_jito( trade::buy::buy_list_with_jito(
&self.rpc, &self.rpc,
@@ -194,7 +194,7 @@ impl PumpFun {
mint, mint,
amount_sols, amount_sols,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
@@ -204,7 +204,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::sell::sell( trade::sell::sell(
&self.rpc, &self.rpc,
@@ -222,7 +222,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::sell::sell_by_percent( trade::sell::sell_by_percent(
&self.rpc, &self.rpc,
@@ -239,7 +239,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
trade::sell::sell_by_percent_with_jito( trade::sell::sell_by_percent_with_jito(
&self.rpc, &self.rpc,
@@ -248,8 +248,8 @@ impl PumpFun {
mint, mint,
percent, percent,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
/// Sell tokens using Jito /// Sell tokens using Jito
@@ -258,7 +258,7 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let jito_client = self.jito_client.as_ref() let jito_client = self.jito_client.as_ref()
.ok_or_else(|| anyhow!("Jito client not found"))?; .ok_or_else(|| anyhow!("Jito client not found"))?;
@@ -270,7 +270,7 @@ impl PumpFun {
mint, mint,
amount_token, amount_token,
slippage_basis_points, slippage_basis_points,
jito_fee, priority_fee,
).await ).await
} }
+18 -20
View File
@@ -19,7 +19,7 @@ pub async fn buy(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
let transaction = build_buy_transaction(rpc, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?; let transaction = build_buy_transaction(rpc, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?;
let signature = rpc.send_transaction(&transaction)?; let signature = rpc.send_transaction(&transaction)?;
@@ -34,11 +34,11 @@ pub async fn buy_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let start_time = Instant::now(); let start_time = Instant::now();
let transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, mint, amount_sol, slippage_basis_points, jito_fee).await?; let transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?;
let signature = jito_client.send_transaction(&transaction).await?; let signature = jito_client.send_transaction(&transaction).await?;
println!("Total Jito buy operation time: {:?}ms", start_time.elapsed().as_millis()); println!("Total Jito buy operation time: {:?}ms", start_time.elapsed().as_millis());
@@ -53,13 +53,13 @@ pub async fn buy_list_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let start_time = Instant::now(); let start_time = Instant::now();
let mut transactions = vec![]; let mut transactions = vec![];
for (i, payer) in payers.iter().enumerate() { for (i, payer) in payers.iter().enumerate() {
let transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, mint, amount_sols[i], slippage_basis_points, jito_fee).await?; let transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, mint, amount_sols[i], slippage_basis_points, priority_fee).await?;
transactions.push(transaction); transactions.push(transaction);
} }
@@ -76,7 +76,7 @@ pub async fn build_buy_transaction(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_buy_instructions(rpc, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?; let instructions = build_buy_instructions(rpc, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?;
@@ -98,9 +98,9 @@ pub async fn build_buy_transaction_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_buy_instructions_with_jito(rpc, jito_client, payer, mint, amount_sol, slippage_basis_points, jito_fee).await?; let instructions = build_buy_instructions_with_jito(rpc, jito_client, payer, mint, amount_sol, slippage_basis_points, priority_fee).await?;
let recent_blockhash = rpc.get_latest_blockhash()?; let recent_blockhash = rpc.get_latest_blockhash()?;
let transaction = Transaction::new_signed_with_payer( let transaction = Transaction::new_signed_with_payer(
&instructions, &instructions,
@@ -118,7 +118,7 @@ pub async fn build_buy_instructions(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
if amount_sol == 0 { if amount_sol == 0 {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
@@ -183,20 +183,14 @@ pub async fn build_buy_instructions(
let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?; let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?;
let fees = rpc.get_recent_prioritization_fees(&[])?; let fees = rpc.get_recent_prioritization_fees(&[])?;
let average_fees = if fees.is_empty() { let average_fees = if fees.is_empty() {
DEFAULT_COMPUTE_UNIT_PRICE sol_to_lamports(DEFAULT_COMPUTE_UNIT_PRICE)
} else { } else {
fees.iter() fees.iter()
.map(|fee| fee.prioritization_fee) .map(|fee| fee.prioritization_fee)
.sum::<u64>() / fees.len() as u64 .sum::<u64>() / fees.len() as u64
}; };
let unit_price = match priority_fee { let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees };
None => average_fees,
Some(pf) => pf.price.unwrap_or(DEFAULT_COMPUTE_UNIT_PRICE)
};
let unit_price = if unit_price == 0 { DEFAULT_COMPUTE_UNIT_PRICE } else { unit_price };
instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32); instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32);
instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price); instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price);
@@ -211,7 +205,7 @@ pub async fn build_buy_instructions_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
if amount_sol == 0 { if amount_sol == 0 {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
@@ -228,7 +222,11 @@ pub async fn build_buy_instructions_with_jito(
let buy_amount_with_slippage = calculate_with_slippage_buy(amount_sol, slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE)); let buy_amount_with_slippage = calculate_with_slippage_buy(amount_sol, slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE));
let mut instructions = vec![]; let mut instructions = vec![
ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32),
];
let ata = get_associated_token_address(&payer.pubkey(), mint); let ata = get_associated_token_address(&payer.pubkey(), mint);
if rpc.get_account(&ata).is_err() { if rpc.get_account(&ata).is_err() {
instructions.push(create_associated_token_account( instructions.push(create_associated_token_account(
@@ -251,7 +249,7 @@ pub async fn build_buy_instructions_with_jito(
let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?; let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?;
let jito_fee = jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT);
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),
+9 -13
View File
@@ -3,7 +3,7 @@ use tokio::sync::RwLock;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use solana_client::rpc_client::RpcClient; use solana_client::rpc_client::RpcClient;
use solana_sdk::{ use solana_sdk::{
compute_budget::ComputeBudgetInstruction, instruction::Instruction, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::Transaction compute_budget::ComputeBudgetInstruction, instruction::Instruction, native_token::sol_to_lamports, pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction, transaction::Transaction
}; };
use spl_associated_token_account::get_associated_token_address; use spl_associated_token_account::get_associated_token_address;
use crate::{accounts, common::logs_data::TradeInfo, constants::{self, trade::{DEFAULT_COMPUTE_UNIT_LIMIT, DEFAULT_COMPUTE_UNIT_PRICE, DEFAULT_SLIPPAGE}}}; use crate::{accounts, common::logs_data::TradeInfo, constants::{self, trade::{DEFAULT_COMPUTE_UNIT_LIMIT, DEFAULT_COMPUTE_UNIT_PRICE, DEFAULT_SLIPPAGE}}};
@@ -13,15 +13,16 @@ lazy_static::lazy_static! {
static ref ACCOUNT_CACHE: RwLock<HashMap<Pubkey, Arc<accounts::GlobalAccount>>> = RwLock::new(HashMap::new()); static ref ACCOUNT_CACHE: RwLock<HashMap<Pubkey, Arc<accounts::GlobalAccount>>> = RwLock::new(HashMap::new());
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct PriorityFee { pub struct PriorityFee {
pub limit: Option<u32>, pub limit: f64,
pub price: Option<u64>, pub price: f64,
pub jito_fee: Option<f64>,
} }
impl Default for PriorityFee { impl Default for PriorityFee {
fn default() -> Self { fn default() -> Self {
Self { limit: Some(DEFAULT_COMPUTE_UNIT_LIMIT), price: Some(DEFAULT_COMPUTE_UNIT_PRICE) } Self { limit: DEFAULT_COMPUTE_UNIT_LIMIT, price: DEFAULT_COMPUTE_UNIT_PRICE, jito_fee: None }
} }
} }
@@ -56,15 +57,10 @@ pub async fn transfer_sol(rpc: &RpcClient, payer: &Keypair, receive_wallet: &Pub
} }
#[inline] #[inline]
pub fn create_priority_fee_instructions(priority_fee: Option<PriorityFee>) -> Vec<Instruction> { pub fn create_priority_fee_instructions(priority_fee: PriorityFee) -> Vec<Instruction> {
let mut instructions = Vec::with_capacity(2); let mut instructions = Vec::with_capacity(2);
let fee = priority_fee.unwrap_or(PriorityFee::default()); instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32));
if let Some(limit) = fee.limit { instructions.push(ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price)));
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(limit));
}
if let Some(price) = fee.price {
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(price));
}
instructions instructions
} }
+21 -21
View File
@@ -10,7 +10,7 @@ use spl_associated_token_account::{
instruction::create_associated_token_account, instruction::create_associated_token_account,
}; };
use crate::{constants::{self, trade::{DEFAULT_COMPUTE_UNIT_PRICE, JITO_TIP_AMOUNT}}, instruction, ipfs::TokenMetadataIPFS, jito::JitoClient, trade::buy::build_buy_transaction_with_jito}; use crate::{constants::{self, trade::JITO_TIP_AMOUNT}, instruction, ipfs::TokenMetadataIPFS, jito::JitoClient, trade::buy::build_buy_transaction_with_jito};
use super::common::{create_priority_fee_instructions, get_buy_amount_with_slippage, get_global_account, PriorityFee}; use super::common::{create_priority_fee_instructions, get_buy_amount_with_slippage, get_global_account, PriorityFee};
@@ -20,7 +20,7 @@ pub async fn create(
payer: &Keypair, payer: &Keypair,
mint: &Keypair, mint: &Keypair,
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
let mut instructions = create_priority_fee_instructions(priority_fee); let mut instructions = create_priority_fee_instructions(priority_fee);
@@ -55,7 +55,7 @@ pub async fn create_and_buy(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
if amount_sol == 0 { if amount_sol == 0 {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
@@ -75,18 +75,18 @@ pub async fn create_and_buy_list_with_jito(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let start_time = Instant::now(); let start_time = Instant::now();
let mut transactions = Vec::new(); let mut transactions = Vec::new();
let transaction = build_create_and_buy_transaction_with_jito(rpc, jito_client, payers[0], mint, ipfs, amount_sols[0], slippage_basis_points, jito_fee).await?; let transaction = build_create_and_buy_transaction_with_jito(rpc, jito_client, payers[0], mint, ipfs, amount_sols[0], slippage_basis_points, priority_fee).await?;
transactions.push(transaction); transactions.push(transaction);
for (i, payer) in payers.iter().skip(1).enumerate() { for (i, payer) in payers.iter().skip(1).enumerate() {
println!("Creating and buying token index: {}", i); println!("Creating and buying token index: {}", i);
let buy_transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, &mint.pubkey(), amount_sols[i], slippage_basis_points, jito_fee).await?; let buy_transaction = build_buy_transaction_with_jito(rpc, jito_client, payer, &mint.pubkey(), amount_sols[i], slippage_basis_points, priority_fee).await?;
transactions.push(buy_transaction); transactions.push(buy_transaction);
} }
@@ -105,12 +105,12 @@ pub async fn create_and_buy_with_jito(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let start_time = Instant::now(); let start_time = Instant::now();
let transaction = build_create_and_buy_transaction_with_jito(rpc, jito_client, payer, mint, ipfs, amount_sol, slippage_basis_points, jito_fee).await?; let transaction = build_create_and_buy_transaction_with_jito(rpc, jito_client, payer, mint, ipfs, amount_sol, slippage_basis_points, priority_fee).await?;
let signature = jito_client.send_transaction(&transaction).await?; let signature = jito_client.send_transaction(&transaction).await?;
@@ -126,7 +126,7 @@ pub async fn build_create_and_buy_transaction(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_create_and_buy_instructions(rpc, payer, mint, ipfs, amount_sol, slippage_basis_points, priority_fee).await?; let instructions = build_create_and_buy_instructions(rpc, payer, mint, ipfs, amount_sol, slippage_basis_points, priority_fee).await?;
let recent_blockhash = rpc.get_latest_blockhash()?; let recent_blockhash = rpc.get_latest_blockhash()?;
@@ -148,9 +148,9 @@ pub async fn build_create_and_buy_transaction_with_jito(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_create_and_buy_instructions_with_jito(rpc, jito_client, payer, mint, ipfs, amount_sol, slippage_basis_points, jito_fee).await?; let instructions = build_create_and_buy_instructions_with_jito(rpc, jito_client, payer, mint, ipfs, amount_sol, slippage_basis_points, priority_fee).await?;
let recent_blockhash = rpc.get_latest_blockhash()?; let recent_blockhash = rpc.get_latest_blockhash()?;
let transaction = Transaction::new_signed_with_payer( let transaction = Transaction::new_signed_with_payer(
&instructions, &instructions,
@@ -169,7 +169,7 @@ pub async fn build_create_and_buy_instructions(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
if amount_sol == 0 { if amount_sol == 0 {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
@@ -242,19 +242,15 @@ pub async fn build_create_and_buy_instructions(
let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?; let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?;
let fees = rpc.get_recent_prioritization_fees(&[])?; let fees = rpc.get_recent_prioritization_fees(&[])?;
let average_fees = if fees.is_empty() { let average_fees = if fees.is_empty() {
DEFAULT_COMPUTE_UNIT_PRICE sol_to_lamports(priority_fee.price)
} else { } else {
fees.iter() fees.iter()
.map(|fee| fee.prioritization_fee) .map(|fee| fee.prioritization_fee)
.sum::<u64>() / fees.len() as u64 .sum::<u64>() / fees.len() as u64
}; };
let unit_price = match priority_fee {
None => average_fees,
Some(pf) => pf.price.unwrap_or(DEFAULT_COMPUTE_UNIT_PRICE)
};
let unit_price = if unit_price == 0 { DEFAULT_COMPUTE_UNIT_PRICE } else { unit_price }; let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees };
instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32); instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32);
instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price); instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price);
@@ -270,7 +266,7 @@ pub async fn build_create_and_buy_instructions_with_jito(
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
if amount_sol == 0 { if amount_sol == 0 {
return Err(anyhow!("Amount cannot be zero")); return Err(anyhow!("Amount cannot be zero"));
@@ -281,7 +277,11 @@ pub async fn build_create_and_buy_instructions_with_jito(
let buy_amount_with_slippage = let buy_amount_with_slippage =
get_buy_amount_with_slippage(amount_sol, slippage_basis_points); get_buy_amount_with_slippage(amount_sol, slippage_basis_points);
let mut instructions = vec![]; let mut instructions = vec![
ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32),
];
instructions.push(instruction::create( instructions.push(instruction::create(
payer, payer,
mint, mint,
@@ -313,7 +313,7 @@ pub async fn build_create_and_buy_instructions_with_jito(
)); ));
let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?; let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?;
let jito_fee = jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT);
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),
+19 -21
View File
@@ -31,7 +31,7 @@ pub async fn sell(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
let transaction = build_sell_transaction(rpc, payer, mint, amount_token, slippage_basis_points, priority_fee).await?; let transaction = build_sell_transaction(rpc, payer, mint, amount_token, slippage_basis_points, priority_fee).await?;
@@ -47,7 +47,7 @@ pub async fn sell_by_percent(
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
if percent == 0 || percent > 100 { if percent == 0 || percent > 100 {
return Err(anyhow!("Percentage must be between 1 and 100")); return Err(anyhow!("Percentage must be between 1 and 100"));
@@ -65,7 +65,7 @@ pub async fn sell_by_percent_with_jito(
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
if percent == 0 || percent > 100 { if percent == 0 || percent > 100 {
return Err(anyhow!("Percentage must be between 1 and 100")); return Err(anyhow!("Percentage must be between 1 and 100"));
@@ -73,7 +73,7 @@ pub async fn sell_by_percent_with_jito(
let (balance_u64, _) = get_token_balance(rpc, payer, mint).await?; let (balance_u64, _) = get_token_balance(rpc, payer, mint).await?;
let amount = balance_u64 * percent / 100; let amount = balance_u64 * percent / 100;
sell_with_jito(rpc, payer, jito_client, mint, Some(amount), slippage_basis_points, jito_fee).await sell_with_jito(rpc, payer, jito_client, mint, Some(amount), slippage_basis_points, priority_fee).await
} }
/// Sell tokens using Jito /// Sell tokens using Jito
@@ -84,11 +84,11 @@ pub async fn sell_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
let start_time = Instant::now(); let start_time = Instant::now();
let transaction = build_sell_transaction_with_jito(rpc, jito_client, payer, mint, amount_token, slippage_basis_points, jito_fee).await?; let transaction = build_sell_transaction_with_jito(rpc, jito_client, payer, mint, amount_token, slippage_basis_points, priority_fee).await?;
let signature = jito_client.send_transaction(&transaction).await?; let signature = jito_client.send_transaction(&transaction).await?;
println!("Total Jito sell operation time: {:?}ms, signature: {}", start_time.elapsed().as_millis(), signature); println!("Total Jito sell operation time: {:?}ms, signature: {}", start_time.elapsed().as_millis(), signature);
@@ -102,7 +102,7 @@ pub async fn build_sell_transaction(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_sell_instructions(rpc, payer, mint, amount_token, slippage_basis_points, priority_fee).await?; let instructions = build_sell_instructions(rpc, payer, mint, amount_token, slippage_basis_points, priority_fee).await?;
let recent_blockhash = rpc.get_latest_blockhash()?; let recent_blockhash = rpc.get_latest_blockhash()?;
@@ -123,9 +123,9 @@ pub async fn build_sell_transaction_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Transaction, anyhow::Error> { ) -> Result<Transaction, anyhow::Error> {
let instructions = build_sell_instructions_with_jito(rpc, jito_client, payer, mint, amount_token, slippage_basis_points, jito_fee).await?; let instructions = build_sell_instructions_with_jito(rpc, jito_client, payer, mint, amount_token, slippage_basis_points, priority_fee).await?;
let recent_blockhash = rpc.get_latest_blockhash()?; let recent_blockhash = rpc.get_latest_blockhash()?;
let transaction = Transaction::new_signed_with_payer( let transaction = Transaction::new_signed_with_payer(
&instructions, &instructions,
@@ -143,7 +143,7 @@ pub async fn build_sell_instructions(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: Option<PriorityFee>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
let (balance_u64, ata) = get_token_balance(rpc, payer, mint).await?; let (balance_u64, ata) = get_token_balance(rpc, payer, mint).await?;
let amount = amount_token.unwrap_or(balance_u64); let amount = amount_token.unwrap_or(balance_u64);
@@ -212,20 +212,14 @@ pub async fn build_sell_instructions(
let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?; let result_cu = result.units_consumed.ok_or_else(|| anyhow!("No compute units consumed"))?;
let fees = rpc.get_recent_prioritization_fees(&[])?; let fees = rpc.get_recent_prioritization_fees(&[])?;
let average_fees = if fees.is_empty() { let average_fees = if fees.is_empty() {
DEFAULT_COMPUTE_UNIT_PRICE sol_to_lamports(DEFAULT_COMPUTE_UNIT_PRICE)
} else { } else {
fees.iter() fees.iter()
.map(|fee| fee.prioritization_fee) .map(|fee| fee.prioritization_fee)
.sum::<u64>() / fees.len() as u64 .sum::<u64>() / fees.len() as u64
}; };
let unit_price = match priority_fee { let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees };
None => average_fees,
Some(pf) => pf.price.unwrap_or(DEFAULT_COMPUTE_UNIT_PRICE)
};
let unit_price = if unit_price == 0 { DEFAULT_COMPUTE_UNIT_PRICE } else { unit_price };
instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32); instructions[0] = ComputeBudgetInstruction::set_compute_unit_limit(result_cu as u32);
instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price); instructions[1] = ComputeBudgetInstruction::set_compute_unit_price(unit_price);
@@ -239,7 +233,7 @@ pub async fn build_sell_instructions_with_jito(
mint: &Pubkey, mint: &Pubkey,
amount_token: Option<u64>, amount_token: Option<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
jito_fee: Option<f64>, priority_fee: PriorityFee,
) -> Result<Vec<Instruction>, anyhow::Error> { ) -> Result<Vec<Instruction>, anyhow::Error> {
let (balance_u64, ata) = get_token_balance(rpc, payer, mint).await?; let (balance_u64, ata) = get_token_balance(rpc, payer, mint).await?;
let amount = amount_token.unwrap_or(balance_u64); let amount = amount_token.unwrap_or(balance_u64);
@@ -258,7 +252,11 @@ pub async fn build_sell_instructions_with_jito(
slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE), slippage_basis_points.unwrap_or(DEFAULT_SLIPPAGE),
); );
let mut instructions = vec![]; let mut instructions = vec![
ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32),
];
instructions.push(instruction::sell( instructions.push(instruction::sell(
payer, payer,
mint, mint,
@@ -278,7 +276,7 @@ pub async fn build_sell_instructions_with_jito(
)?); )?);
let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?; let tip_account = jito_client.get_tip_account().await.map_err(|e| anyhow!(e))?;
let jito_fee = jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT);
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),