refactor: reorganize code structure and optimize modular design
- Remove src/common/address_lookup.rs and tip_cache.rs to simplify common modules - Move protocol-specific utility functions from trading/ to instruction/utils/ - Refactor utility functions for PumpFun, PumpSwap, Raydium AMM V4, and Raydium CPMM - Consolidate constant definitions by inlining seeds and accounts modules into respective utility files - Update all import paths to ensure code consistency - Optimize trading parameter construction and executor logic - Improve address lookup cache and nonce management mechanisms
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::constants::bonk::accounts;
|
||||
use crate::instruction::utils::bonk::accounts;
|
||||
|
||||
/// Calculates the amount of tokens to receive when buying with SOL
|
||||
///
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use solana_sdk::{native_token::sol_str_to_lamports, pubkey::Pubkey};
|
||||
|
||||
use crate::{
|
||||
constants::pumpfun::global_constants::{CREATOR_FEE, FEE_BASIS_POINTS},
|
||||
instruction::utils::pumpfun::global_constants::{CREATOR_FEE, FEE_BASIS_POINTS},
|
||||
utils::calc::common::compute_fee,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::common::{
|
||||
calculate_with_slippage_buy, calculate_with_slippage_sell, ceil_div, compute_fee,
|
||||
};
|
||||
use crate::constants::pumpswap::accounts::{
|
||||
use crate::instruction::utils::pumpswap::accounts::{
|
||||
COIN_CREATOR_FEE_BASIS_POINTS, LP_FEE_BASIS_POINTS, PROTOCOL_FEE_BASIS_POINTS,
|
||||
};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::constants::raydium_amm_v4::accounts::{
|
||||
use crate::instruction::utils::raydium_amm_v4::accounts::{
|
||||
SWAP_FEE_DENOMINATOR, SWAP_FEE_NUMERATOR, TRADE_FEE_DENOMINATOR, TRADE_FEE_NUMERATOR,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::constants::raydium_cpmm::accounts::{
|
||||
use crate::instruction::utils::raydium_cpmm::accounts::{
|
||||
CREATOR_FEE_RATE, FEE_RATE_DENOMINATOR_VALUE, FUND_FEE_RATE, PROTOCOL_FEE_RATE, TRADE_FEE_RATE,
|
||||
};
|
||||
|
||||
@@ -176,7 +176,8 @@ pub fn compute_swap_amount(
|
||||
true,
|
||||
);
|
||||
|
||||
let min_amount_out = ((swap_result.output_amount as f64) * (1.0 - (slippage_basis_points as f64) / 10000.0)) as u64;
|
||||
let min_amount_out = ((swap_result.output_amount as f64)
|
||||
* (1.0 - (slippage_basis_points as f64) / 10000.0)) as u64;
|
||||
|
||||
let all_trade = swap_result.input_amount == amount_in;
|
||||
|
||||
|
||||
+14
-11
@@ -1,5 +1,5 @@
|
||||
pub mod price;
|
||||
pub mod calc;
|
||||
pub mod price;
|
||||
|
||||
use crate::solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
||||
use crate::trading;
|
||||
@@ -62,7 +62,7 @@ impl SolanaTrade {
|
||||
|
||||
#[inline]
|
||||
pub fn get_pumpfun_token_buy_price(&self, amount: u64, trade_info: &PumpFunTradeEvent) -> u64 {
|
||||
trading::pumpfun::common::get_buy_price(amount, trade_info)
|
||||
crate::instruction::utils::pumpfun::get_buy_price(amount, trade_info)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -71,7 +71,8 @@ impl SolanaTrade {
|
||||
mint: &Pubkey,
|
||||
) -> Result<f64, anyhow::Error> {
|
||||
let (bonding_curve, _) =
|
||||
trading::pumpfun::common::fetch_bonding_curve_account(&self.rpc, mint).await?;
|
||||
crate::instruction::utils::pumpfun::fetch_bonding_curve_account(&self.rpc, mint)
|
||||
.await?;
|
||||
|
||||
let virtual_sol_reserves = bonding_curve.virtual_sol_reserves;
|
||||
let virtual_token_reserves = bonding_curve.virtual_token_reserves;
|
||||
@@ -85,7 +86,8 @@ impl SolanaTrade {
|
||||
mint: &Pubkey,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let (bonding_curve, _) =
|
||||
trading::pumpfun::common::fetch_bonding_curve_account(&self.rpc, mint).await?;
|
||||
crate::instruction::utils::pumpfun::fetch_bonding_curve_account(&self.rpc, mint)
|
||||
.await?;
|
||||
|
||||
let actual_sol_reserves = bonding_curve.real_sol_reserves;
|
||||
|
||||
@@ -95,7 +97,8 @@ impl SolanaTrade {
|
||||
#[inline]
|
||||
pub async fn get_pumpfun_token_creator(&self, mint: &Pubkey) -> Result<Pubkey, anyhow::Error> {
|
||||
let (bonding_curve, _) =
|
||||
trading::pumpfun::common::fetch_bonding_curve_account(&self.rpc, mint).await?;
|
||||
crate::instruction::utils::pumpfun::fetch_bonding_curve_account(&self.rpc, mint)
|
||||
.await?;
|
||||
|
||||
let creator = bonding_curve.creator;
|
||||
|
||||
@@ -109,10 +112,10 @@ impl SolanaTrade {
|
||||
&self,
|
||||
pool_address: &Pubkey,
|
||||
) -> Result<f64, anyhow::Error> {
|
||||
let pool = trading::pumpswap::common::fetch_pool(&self.rpc, pool_address).await?;
|
||||
let pool = crate::instruction::utils::pumpswap::fetch_pool(&self.rpc, pool_address).await?;
|
||||
|
||||
let (base_amount, quote_amount) =
|
||||
trading::pumpswap::common::get_token_balances(&pool, &self.rpc).await?;
|
||||
crate::instruction::utils::pumpswap::get_token_balances(&pool, &self.rpc).await?;
|
||||
|
||||
// Calculate price using constant product formula (x * y = k)
|
||||
// Price = quote_amount / base_amount
|
||||
@@ -130,10 +133,10 @@ impl SolanaTrade {
|
||||
&self,
|
||||
pool_address: &Pubkey,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let pool = trading::pumpswap::common::fetch_pool(&self.rpc, pool_address).await?;
|
||||
let pool = crate::instruction::utils::pumpswap::fetch_pool(&self.rpc, pool_address).await?;
|
||||
|
||||
let (_, quote_amount) =
|
||||
trading::pumpswap::common::get_token_balances(&pool, &self.rpc).await?;
|
||||
crate::instruction::utils::pumpswap::get_token_balances(&pool, &self.rpc).await?;
|
||||
|
||||
Ok(quote_amount)
|
||||
}
|
||||
@@ -143,10 +146,10 @@ impl SolanaTrade {
|
||||
&self,
|
||||
pool_address: &Pubkey,
|
||||
) -> Result<u64, anyhow::Error> {
|
||||
let pool = trading::pumpswap::common::fetch_pool(&self.rpc, pool_address).await?;
|
||||
let pool = crate::instruction::utils::pumpswap::fetch_pool(&self.rpc, pool_address).await?;
|
||||
|
||||
let (base_amount, _) =
|
||||
trading::pumpswap::common::get_token_balances(&pool, &self.rpc).await?;
|
||||
crate::instruction::utils::pumpswap::get_token_balances(&pool, &self.rpc).await?;
|
||||
|
||||
Ok(base_amount)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::types::PoolState;
|
||||
|
||||
use crate::constants::{
|
||||
bonk::accounts::WSOL_TOKEN_ACCOUNT,
|
||||
decimals::{DEFAULT_TOKEN_DECIMALS, SOL_DECIMALS},
|
||||
use crate::{
|
||||
constants::decimals::{DEFAULT_TOKEN_DECIMALS, SOL_DECIMALS},
|
||||
instruction::utils::bonk::accounts::WSOL_TOKEN_ACCOUNT,
|
||||
};
|
||||
|
||||
/// Calculate the token price in WSOL based on pool state
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::types::BondingCurve;
|
||||
|
||||
use crate::constants::pumpfun::global_constants::{LAMPORTS_PER_SOL, SCALE};
|
||||
use crate::instruction::utils::pumpfun::global_constants::{LAMPORTS_PER_SOL, SCALE};
|
||||
|
||||
/// Calculate the token price in SOL based on virtual reserves
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user