update fee for custructor

This commit is contained in:
William
2025-02-21 23:02:15 +08:00
parent ecb2ffa747
commit 12ddfe4dc0
6 changed files with 50 additions and 44 deletions
+2
View File
@@ -57,4 +57,6 @@ pub mod trade {
pub const DEFAULT_SLIPPAGE: u64 = 3000; // 30% pub const DEFAULT_SLIPPAGE: u64 = 3000; // 30%
pub const DEFAULT_COMPUTE_UNIT_LIMIT: f64 = 78000.0; pub const DEFAULT_COMPUTE_UNIT_LIMIT: f64 = 78000.0;
pub const DEFAULT_COMPUTE_UNIT_PRICE: f64 = 500000.0; pub const DEFAULT_COMPUTE_UNIT_PRICE: f64 = 500000.0;
pub const DEFAULT_BUY_JITO_FEE: f64 = 0.0006;
pub const DEFAULT_SELL_JITO_FEE: f64 = 0.00006;
} }
+16 -23
View File
@@ -29,6 +29,7 @@ pub struct PumpFun {
pub payer: Arc<Keypair>, pub payer: Arc<Keypair>,
pub rpc: RpcClient, pub rpc: RpcClient,
pub jito_client: Option<JitoClient>, pub jito_client: Option<JitoClient>,
pub priority_fee: PriorityFee,
} }
impl Clone for PumpFun { impl Clone for PumpFun {
@@ -40,6 +41,7 @@ impl Clone for PumpFun {
self.rpc.commitment() self.rpc.commitment()
), ),
jito_client: self.jito_client.clone(), jito_client: self.jito_client.clone(),
priority_fee: self.priority_fee.clone(),
} }
} }
} }
@@ -49,8 +51,9 @@ impl PumpFun {
pub fn new( pub fn new(
payer: Arc<Keypair>, payer: Arc<Keypair>,
rpc_url: String, rpc_url: String,
commitment: Option<CommitmentConfig>,
jito_url: Option<String>, jito_url: Option<String>,
commitment: Option<CommitmentConfig>,
priority_fee: PriorityFee,
) -> Self { ) -> Self {
let rpc = RpcClient::new_with_commitment( let rpc = RpcClient::new_with_commitment(
rpc_url, rpc_url,
@@ -63,6 +66,7 @@ impl PumpFun {
payer, payer,
rpc, rpc,
jito_client, jito_client,
priority_fee,
} }
} }
@@ -71,14 +75,13 @@ impl PumpFun {
&self, &self,
mint: &Keypair, mint: &Keypair,
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::create::create( trade::create::create(
&self.rpc, &self.rpc,
&self.payer, &self.payer,
mint, mint,
ipfs, ipfs,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -88,7 +91,6 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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,
@@ -97,7 +99,7 @@ impl PumpFun {
ipfs, ipfs,
amount_sol, amount_sol,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -108,7 +110,6 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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 +119,7 @@ impl PumpFun {
ipfs, ipfs,
amount_sols, amount_sols,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -129,7 +130,6 @@ impl PumpFun {
ipfs: TokenMetadataIPFS, ipfs: TokenMetadataIPFS,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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,
priority_fee, self.priority_fee,
).await ).await
} }
/// Buy tokens /// Buy tokens
@@ -148,7 +148,6 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
priority_fee: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::buy::buy( trade::buy::buy(
&self.rpc, &self.rpc,
@@ -156,7 +155,7 @@ impl PumpFun {
mint, mint,
amount_sol, amount_sol,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -166,7 +165,6 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sol: u64, amount_sol: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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 +173,7 @@ impl PumpFun {
mint, mint,
amount_sol, amount_sol,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -185,7 +183,6 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
amount_sols: Vec<u64>, amount_sols: Vec<u64>,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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 +191,7 @@ impl PumpFun {
mint, mint,
amount_sols, amount_sols,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -204,7 +201,6 @@ 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: PriorityFee,
) -> Result<Signature, anyhow::Error> { ) -> Result<Signature, anyhow::Error> {
trade::sell::sell( trade::sell::sell(
&self.rpc, &self.rpc,
@@ -212,7 +208,7 @@ impl PumpFun {
mint, mint,
amount_token, amount_token,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -222,7 +218,6 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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,
@@ -230,7 +225,7 @@ impl PumpFun {
mint, mint,
percent, percent,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -239,7 +234,6 @@ impl PumpFun {
mint: &Pubkey, mint: &Pubkey,
percent: u64, percent: u64,
slippage_basis_points: Option<u64>, slippage_basis_points: Option<u64>,
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,7 +242,7 @@ impl PumpFun {
mint, mint,
percent, percent,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
@@ -258,7 +252,6 @@ 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: 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 +263,7 @@ impl PumpFun {
mint, mint,
amount_token, amount_token,
slippage_basis_points, slippage_basis_points,
priority_fee, self.priority_fee,
).await ).await
} }
+5 -4
View File
@@ -190,7 +190,8 @@ pub async fn build_buy_instructions(
.sum::<u64>() / fees.len() as u64 .sum::<u64>() / fees.len() as u64
}; };
let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees };
let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.unit_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);
@@ -223,8 +224,8 @@ 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_price(sol_to_lamports(priority_fee.unit_price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32), ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.unit_limit) as u32),
]; ];
let ata = get_associated_token_address(&payer.pubkey(), mint); let ata = get_associated_token_address(&payer.pubkey(), mint);
@@ -249,7 +250,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 = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.buy_jito_fee;
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),
+18 -8
View File
@@ -1,4 +1,5 @@
use anyhow::anyhow; use anyhow::anyhow;
use serde::Deserialize;
use tokio::sync::RwLock; 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;
@@ -6,23 +7,30 @@ use solana_sdk::{
compute_budget::ComputeBudgetInstruction, instruction::Instruction, native_token::sol_to_lamports, 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_BUY_JITO_FEE, DEFAULT_COMPUTE_UNIT_LIMIT, DEFAULT_COMPUTE_UNIT_PRICE, DEFAULT_SELL_JITO_FEE, DEFAULT_SLIPPAGE}}};
use borsh::BorshDeserialize; use borsh::BorshDeserialize;
lazy_static::lazy_static! { 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)] #[derive(Debug, Deserialize, Clone, Copy, PartialEq)]
pub struct PriorityFee { pub struct PriorityFee {
pub limit: f64, pub unit_limit: f64,
pub price: f64, pub unit_price: f64,
pub jito_fee: Option<f64>, pub buy_jito_fee: f64,
pub sell_jito_fee: f64,
} }
impl Default for PriorityFee { impl Default for PriorityFee {
fn default() -> Self { fn default() -> Self {
Self { limit: DEFAULT_COMPUTE_UNIT_LIMIT, price: DEFAULT_COMPUTE_UNIT_PRICE, jito_fee: None } Self {
unit_limit: DEFAULT_COMPUTE_UNIT_LIMIT,
unit_price: DEFAULT_COMPUTE_UNIT_PRICE,
buy_jito_fee: DEFAULT_BUY_JITO_FEE,
sell_jito_fee: DEFAULT_SELL_JITO_FEE
}
} }
} }
@@ -59,8 +67,10 @@ pub async fn transfer_sol(rpc: &RpcClient, payer: &Keypair, receive_wallet: &Pub
#[inline] #[inline]
pub fn create_priority_fee_instructions(priority_fee: 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);
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32)); let unit_limit = sol_to_lamports(priority_fee.unit_limit);
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price))); let unit_price = sol_to_lamports(priority_fee.unit_price);
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(unit_limit as u32));
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(unit_price));
instructions instructions
} }
+5 -5
View File
@@ -242,7 +242,7 @@ 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() {
sol_to_lamports(priority_fee.price) sol_to_lamports(priority_fee.unit_price)
} else { } else {
fees.iter() fees.iter()
.map(|fee| fee.prioritization_fee) .map(|fee| fee.prioritization_fee)
@@ -250,7 +250,7 @@ pub async fn build_create_and_buy_instructions(
}; };
let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees }; let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.unit_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);
@@ -278,8 +278,8 @@ pub async fn build_create_and_buy_instructions_with_jito(
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_price(sol_to_lamports(priority_fee.unit_price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32), ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.unit_limit) as u32),
]; ];
instructions.push(instruction::create( instructions.push(instruction::create(
@@ -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 = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.buy_jito_fee;
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),
+4 -4
View File
@@ -219,7 +219,7 @@ pub async fn build_sell_instructions(
.sum::<u64>() / fees.len() as u64 .sum::<u64>() / fees.len() as u64
}; };
let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.price) } else { average_fees }; let unit_price = if average_fees == 0 { sol_to_lamports(priority_fee.unit_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);
@@ -253,8 +253,8 @@ pub async fn build_sell_instructions_with_jito(
); );
let mut instructions = vec![ let mut instructions = vec![
ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.price)), ComputeBudgetInstruction::set_compute_unit_price(sol_to_lamports(priority_fee.unit_price)),
ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.limit) as u32), ComputeBudgetInstruction::set_compute_unit_limit(sol_to_lamports(priority_fee.unit_limit) as u32),
]; ];
instructions.push(instruction::sell( instructions.push(instruction::sell(
@@ -276,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 = priority_fee.jito_fee.unwrap_or(JITO_TIP_AMOUNT); let jito_fee = priority_fee.sell_jito_fee;
instructions.push( instructions.push(
system_instruction::transfer( system_instruction::transfer(
&payer.pubkey(), &payer.pubkey(),