refactor: remove solana-streamer-sdk dependency and migrate type definitions locally
This refactoring migrates protocol-related type definitions from solana-streamer-sdk to local modules: - Add local type files: bonk_types.rs, pumpswap_types.rs, raydium_amm_v4_types.rs, raydium_cpmm_types.rs - Add BorshDeserialize support for all types - Refactor BondingCurveAccount to support Borsh deserialization - Update import paths across all protocol utility modules (bonk, pumpfun, pumpswap, raydium) - Refactor trade parameter construction methods from event objects to individual parameters for better flexibility - Fix nonce_cache import path to use local SolanaRpcClient
This commit is contained in:
+1
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "sol-trade-sdk"
|
||||
version = "2.0.0"
|
||||
version = "3.0.0"
|
||||
edition = "2021"
|
||||
authors = [
|
||||
"William <byteblock6@gmail.com>",
|
||||
@@ -38,7 +38,6 @@ members = [
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
solana-streamer-sdk = "0.5.0"
|
||||
solana-sdk = "3.0.0"
|
||||
solana-client = "3.0.0"
|
||||
solana-program = "3.0.0"
|
||||
|
||||
@@ -87,14 +87,14 @@ Add the dependency to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
# Add to your Cargo.toml
|
||||
sol-trade-sdk = { path = "./sol-trade-sdk", version = "2.0.0" }
|
||||
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.0.0" }
|
||||
```
|
||||
|
||||
### Use crates.io
|
||||
|
||||
```toml
|
||||
# Add to your Cargo.toml
|
||||
sol-trade-sdk = "2.0.0"
|
||||
sol-trade-sdk = "3.0.0"
|
||||
```
|
||||
|
||||
## 🛠️ Usage Examples
|
||||
|
||||
+2
-2
@@ -87,14 +87,14 @@ git clone https://github.com/0xfnzero/sol-trade-sdk
|
||||
|
||||
```toml
|
||||
# 添加到您的 Cargo.toml
|
||||
sol-trade-sdk = { path = "./sol-trade-sdk", version = "2.0.0" }
|
||||
sol-trade-sdk = { path = "./sol-trade-sdk", version = "3.0.0" }
|
||||
```
|
||||
|
||||
### 使用 crates.io
|
||||
|
||||
```toml
|
||||
# 添加到您的 Cargo.toml
|
||||
sol-trade-sdk = "2.0.0"
|
||||
sol-trade-sdk = "3.0.0"
|
||||
```
|
||||
|
||||
## 🛠️ 使用示例
|
||||
|
||||
@@ -149,7 +149,18 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(
|
||||
trade_info.bonding_curve,
|
||||
trade_info.associated_bonding_curve,
|
||||
trade_info.mint,
|
||||
trade_info.creator,
|
||||
trade_info.creator_vault,
|
||||
trade_info.virtual_sol_reserves,
|
||||
trade_info.virtual_sol_reserves,
|
||||
trade_info.real_token_reserves,
|
||||
trade_info.real_sol_reserves,
|
||||
None,
|
||||
)),
|
||||
lookup_table_key: Some(lookup_table_key), // you still need to update the AddressLookupTableCache
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: false,
|
||||
|
||||
@@ -142,7 +142,20 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(BonkParams::from_trade(trade_info.clone())),
|
||||
extension_params: Box::new(BonkParams::from_trade(
|
||||
trade_info.virtual_base,
|
||||
trade_info.virtual_quote,
|
||||
trade_info.real_base_after,
|
||||
trade_info.real_quote_after,
|
||||
trade_info.pool_state,
|
||||
trade_info.base_vault,
|
||||
trade_info.quote_vault,
|
||||
trade_info.base_token_program,
|
||||
trade_info.platform_config,
|
||||
trade_info.platform_associated_account,
|
||||
trade_info.creator_associated_account,
|
||||
trade_info.global_config,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
@@ -171,7 +184,20 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
|
||||
input_token_amount: amount_token,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(BonkParams::from_trade(trade_info.clone())),
|
||||
extension_params: Box::new(BonkParams::from_trade(
|
||||
trade_info.virtual_base,
|
||||
trade_info.virtual_quote,
|
||||
trade_info.real_base_after,
|
||||
trade_info.real_quote_after,
|
||||
trade_info.pool_state,
|
||||
trade_info.base_vault,
|
||||
trade_info.quote_vault,
|
||||
trade_info.base_token_program,
|
||||
trade_info.platform_config,
|
||||
trade_info.platform_associated_account,
|
||||
trade_info.creator_associated_account,
|
||||
trade_info.global_config,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
open_seed_optimize: false,
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
use sol_trade_sdk::common::spl_associated_token_account::get_associated_token_address;
|
||||
use sol_trade_sdk::common::TradeConfig;
|
||||
use solana_streamer_sdk::streaming::event_parser::common::filter::EventTypeFilter;
|
||||
use solana_streamer_sdk::streaming::event_parser::common::EventType;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
|
||||
use solana_streamer_sdk::streaming::event_parser::{Protocol, UnifiedEvent};
|
||||
use solana_streamer_sdk::{match_event, streaming::ShredStreamGrpc};
|
||||
use sol_trade_sdk::{
|
||||
common::AnyResult,
|
||||
swqos::SwqosConfig,
|
||||
@@ -14,6 +9,11 @@ use sol_trade_sdk::{
|
||||
use solana_commitment_config::CommitmentConfig;
|
||||
use solana_sdk::signature::Keypair;
|
||||
use solana_sdk::signer::Signer;
|
||||
use solana_streamer_sdk::streaming::event_parser::common::filter::EventTypeFilter;
|
||||
use solana_streamer_sdk::streaming::event_parser::common::EventType;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
|
||||
use solana_streamer_sdk::streaming::event_parser::{Protocol, UnifiedEvent};
|
||||
use solana_streamer_sdk::{match_event, streaming::ShredStreamGrpc};
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
@@ -113,7 +113,19 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(BonkParams::from_dev_trade(trade_info.clone())),
|
||||
extension_params: Box::new(BonkParams::from_dev_trade(
|
||||
trade_info.exact_in,
|
||||
trade_info.amount_in,
|
||||
trade_info.amount_out,
|
||||
trade_info.pool_state,
|
||||
trade_info.base_vault,
|
||||
trade_info.quote_vault,
|
||||
trade_info.base_token_program,
|
||||
trade_info.platform_config,
|
||||
trade_info.platform_associated_account,
|
||||
trade_info.creator_associated_account,
|
||||
trade_info.global_config,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
|
||||
@@ -136,7 +136,18 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(
|
||||
trade_info.bonding_curve,
|
||||
trade_info.associated_bonding_curve,
|
||||
trade_info.mint,
|
||||
trade_info.creator,
|
||||
trade_info.creator_vault,
|
||||
trade_info.virtual_token_reserves,
|
||||
trade_info.virtual_sol_reserves,
|
||||
trade_info.real_token_reserves,
|
||||
trade_info.real_sol_reserves,
|
||||
None,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: false,
|
||||
|
||||
@@ -132,7 +132,18 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(
|
||||
trade_info.bonding_curve,
|
||||
trade_info.associated_bonding_curve,
|
||||
trade_info.mint,
|
||||
trade_info.creator,
|
||||
trade_info.creator_vault,
|
||||
trade_info.virtual_token_reserves,
|
||||
trade_info.virtual_sol_reserves,
|
||||
trade_info.real_token_reserves,
|
||||
trade_info.real_sol_reserves,
|
||||
None,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: false,
|
||||
@@ -162,7 +173,18 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
with_tip: false,
|
||||
extension_params: Box::new(PumpFunParams::from_trade(&trade_info, Some(true))),
|
||||
extension_params: Box::new(PumpFunParams::from_trade(
|
||||
trade_info.bonding_curve,
|
||||
trade_info.associated_bonding_curve,
|
||||
trade_info.mint,
|
||||
trade_info.creator,
|
||||
trade_info.creator_vault,
|
||||
trade_info.virtual_token_reserves,
|
||||
trade_info.virtual_sol_reserves,
|
||||
trade_info.real_token_reserves,
|
||||
trade_info.real_sol_reserves,
|
||||
Some(true),
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_output_token_ata: false,
|
||||
|
||||
@@ -100,7 +100,16 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
|
||||
input_token_amount: buy_sol_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(PumpFunParams::from_dev_trade(&trade_info, None)),
|
||||
extension_params: Box::new(PumpFunParams::from_dev_trade(
|
||||
trade_info.mint,
|
||||
trade_info.token_amount,
|
||||
trade_info.max_sol_cost,
|
||||
trade_info.creator,
|
||||
trade_info.bonding_curve,
|
||||
trade_info.associated_bonding_curve,
|
||||
trade_info.creator_vault,
|
||||
None,
|
||||
)),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
|
||||
@@ -131,7 +131,19 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
||||
}
|
||||
|
||||
async fn pumpswap_trade_with_grpc_buy_event(trade_info: PumpSwapBuyEvent) -> AnyResult<()> {
|
||||
let params = PumpSwapParams::from_buy_trade(&trade_info);
|
||||
let params = PumpSwapParams::new(
|
||||
trade_info.pool,
|
||||
trade_info.base_mint,
|
||||
trade_info.quote_mint,
|
||||
trade_info.pool_base_token_account,
|
||||
trade_info.pool_quote_token_account,
|
||||
trade_info.pool_base_token_reserves,
|
||||
trade_info.pool_quote_token_reserves,
|
||||
trade_info.coin_creator_vault_ata,
|
||||
trade_info.coin_creator_vault_authority,
|
||||
trade_info.base_token_program,
|
||||
trade_info.quote_token_program,
|
||||
);
|
||||
let mint = if trade_info.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
trade_info.quote_mint
|
||||
} else {
|
||||
@@ -142,7 +154,19 @@ async fn pumpswap_trade_with_grpc_buy_event(trade_info: PumpSwapBuyEvent) -> Any
|
||||
}
|
||||
|
||||
async fn pumpswap_trade_with_grpc_sell_event(trade_info: PumpSwapSellEvent) -> AnyResult<()> {
|
||||
let params = PumpSwapParams::from_sell_trade(&trade_info);
|
||||
let params = PumpSwapParams::new(
|
||||
trade_info.pool,
|
||||
trade_info.base_mint,
|
||||
trade_info.quote_mint,
|
||||
trade_info.pool_base_token_account,
|
||||
trade_info.pool_quote_token_account,
|
||||
trade_info.pool_base_token_reserves,
|
||||
trade_info.pool_quote_token_reserves,
|
||||
trade_info.coin_creator_vault_ata,
|
||||
trade_info.coin_creator_vault_authority,
|
||||
trade_info.base_token_program,
|
||||
trade_info.quote_token_program,
|
||||
);
|
||||
let mint = if trade_info.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
trade_info.quote_mint
|
||||
} else {
|
||||
|
||||
@@ -130,9 +130,12 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
||||
} else {
|
||||
amm_info.pc_mint
|
||||
};
|
||||
let params = RaydiumAmmV4Params::from_amm_info_and_reserves(
|
||||
let params = RaydiumAmmV4Params::new(
|
||||
trade_info.amm,
|
||||
amm_info,
|
||||
amm_info.coin_mint,
|
||||
amm_info.pc_mint,
|
||||
amm_info.token_coin,
|
||||
amm_info.token_pc,
|
||||
coin_reserve,
|
||||
pc_reserve,
|
||||
);
|
||||
|
||||
+25
-14
@@ -25,6 +25,7 @@
|
||||
//! - `get_final_market_cap_sol`: Calculates the final market cap in SOL after all tokens are sold
|
||||
//! - `get_buy_out_price`: Calculates the price to buy out all remaining tokens
|
||||
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
@@ -33,14 +34,15 @@ use crate::instruction::utils::pumpfun::global_constants::{
|
||||
TOKEN_TOTAL_SUPPLY,
|
||||
};
|
||||
use crate::instruction::utils::pumpfun::{get_bonding_curve_pda, get_creator_vault_pda};
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
||||
|
||||
/// Represents the global configuration account for token pricing and fees
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, BorshDeserialize)]
|
||||
pub struct BondingCurveAccount {
|
||||
/// Unique identifier for the bonding curve
|
||||
#[borsh(skip)]
|
||||
pub discriminator: u64,
|
||||
/// Account address
|
||||
#[borsh(skip)]
|
||||
pub account: Pubkey,
|
||||
/// Virtual token reserves used for price calculations
|
||||
pub virtual_token_reserves: u64,
|
||||
@@ -60,15 +62,16 @@ pub struct BondingCurveAccount {
|
||||
|
||||
impl BondingCurveAccount {
|
||||
pub fn from_dev_trade(
|
||||
bonding_curve: Pubkey,
|
||||
mint: &Pubkey,
|
||||
dev_token_amount: u64,
|
||||
dev_sol_amount: u64,
|
||||
creator: Pubkey,
|
||||
) -> Self {
|
||||
let account = if mint != &Pubkey::default() {
|
||||
get_bonding_curve_pda(mint).unwrap()
|
||||
let account = if bonding_curve != Pubkey::default() {
|
||||
bonding_curve
|
||||
} else {
|
||||
Pubkey::default()
|
||||
get_bonding_curve_pda(&mint).unwrap()
|
||||
};
|
||||
Self {
|
||||
discriminator: 0,
|
||||
@@ -83,22 +86,30 @@ impl BondingCurveAccount {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_trade(event: &PumpFunTradeEvent) -> Self {
|
||||
let account = if event.bonding_curve != Pubkey::default() {
|
||||
event.bonding_curve
|
||||
pub fn from_trade(
|
||||
bonding_curve: Pubkey,
|
||||
mint: Pubkey,
|
||||
creator: Pubkey,
|
||||
virtual_token_reserves: u64,
|
||||
virtual_sol_reserves: u64,
|
||||
real_token_reserves: u64,
|
||||
real_sol_reserves: u64,
|
||||
) -> Self {
|
||||
let account = if bonding_curve != Pubkey::default() {
|
||||
bonding_curve
|
||||
} else {
|
||||
get_bonding_curve_pda(&event.mint).unwrap()
|
||||
get_bonding_curve_pda(&mint).unwrap()
|
||||
};
|
||||
Self {
|
||||
discriminator: 0,
|
||||
account: account,
|
||||
virtual_token_reserves: event.virtual_token_reserves,
|
||||
virtual_sol_reserves: event.virtual_sol_reserves,
|
||||
real_token_reserves: event.real_token_reserves,
|
||||
real_sol_reserves: event.real_sol_reserves,
|
||||
virtual_token_reserves: virtual_token_reserves,
|
||||
virtual_sol_reserves: virtual_sol_reserves,
|
||||
real_token_reserves: real_token_reserves,
|
||||
real_sol_reserves: real_sol_reserves,
|
||||
token_total_supply: TOKEN_TOTAL_SUPPLY,
|
||||
complete: false,
|
||||
creator: event.creator,
|
||||
creator: creator,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,10 @@ use solana_nonce::state::State;
|
||||
use solana_nonce::versions::Versions;
|
||||
use solana_sdk::account_utils::StateMut;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::common::SolanaRpcClient;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use tracing::error;
|
||||
use crate::common::SolanaRpcClient;
|
||||
|
||||
/// NonceInfo structure to store nonce-related information
|
||||
pub struct NonceInfo {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::common::SolanaRpcClient;
|
||||
use crate::{
|
||||
common::SolanaRpcClient,
|
||||
instruction::utils::bonk_types::{pool_state_decode, PoolState},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::{
|
||||
pool_state_decode, types::PoolState,
|
||||
};
|
||||
|
||||
/// Constants used as seeds for deriving PDAs (Program Derived Addresses)
|
||||
pub mod seeds {
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub enum TradeDirection {
|
||||
#[default]
|
||||
Buy,
|
||||
Sell,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub enum PoolStatus {
|
||||
#[default]
|
||||
Fund,
|
||||
Migrate,
|
||||
Trade,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct MintParams {
|
||||
pub decimals: u8,
|
||||
pub name: String,
|
||||
pub symbol: String,
|
||||
pub uri: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct VestingParams {
|
||||
pub total_locked_amount: u64,
|
||||
pub cliff_period: u64,
|
||||
pub unlock_period: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub enum AmmFeeOn {
|
||||
#[default]
|
||||
QuoteToken,
|
||||
BothToken,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct ConstantCurve {
|
||||
pub supply: u64,
|
||||
pub total_base_sell: u64,
|
||||
pub total_quote_fund_raising: u64,
|
||||
pub migrate_type: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct FixedCurve {
|
||||
pub supply: u64,
|
||||
pub total_quote_fund_raising: u64,
|
||||
pub migrate_type: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct LinearCurve {
|
||||
pub supply: u64,
|
||||
pub total_quote_fund_raising: u64,
|
||||
pub migrate_type: u8,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub enum CurveParams {
|
||||
Constant { data: ConstantCurve },
|
||||
Fixed { data: FixedCurve },
|
||||
Linear { data: LinearCurve },
|
||||
}
|
||||
impl Default for CurveParams {
|
||||
fn default() -> Self {
|
||||
Self::Constant { data: ConstantCurve::default() }
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct VestingSchedule {
|
||||
pub total_locked_amount: u64,
|
||||
pub cliff_period: u64,
|
||||
pub unlock_period: u64,
|
||||
pub start_time: u64,
|
||||
pub allocated_share_amount: u64,
|
||||
}
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PoolState {
|
||||
pub epoch: u64,
|
||||
pub auth_bump: u8,
|
||||
pub status: u8,
|
||||
pub base_decimals: u8,
|
||||
pub quote_decimals: u8,
|
||||
pub migrate_type: u8,
|
||||
pub supply: u64,
|
||||
pub total_base_sell: u64,
|
||||
pub virtual_base: u64,
|
||||
pub virtual_quote: u64,
|
||||
pub real_base: u64,
|
||||
pub real_quote: u64,
|
||||
pub total_quote_fund_raising: u64,
|
||||
pub quote_protocol_fee: u64,
|
||||
pub platform_fee: u64,
|
||||
pub migrate_fee: u64,
|
||||
pub vesting_schedule: VestingSchedule,
|
||||
pub global_config: Pubkey,
|
||||
pub platform_config: Pubkey,
|
||||
pub base_mint: Pubkey,
|
||||
pub quote_mint: Pubkey,
|
||||
pub base_vault: Pubkey,
|
||||
pub quote_vault: Pubkey,
|
||||
pub creator: Pubkey,
|
||||
pub padding: [u64; 8],
|
||||
}
|
||||
pub const POOL_STATE_SIZE: usize = 8 + 1 * 5 + 8 * 10 + 32 * 7 + 8 * 8 + 8 * 5;
|
||||
pub fn pool_state_decode(data: &[u8]) -> Option<PoolState> {
|
||||
if data.len() < POOL_STATE_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<PoolState>(&data[..POOL_STATE_SIZE]).ok()
|
||||
}
|
||||
@@ -3,3 +3,9 @@ pub mod pumpfun;
|
||||
pub mod pumpswap;
|
||||
pub mod raydium_amm_v4;
|
||||
pub mod raydium_cpmm;
|
||||
|
||||
// types
|
||||
pub mod bonk_types;
|
||||
pub mod pumpswap_types;
|
||||
pub mod raydium_amm_v4_types;
|
||||
pub mod raydium_cpmm_types;
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::common::{global::GlobalAccount, SolanaRpcClient};
|
||||
use crate::common::{bonding_curve::BondingCurveAccount, global::GlobalAccount, SolanaRpcClient};
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -216,13 +215,7 @@ pub fn get_user_volume_accumulator_pda(user: &Pubkey) -> Option<Pubkey> {
|
||||
pub async fn fetch_bonding_curve_account(
|
||||
rpc: &SolanaRpcClient,
|
||||
mint: &Pubkey,
|
||||
) -> Result<
|
||||
(
|
||||
Arc<solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::types::BondingCurve>,
|
||||
Pubkey,
|
||||
),
|
||||
anyhow::Error,
|
||||
> {
|
||||
) -> Result<(Arc<BondingCurveAccount>, Pubkey), anyhow::Error> {
|
||||
let bonding_curve_pda: Pubkey =
|
||||
get_bonding_curve_pda(mint).ok_or(anyhow!("Bonding curve not found"))?;
|
||||
|
||||
@@ -231,26 +224,29 @@ pub async fn fetch_bonding_curve_account(
|
||||
return Err(anyhow!("Bonding curve not found"));
|
||||
}
|
||||
|
||||
let bonding_curve = solana_sdk::borsh1::try_from_slice_unchecked::<
|
||||
solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::types::BondingCurve,
|
||||
>(&account.data[8..])
|
||||
.map_err(|e| anyhow::anyhow!("Failed to deserialize bonding curve account: {}", e))?;
|
||||
let bonding_curve =
|
||||
solana_sdk::borsh1::try_from_slice_unchecked::<BondingCurveAccount>(&account.data[8..])
|
||||
.map_err(|e| anyhow::anyhow!("Failed to deserialize bonding curve account: {}", e))?;
|
||||
|
||||
Ok((Arc::new(bonding_curve), bonding_curve_pda))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_buy_price(amount: u64, trade_info: &PumpFunTradeEvent) -> u64 {
|
||||
pub fn get_buy_price(
|
||||
amount: u64,
|
||||
virtual_sol_reserves: u64,
|
||||
virtual_token_reserves: u64,
|
||||
real_token_reserves: u64,
|
||||
) -> u64 {
|
||||
if amount == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let n: u128 =
|
||||
(trade_info.virtual_sol_reserves as u128) * (trade_info.virtual_token_reserves as u128);
|
||||
let i: u128 = (trade_info.virtual_sol_reserves as u128) + (amount as u128);
|
||||
let n: u128 = (virtual_sol_reserves as u128) * (virtual_token_reserves as u128);
|
||||
let i: u128 = (virtual_sol_reserves as u128) + (amount as u128);
|
||||
let r: u128 = n / i + 1;
|
||||
let s: u128 = (trade_info.virtual_token_reserves as u128) - r;
|
||||
let s: u128 = (virtual_token_reserves as u128) - r;
|
||||
let s_u64 = s as u64;
|
||||
|
||||
s_u64.min(trade_info.real_token_reserves)
|
||||
s_u64.min(real_token_reserves)
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ use crate::{
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id, SolanaRpcClient,
|
||||
},
|
||||
constants::TOKEN_PROGRAM,
|
||||
instruction::utils::pumpswap_types::{pool_decode, Pool},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use solana_account_decoder::UiAccountEncoding;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpswap::types::{pool_decode, Pool};
|
||||
|
||||
/// Constants used as seeds for deriving PDAs (Program Derived Addresses)
|
||||
pub mod seeds {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct Pool {
|
||||
pub pool_bump: u8,
|
||||
pub index: u16,
|
||||
pub creator: Pubkey,
|
||||
pub base_mint: Pubkey,
|
||||
pub quote_mint: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub pool_base_token_account: Pubkey,
|
||||
pub pool_quote_token_account: Pubkey,
|
||||
pub lp_supply: u64,
|
||||
pub coin_creator: Pubkey,
|
||||
}
|
||||
|
||||
pub const POOL_SIZE: usize = 1 + 2 + 32 * 6 + 8 + 32;
|
||||
|
||||
pub fn pool_decode(data: &[u8]) -> Option<Pool> {
|
||||
if data.len() < POOL_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<Pool>(&data[..POOL_SIZE]).ok()
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
use crate::{
|
||||
common::SolanaRpcClient,
|
||||
instruction::utils::raydium_amm_v4_types::{amm_info_decode, AmmInfo},
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::raydium_amm_v4::types::{
|
||||
amm_info_decode, AmmInfo,
|
||||
};
|
||||
|
||||
use crate::common::SolanaRpcClient;
|
||||
|
||||
/// Constants used as seeds for deriving PDAs (Program Derived Addresses)
|
||||
pub mod seeds {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct Fees {
|
||||
pub min_separate_numerator: u64,
|
||||
pub min_separate_denominator: u64,
|
||||
pub trade_fee_numerator: u64,
|
||||
pub trade_fee_denominator: u64,
|
||||
pub pnl_numerator: u64,
|
||||
pub pnl_denominator: u64,
|
||||
pub swap_fee_numerator: u64,
|
||||
pub swap_fee_denominator: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct OutPutData {
|
||||
pub need_take_pnl_coin: u64,
|
||||
pub need_take_pnl_pc: u64,
|
||||
pub total_pnl_pc: u64,
|
||||
pub total_pnl_coin: u64,
|
||||
pub pool_open_time: u64,
|
||||
pub punish_pc_amount: u64,
|
||||
pub punish_coin_amount: u64,
|
||||
pub orderbook_to_init_time: u64,
|
||||
pub swap_coin_in_amount: u128,
|
||||
pub swap_pc_out_amount: u128,
|
||||
pub swap_take_pc_fee: u64,
|
||||
pub swap_pc_in_amount: u128,
|
||||
pub swap_coin_out_amount: u128,
|
||||
pub swap_take_coin_fee: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct AmmInfo {
|
||||
pub status: u64,
|
||||
pub nonce: u64,
|
||||
pub order_num: u64,
|
||||
pub depth: u64,
|
||||
pub coin_decimals: u64,
|
||||
pub pc_decimals: u64,
|
||||
pub state: u64,
|
||||
pub reset_flag: u64,
|
||||
pub min_size: u64,
|
||||
pub vol_max_cut_ratio: u64,
|
||||
pub amount_wave: u64,
|
||||
pub coin_lot_size: u64,
|
||||
pub pc_lot_size: u64,
|
||||
pub min_price_multiplier: u64,
|
||||
pub max_price_multiplier: u64,
|
||||
pub sys_decimal_value: u64,
|
||||
pub fees: Fees,
|
||||
pub out_put: OutPutData,
|
||||
pub token_coin: Pubkey,
|
||||
pub token_pc: Pubkey,
|
||||
pub coin_mint: Pubkey,
|
||||
pub pc_mint: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub open_orders: Pubkey,
|
||||
pub market: Pubkey,
|
||||
pub serum_dex: Pubkey,
|
||||
pub target_orders: Pubkey,
|
||||
pub withdraw_queue: Pubkey,
|
||||
pub token_temp_lp: Pubkey,
|
||||
pub amm_owner: Pubkey,
|
||||
pub lp_amount: u64,
|
||||
pub client_order_id: u64,
|
||||
pub padding: [u64; 2],
|
||||
}
|
||||
|
||||
pub const AMM_INFO_SIZE: usize = 752;
|
||||
|
||||
pub fn amm_info_decode(data: &[u8]) -> Option<AmmInfo> {
|
||||
if data.len() < AMM_INFO_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<AmmInfo>(&data[..AMM_INFO_SIZE]).ok()
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
use crate::{common::SolanaRpcClient, trading::core::params::RaydiumCpmmParams};
|
||||
use crate::{
|
||||
common::SolanaRpcClient,
|
||||
instruction::utils::raydium_cpmm_types::{pool_state_decode, PoolState},
|
||||
trading::core::params::RaydiumCpmmParams,
|
||||
};
|
||||
use anyhow::anyhow;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::raydium_cpmm::types::{
|
||||
pool_state_decode, PoolState,
|
||||
};
|
||||
|
||||
/// Constants used as seeds for deriving PDAs (Program Derived Addresses)
|
||||
pub mod seeds {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct PoolState {
|
||||
pub amm_config: Pubkey,
|
||||
pub pool_creator: Pubkey,
|
||||
pub token0_vault: Pubkey,
|
||||
pub token1_vault: Pubkey,
|
||||
pub lp_mint: Pubkey,
|
||||
pub token0_mint: Pubkey,
|
||||
pub token1_mint: Pubkey,
|
||||
pub token0_program: Pubkey,
|
||||
pub token1_program: Pubkey,
|
||||
pub observation_key: Pubkey,
|
||||
pub auth_bump: u8,
|
||||
pub status: u8,
|
||||
pub lp_mint_decimals: u8,
|
||||
pub mint0_decimals: u8,
|
||||
pub mint1_decimals: u8,
|
||||
pub lp_supply: u64,
|
||||
pub protocol_fees_token0: u64,
|
||||
pub protocol_fees_token1: u64,
|
||||
pub fund_fees_token0: u64,
|
||||
pub fund_fees_token1: u64,
|
||||
pub open_time: u64,
|
||||
pub recent_epoch: u64,
|
||||
pub padding: [u64; 31],
|
||||
}
|
||||
|
||||
pub const POOL_STATE_SIZE: usize = 629;
|
||||
|
||||
pub fn pool_state_decode(data: &[u8]) -> Option<PoolState> {
|
||||
if data.len() < POOL_STATE_SIZE {
|
||||
return None;
|
||||
}
|
||||
borsh::from_slice::<PoolState>(&data[..POOL_STATE_SIZE]).ok()
|
||||
}
|
||||
+139
-100
@@ -3,19 +3,11 @@ use crate::common::bonding_curve::BondingCurveAccount;
|
||||
use crate::common::nonce_cache::DurableNonceInfo;
|
||||
use crate::common::spl_associated_token_account::get_associated_token_address_with_program_id;
|
||||
use crate::common::SolanaRpcClient;
|
||||
use solana_streamer_sdk::streaming::event_parser::common::EventType;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
|
||||
use crate::swqos::{SwqosClient, TradeType};
|
||||
use crate::trading::common::get_multi_token_balances;
|
||||
use crate::trading::MiddlewareManager;
|
||||
use solana_hash::Hash;
|
||||
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpswap::{
|
||||
PumpSwapBuyEvent, PumpSwapSellEvent,
|
||||
};
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::raydium_amm_v4::types::AmmInfo;
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::raydium_cpmm::RaydiumCpmmSwapEvent;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Swap parameters
|
||||
@@ -74,32 +66,55 @@ impl PumpFunParams {
|
||||
}
|
||||
|
||||
pub fn from_dev_trade(
|
||||
event: &PumpFunTradeEvent,
|
||||
mint: Pubkey,
|
||||
token_amount: u64,
|
||||
max_sol_cost: u64,
|
||||
creator: Pubkey,
|
||||
bonding_curve: Pubkey,
|
||||
associated_bonding_curve: Pubkey,
|
||||
creator_vault: Pubkey,
|
||||
close_token_account_when_sell: Option<bool>,
|
||||
) -> Self {
|
||||
let bonding_curve = BondingCurveAccount::from_dev_trade(
|
||||
&event.mint,
|
||||
event.token_amount,
|
||||
event.max_sol_cost,
|
||||
event.creator,
|
||||
let bonding_curve_account = BondingCurveAccount::from_dev_trade(
|
||||
bonding_curve,
|
||||
&mint,
|
||||
token_amount,
|
||||
max_sol_cost,
|
||||
creator,
|
||||
);
|
||||
Self {
|
||||
bonding_curve: Arc::new(bonding_curve),
|
||||
associated_bonding_curve: event.associated_bonding_curve,
|
||||
creator_vault: event.creator_vault,
|
||||
bonding_curve: Arc::new(bonding_curve_account),
|
||||
associated_bonding_curve: associated_bonding_curve,
|
||||
creator_vault: creator_vault,
|
||||
close_token_account_when_sell: close_token_account_when_sell,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_trade(
|
||||
event: &PumpFunTradeEvent,
|
||||
bonding_curve: Pubkey,
|
||||
associated_bonding_curve: Pubkey,
|
||||
mint: Pubkey,
|
||||
creator: Pubkey,
|
||||
creator_vault: Pubkey,
|
||||
virtual_token_reserves: u64,
|
||||
virtual_sol_reserves: u64,
|
||||
real_token_reserves: u64,
|
||||
real_sol_reserves: u64,
|
||||
close_token_account_when_sell: Option<bool>,
|
||||
) -> Self {
|
||||
let bonding_curve = BondingCurveAccount::from_trade(event);
|
||||
let bonding_curve = BondingCurveAccount::from_trade(
|
||||
bonding_curve,
|
||||
mint,
|
||||
creator,
|
||||
virtual_token_reserves,
|
||||
virtual_sol_reserves,
|
||||
real_token_reserves,
|
||||
real_sol_reserves,
|
||||
);
|
||||
Self {
|
||||
bonding_curve: Arc::new(bonding_curve),
|
||||
associated_bonding_curve: event.associated_bonding_curve,
|
||||
creator_vault: event.creator_vault,
|
||||
associated_bonding_curve: associated_bonding_curve,
|
||||
creator_vault: creator_vault,
|
||||
close_token_account_when_sell: close_token_account_when_sell,
|
||||
}
|
||||
}
|
||||
@@ -184,35 +199,31 @@ pub struct PumpSwapParams {
|
||||
}
|
||||
|
||||
impl PumpSwapParams {
|
||||
pub fn from_buy_trade(event: &PumpSwapBuyEvent) -> Self {
|
||||
pub fn new(
|
||||
pool: Pubkey,
|
||||
base_mint: Pubkey,
|
||||
quote_mint: Pubkey,
|
||||
pool_base_token_account: Pubkey,
|
||||
pool_quote_token_account: Pubkey,
|
||||
pool_base_token_reserves: u64,
|
||||
pool_quote_token_reserves: u64,
|
||||
coin_creator_vault_ata: Pubkey,
|
||||
coin_creator_vault_authority: Pubkey,
|
||||
base_token_program: Pubkey,
|
||||
quote_token_program: Pubkey,
|
||||
) -> Self {
|
||||
Self {
|
||||
pool: event.pool,
|
||||
base_mint: event.base_mint,
|
||||
quote_mint: event.quote_mint,
|
||||
pool_base_token_account: event.pool_base_token_account,
|
||||
pool_quote_token_account: event.pool_quote_token_account,
|
||||
pool_base_token_reserves: event.pool_base_token_reserves,
|
||||
pool_quote_token_reserves: event.pool_quote_token_reserves,
|
||||
coin_creator_vault_ata: event.coin_creator_vault_ata,
|
||||
coin_creator_vault_authority: event.coin_creator_vault_authority,
|
||||
base_token_program: event.base_token_program,
|
||||
quote_token_program: event.quote_token_program,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_sell_trade(event: &PumpSwapSellEvent) -> Self {
|
||||
Self {
|
||||
pool: event.pool,
|
||||
base_mint: event.base_mint,
|
||||
quote_mint: event.quote_mint,
|
||||
pool_base_token_account: event.pool_base_token_account,
|
||||
pool_quote_token_account: event.pool_quote_token_account,
|
||||
pool_base_token_reserves: event.pool_base_token_reserves,
|
||||
pool_quote_token_reserves: event.pool_quote_token_reserves,
|
||||
coin_creator_vault_ata: event.coin_creator_vault_ata,
|
||||
coin_creator_vault_authority: event.coin_creator_vault_authority,
|
||||
base_token_program: event.base_token_program,
|
||||
quote_token_program: event.quote_token_program,
|
||||
pool,
|
||||
base_mint,
|
||||
quote_mint,
|
||||
pool_base_token_account,
|
||||
pool_quote_token_account,
|
||||
pool_base_token_reserves,
|
||||
pool_quote_token_reserves,
|
||||
coin_creator_vault_ata,
|
||||
coin_creator_vault_authority,
|
||||
base_token_program,
|
||||
quote_token_program,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,31 +340,56 @@ impl BonkParams {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
pub fn from_trade(trade_info: BonkTradeEvent) -> Self {
|
||||
pub fn from_trade(
|
||||
virtual_base: u64,
|
||||
virtual_quote: u64,
|
||||
real_base_after: u64,
|
||||
real_quote_after: u64,
|
||||
pool_state: Pubkey,
|
||||
base_vault: Pubkey,
|
||||
quote_vault: Pubkey,
|
||||
base_token_program: Pubkey,
|
||||
platform_config: Pubkey,
|
||||
platform_associated_account: Pubkey,
|
||||
creator_associated_account: Pubkey,
|
||||
global_config: Pubkey,
|
||||
) -> Self {
|
||||
Self {
|
||||
virtual_base: trade_info.virtual_base as u128,
|
||||
virtual_quote: trade_info.virtual_quote as u128,
|
||||
real_base: trade_info.real_base_after as u128,
|
||||
real_quote: trade_info.real_quote_after as u128,
|
||||
pool_state: trade_info.pool_state,
|
||||
base_vault: trade_info.base_vault,
|
||||
quote_vault: trade_info.quote_vault,
|
||||
mint_token_program: trade_info.base_token_program,
|
||||
platform_config: trade_info.platform_config,
|
||||
platform_associated_account: trade_info.platform_associated_account,
|
||||
creator_associated_account: trade_info.creator_associated_account,
|
||||
global_config: trade_info.global_config,
|
||||
virtual_base: virtual_base as u128,
|
||||
virtual_quote: virtual_quote as u128,
|
||||
real_base: real_base_after as u128,
|
||||
real_quote: real_quote_after as u128,
|
||||
pool_state: pool_state,
|
||||
base_vault: base_vault,
|
||||
quote_vault: quote_vault,
|
||||
mint_token_program: base_token_program,
|
||||
platform_config: platform_config,
|
||||
platform_associated_account: platform_associated_account,
|
||||
creator_associated_account: creator_associated_account,
|
||||
global_config: global_config,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_dev_trade(trade_info: BonkTradeEvent) -> Self {
|
||||
pub fn from_dev_trade(
|
||||
is_exact_in: bool,
|
||||
amount_in: u64,
|
||||
amount_out: u64,
|
||||
pool_state: Pubkey,
|
||||
base_vault: Pubkey,
|
||||
quote_vault: Pubkey,
|
||||
base_token_program: Pubkey,
|
||||
platform_config: Pubkey,
|
||||
platform_associated_account: Pubkey,
|
||||
creator_associated_account: Pubkey,
|
||||
global_config: Pubkey,
|
||||
) -> Self {
|
||||
const DEFAULT_VIRTUAL_BASE: u128 = 1073025605596382;
|
||||
const DEFAULT_VIRTUAL_QUOTE: u128 = 30000852951;
|
||||
let amount_in = if trade_info.metadata.event_type == EventType::BonkBuyExactIn {
|
||||
trade_info.amount_in
|
||||
let _amount_in = if is_exact_in {
|
||||
amount_in
|
||||
} else {
|
||||
crate::instruction::utils::bonk::get_amount_in(
|
||||
trade_info.amount_out,
|
||||
amount_out,
|
||||
crate::instruction::utils::bonk::accounts::PROTOCOL_FEE_RATE,
|
||||
crate::instruction::utils::bonk::accounts::PLATFORM_FEE_RATE,
|
||||
crate::instruction::utils::bonk::accounts::SHARE_FEE_RATE,
|
||||
@@ -370,9 +406,9 @@ impl BonkParams {
|
||||
crate::instruction::utils::bonk::accounts::PLATFORM_FEE_RATE,
|
||||
crate::instruction::utils::bonk::accounts::SHARE_FEE_RATE,
|
||||
) as u128;
|
||||
let amount_out = if trade_info.metadata.event_type == EventType::BonkBuyExactIn {
|
||||
let _amount_out = if is_exact_in {
|
||||
crate::instruction::utils::bonk::get_amount_out(
|
||||
trade_info.amount_in,
|
||||
amount_in,
|
||||
crate::instruction::utils::bonk::accounts::PROTOCOL_FEE_RATE,
|
||||
crate::instruction::utils::bonk::accounts::PLATFORM_FEE_RATE,
|
||||
crate::instruction::utils::bonk::accounts::SHARE_FEE_RATE,
|
||||
@@ -383,22 +419,22 @@ impl BonkParams {
|
||||
0,
|
||||
) as u128
|
||||
} else {
|
||||
trade_info.amount_out as u128
|
||||
amount_out as u128
|
||||
};
|
||||
let real_base = amount_out;
|
||||
let real_base = _amount_out;
|
||||
Self {
|
||||
virtual_base: DEFAULT_VIRTUAL_BASE,
|
||||
virtual_quote: DEFAULT_VIRTUAL_QUOTE,
|
||||
real_base: real_base,
|
||||
real_quote: real_quote,
|
||||
pool_state: trade_info.pool_state,
|
||||
base_vault: trade_info.base_vault,
|
||||
quote_vault: trade_info.quote_vault,
|
||||
mint_token_program: trade_info.base_token_program,
|
||||
platform_config: trade_info.platform_config,
|
||||
platform_associated_account: trade_info.platform_associated_account,
|
||||
creator_associated_account: trade_info.creator_associated_account,
|
||||
global_config: trade_info.global_config,
|
||||
pool_state: pool_state,
|
||||
base_vault: base_vault,
|
||||
quote_vault: quote_vault,
|
||||
mint_token_program: base_token_program,
|
||||
platform_config: platform_config,
|
||||
platform_associated_account: platform_associated_account,
|
||||
creator_associated_account: creator_associated_account,
|
||||
global_config: global_config,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,7 +510,7 @@ pub struct RaydiumCpmmParams {
|
||||
pub base_vault: Pubkey,
|
||||
/// Quote token vault address
|
||||
pub quote_vault: Pubkey,
|
||||
/// Base token program ID
|
||||
/// Base token program ID
|
||||
pub base_token_program: Pubkey,
|
||||
/// Quote token program ID
|
||||
pub quote_token_program: Pubkey,
|
||||
@@ -484,22 +520,30 @@ pub struct RaydiumCpmmParams {
|
||||
|
||||
impl RaydiumCpmmParams {
|
||||
pub fn from_trade(
|
||||
trade_info: RaydiumCpmmSwapEvent,
|
||||
pool_state: Pubkey,
|
||||
amm_config: Pubkey,
|
||||
input_token_mint: Pubkey,
|
||||
output_token_mint: Pubkey,
|
||||
input_vault: Pubkey,
|
||||
output_vault: Pubkey,
|
||||
input_token_program: Pubkey,
|
||||
output_token_program: Pubkey,
|
||||
observation_state: Pubkey,
|
||||
base_reserve: u64,
|
||||
quote_reserve: u64,
|
||||
) -> Self {
|
||||
Self {
|
||||
pool_state: trade_info.pool_state,
|
||||
amm_config: trade_info.amm_config,
|
||||
base_mint: trade_info.input_token_mint,
|
||||
quote_mint: trade_info.output_token_mint,
|
||||
pool_state: pool_state,
|
||||
amm_config: amm_config,
|
||||
base_mint: input_token_mint,
|
||||
quote_mint: output_token_mint,
|
||||
base_reserve: base_reserve,
|
||||
quote_reserve: quote_reserve,
|
||||
base_vault: trade_info.input_vault,
|
||||
quote_vault: trade_info.output_vault,
|
||||
base_token_program: trade_info.input_token_program,
|
||||
quote_token_program: trade_info.output_token_program,
|
||||
observation_state: trade_info.observation_state,
|
||||
base_vault: input_vault,
|
||||
quote_vault: output_vault,
|
||||
base_token_program: input_token_program,
|
||||
quote_token_program: output_token_program,
|
||||
observation_state: observation_state,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -564,21 +608,16 @@ pub struct RaydiumAmmV4Params {
|
||||
}
|
||||
|
||||
impl RaydiumAmmV4Params {
|
||||
pub fn from_amm_info_and_reserves(
|
||||
pub fn new(
|
||||
amm: Pubkey,
|
||||
amm_info: AmmInfo,
|
||||
coin_mint: Pubkey,
|
||||
pc_mint: Pubkey,
|
||||
token_coin: Pubkey,
|
||||
token_pc: Pubkey,
|
||||
coin_reserve: u64,
|
||||
pc_reserve: u64,
|
||||
) -> Self {
|
||||
Self {
|
||||
amm,
|
||||
coin_mint: amm_info.coin_mint,
|
||||
pc_mint: amm_info.pc_mint,
|
||||
token_coin: amm_info.token_coin,
|
||||
token_pc: amm_info.token_pc,
|
||||
coin_reserve,
|
||||
pc_reserve,
|
||||
}
|
||||
Self { amm, coin_mint, pc_mint, token_coin, token_pc, coin_reserve, pc_reserve }
|
||||
}
|
||||
pub async fn from_amm_address_by_rpc(
|
||||
rpc: &SolanaRpcClient,
|
||||
|
||||
+1
-46
@@ -1,49 +1,4 @@
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::bonk::types::PoolState;
|
||||
|
||||
use crate::{
|
||||
constants::decimals::{DEFAULT_TOKEN_DECIMALS, SOL_DECIMALS},
|
||||
constants::WSOL_TOKEN_ACCOUNT,
|
||||
};
|
||||
|
||||
/// Calculate the token price in WSOL based on pool state
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `pool_state` - Pool state
|
||||
///
|
||||
/// # Returns
|
||||
/// Token price in WSOL as f64
|
||||
pub fn price_token_in_wsol_with_pool_state(pool_state: &PoolState) -> f64 {
|
||||
if pool_state.quote_mint != WSOL_TOKEN_ACCOUNT {
|
||||
log::error!("Quote mint is not WSOL: {:?}", pool_state.quote_mint);
|
||||
return 0.0;
|
||||
}
|
||||
price_base_in_quote(
|
||||
pool_state.virtual_base,
|
||||
pool_state.virtual_quote,
|
||||
pool_state.real_base,
|
||||
pool_state.real_quote,
|
||||
pool_state.base_decimals,
|
||||
pool_state.quote_decimals,
|
||||
)
|
||||
}
|
||||
|
||||
/// Calculate the price of base in quote based on pool state
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `pool_state` - Pool state
|
||||
///
|
||||
/// # Returns
|
||||
/// The price of base in quote
|
||||
pub fn price_base_in_quote_with_pool_state(pool_state: &PoolState) -> f64 {
|
||||
price_base_in_quote(
|
||||
pool_state.virtual_base,
|
||||
pool_state.virtual_quote,
|
||||
pool_state.real_base,
|
||||
pool_state.real_quote,
|
||||
pool_state.base_decimals,
|
||||
pool_state.quote_decimals,
|
||||
)
|
||||
}
|
||||
use crate::constants::decimals::{DEFAULT_TOKEN_DECIMALS, SOL_DECIMALS};
|
||||
|
||||
/// Calculate the price of token in WSOL
|
||||
///
|
||||
|
||||
@@ -1,18 +1,5 @@
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::types::BondingCurve;
|
||||
|
||||
use crate::instruction::utils::pumpfun::global_constants::{LAMPORTS_PER_SOL, SCALE};
|
||||
|
||||
/// Calculate the token price in SOL based on virtual reserves
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `bonding_curve` - Bonding curve account
|
||||
///
|
||||
/// # Returns
|
||||
/// Token price in SOL as f64
|
||||
pub fn price_token_in_sol_with_bonding_curve(bonding_curve: &BondingCurve) -> f64 {
|
||||
price_token_in_sol(bonding_curve.virtual_sol_reserves, bonding_curve.virtual_token_reserves)
|
||||
}
|
||||
|
||||
/// Calculate the token price in SOL based on virtual reserves
|
||||
///
|
||||
/// # Arguments
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use solana_streamer_sdk::streaming::event_parser::protocols::raydium_clmm::types::PoolState;
|
||||
|
||||
/// Calculate the price of token0 in token1
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -36,33 +34,3 @@ pub fn price_token1_in_token0(
|
||||
) -> f64 {
|
||||
1.0 / price_token0_in_token1(sqrt_price_x64, decimals_token0, decimals_token1)
|
||||
}
|
||||
|
||||
/// Calculate the price of token0 in token1 based on pool state
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `pool_state` - The pool state
|
||||
///
|
||||
/// # Returns
|
||||
/// The price of token0 in token1
|
||||
pub fn price_token0_in_token1_with_pool_state(pool_state: &PoolState) -> f64 {
|
||||
price_token0_in_token1(
|
||||
pool_state.sqrt_price_x64,
|
||||
pool_state.mint_decimals0,
|
||||
pool_state.mint_decimals1,
|
||||
)
|
||||
}
|
||||
|
||||
/// Calculate the price of token1 in token0 based on pool state
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `pool_state` - The pool state
|
||||
///
|
||||
/// # Returns
|
||||
/// The price of token1 in token0
|
||||
pub fn price_token1_in_token0_with_pool_state(pool_state: &PoolState) -> f64 {
|
||||
price_token1_in_token0(
|
||||
pool_state.sqrt_price_x64,
|
||||
pool_state.mint_decimals0,
|
||||
pool_state.mint_decimals1,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user