diff --git a/src/accounts/mod.rs b/src/accounts/mod.rs deleted file mode 100755 index f77567c..0000000 --- a/src/accounts/mod.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Accounts for the Pump.fun Solana Program -//! -//! This module contains the definitions for the accounts used by the Pump.fun program. -//! -//! # Accounts -//! -//! - `BondingCurve`: Represents a bonding curve account. -//! - `Global`: Represents the global configuration account. - -mod bonding_curve; -mod global; - -pub use bonding_curve::*; -pub use global::*; diff --git a/src/accounts/bonding_curve.rs b/src/common/bonding_curve.rs similarity index 100% rename from src/accounts/bonding_curve.rs rename to src/common/bonding_curve.rs diff --git a/src/accounts/global.rs b/src/common/global.rs similarity index 100% rename from src/accounts/global.rs rename to src/common/global.rs diff --git a/src/common/mod.rs b/src/common/mod.rs index 5d8d161..e421d9d 100755 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -4,5 +4,7 @@ pub mod tip_cache; pub mod types; pub mod address_lookup_cache; pub mod subscription_handle; +pub mod bonding_curve; +pub mod global; pub use types::*; diff --git a/src/error/mod.rs b/src/error/mod.rs deleted file mode 100755 index 24f4b57..0000000 --- a/src/error/mod.rs +++ /dev/null @@ -1,197 +0,0 @@ -//! Error types for the Pump.fun SDK. -//! -//! This module defines the `ClientError` enum, which encompasses various error types that can occur when interacting with the Pump.fun program. -//! It includes specific error cases for bonding curve operations, metadata uploads, Solana client errors, and more. -//! -//! The `ClientError` enum provides a comprehensive set of error types to help developers handle and debug issues that may arise during interactions with the Pump.fun program. -//! -//! # Error Types -//! -//! - `BondingCurveNotFound`: The bonding curve account was not found. -//! - `BondingCurveError`: An error occurred while interacting with the bonding curve. -//! - `BorshError`: An error occurred while serializing or deserializing data using Borsh. -//! - `SolanaClientError`: An error occurred while interacting with the Solana RPC client. -//! - `UploadMetadataError`: An error occurred while uploading metadata to IPFS. -//! - `InvalidInput`: Invalid input parameters were provided. -//! - `InsufficientFunds`: Insufficient funds for a transaction. -//! - `SimulationError`: Transaction simulation failed. -//! - `RateLimitExceeded`: Rate limit exceeded. - -use serde_json::Error; -use solana_client::{ - client_error::ClientError as SolanaClientError, - pubsub_client::PubsubClientError -}; -use solana_sdk::pubkey::ParsePubkeyError; - -// #[derive(Debug)] -// #[allow(dead_code)] -// pub struct AppError(anyhow::Error); - -// impl From for AppError -// where -// E: Into, -// { -// fn from(err: E) -> Self { -// Self(err.into()) -// } -// } - -#[derive(Debug)] -pub enum ClientError { - /// Bonding curve account was not found - BondingCurveNotFound, - /// Error related to bonding curve operations - BondingCurveError(&'static str), - /// Error deserializing data using Borsh - BorshError(std::io::Error), - /// Error from Solana RPC client - SolanaClientError(solana_client::client_error::ClientError), - /// Error uploading metadata - UploadMetadataError(Box), - /// Invalid input parameters - InvalidInput(&'static str), - /// Insufficient funds for transaction - InsufficientFunds, - /// Transaction simulation failed - SimulationError(String), - /// Rate limit exceeded - RateLimitExceeded, - - OrderLimitExceeded, - - ExternalService(String), - - Redis(String, String), - - Solana(String, String), - - Parse(String, String), - - Pubkey(String, String), - - Jito(String, String), - - Join(String), - - Subscribe(String, String), - - Send(String, String), - - Other(String), - - Anyhow(&'static str), - - InvalidData(String), - - PumpFunBuy(String), - - PumpFunSell(String), - - Timeout(String, String), - - Duplicate(String), - - InvalidEventType, - - ChannelClosed, -} - -impl std::fmt::Display for ClientError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::BondingCurveNotFound => write!(f, "Bonding curve not found"), - Self::BondingCurveError(msg) => write!(f, "Bonding curve error: {}", msg), - Self::BorshError(err) => write!(f, "Borsh serialization error: {}", err), - Self::SolanaClientError(err) => write!(f, "Solana client error: {}", err), - Self::UploadMetadataError(err) => write!(f, "Metadata upload error: {}", err), - Self::InvalidInput(msg) => write!(f, "Invalid input: {}", msg), - Self::InsufficientFunds => write!(f, "Insufficient funds for transaction"), - Self::SimulationError(msg) => write!(f, "Transaction simulation failed: {}", msg), - Self::ExternalService(msg) => write!(f, "External service error: {}", msg), - Self::RateLimitExceeded => write!(f, "Rate limit exceeded"), - Self::OrderLimitExceeded => write!(f, "Order limit exceeded"), - Self::Anyhow(msg) => write!(f, "Anyhow error: {}", msg), - Self::Solana(msg, details) => write!(f, "Solana error: {}, details: {}", msg, details), - Self::Parse(msg, details) => write!(f, "Parse error: {}, details: {}", msg, details), - Self::Jito(msg, details) => write!(f, "Jito error: {}, details: {}", msg, details), - Self::Redis(msg, details) => write!(f, "Redis error: {}, details: {}", msg, details), - Self::Join(msg) => write!(f, "Task join error: {}", msg), - Self::Pubkey(msg, details) => write!(f, "Pubkey error: {}, details: {}", msg, details), - Self::Subscribe(msg, details) => write!(f, "Subscribe error: {}, details: {}", msg, details), - Self::Send(msg, details) => write!(f, "Send error: {}, details: {}", msg, details), - Self::Other(msg) => write!(f, "Other error: {}", msg), - Self::PumpFunBuy(msg) => write!(f, "PumpFun buy error: {}", msg), - Self::PumpFunSell(msg) => write!(f, "PumpFun sell error: {}", msg), - Self::InvalidData(msg) => write!(f, "Invalid data: {}", msg), - Self::Timeout(msg, details) => write!(f, "Operation timed out: {}, details: {}", msg, details), - Self::Duplicate(msg) => write!(f, "Duplicate event: {}", msg), - Self::InvalidEventType => write!(f, "Invalid event type"), - Self::ChannelClosed => write!(f, "Channel closed"), - } - } -} -impl std::error::Error for ClientError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Self::BorshError(err) => Some(err), - Self::SolanaClientError(err) => Some(err), - Self::UploadMetadataError(err) => Some(err.as_ref()), - Self::ExternalService(_) => None, - Self::Redis(_, _) => None, - Self::Solana(_, _) => None, - Self::Parse(_, _) => None, - Self::Jito(_, _) => None, - Self::Join(_) => None, - Self::Pubkey(_, _) => None, - Self::Subscribe(_, _) => None, - Self::Send(_, _) => None, - Self::Other(_) => None, - Self::PumpFunBuy(_) => None, - Self::PumpFunSell(_) => None, - Self::Timeout(_, _) => None, - Self::Duplicate(_) => None, - Self::InvalidEventType => None, - Self::ChannelClosed => None, - _ => None, - } - } -} - -impl From for ClientError { - fn from(error: SolanaClientError) -> Self { - ClientError::Solana( - "Solana client error".to_string(), - error.to_string(), - ) - } -} - -impl From for ClientError { - fn from(error: PubsubClientError) -> Self { - ClientError::Solana( - "PubSub client error".to_string(), - error.to_string(), - ) - } -} - -impl From for ClientError { - fn from(error: ParsePubkeyError) -> Self { - ClientError::Pubkey( - "Pubkey error".to_string(), - error.to_string(), - ) - } -} - -impl From for ClientError { - fn from(err: Error) -> Self { - ClientError::Parse( - "JSON serialization error".to_string(), - err.to_string() - ) - } -} - -pub type ClientResult = Result; diff --git a/src/event_parser/core/traits.rs b/src/event_parser/core/traits.rs index cf9213b..58e0f3e 100644 --- a/src/event_parser/core/traits.rs +++ b/src/event_parser/core/traits.rs @@ -9,7 +9,6 @@ use std::collections::HashMap; use std::fmt::Debug; use crate::{ - error, event_parser::{common::{utils::*, EventMetadata, EventType, ProtocolType}, protocols::{pumpfun::{PumpFunCreateTokenEvent, PumpFunTradeEvent}, bonk::{BonkPoolCreateEvent, BonkTradeEvent}}}, }; diff --git a/src/lib.rs b/src/lib.rs index ef86a5c..f7142ff 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ -pub mod accounts; pub mod common; pub mod constants; -pub mod error; pub mod event_parser; pub mod grpc; pub mod instruction; @@ -22,7 +20,6 @@ use swqos::SwqosClient; use common::{PriorityFee, SolanaRpcClient, TradeConfig}; -use accounts::BondingCurveAccount; use constants::trade_platform::{PUMPFUN, PUMPFUN_SWAP, BONK}; use constants::trade_type::{COPY_BUY, SNIPER_BUY}; diff --git a/src/main.rs b/src/main.rs index d970b9b..6318fd5 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use std::{str::FromStr, sync::Arc}; use sol_trade_sdk::{ - accounts::BondingCurveAccount, - common::{AnyResult, PriorityFee, TradeConfig}, + common::{bonding_curve::BondingCurveAccount, AnyResult, PriorityFee, TradeConfig}, constants::{pumpfun::global_constants::TOKEN_TOTAL_SUPPLY, trade_type}, event_parser::{ protocols::{ diff --git a/src/trading/core/params.rs b/src/trading/core/params.rs index 201d312..e50403f 100755 --- a/src/trading/core/params.rs +++ b/src/trading/core/params.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use super::traits::ProtocolParams; use crate::common::{PriorityFee, SolanaRpcClient}; use crate::swqos::SwqosClient; -use crate::accounts::BondingCurveAccount; +use crate::common::bonding_curve::BondingCurveAccount; /// 通用买入参数 #[derive(Clone)] diff --git a/src/trading/pumpfun/buy.rs b/src/trading/pumpfun/buy.rs index f23325a..73c4c7b 100755 --- a/src/trading/pumpfun/buy.rs +++ b/src/trading/pumpfun/buy.rs @@ -1,6 +1,5 @@ -use crate::accounts::BondingCurveAccount; use crate::{ - common::{PriorityFee, SolanaRpcClient}, + common::{bonding_curve::BondingCurveAccount, PriorityFee, SolanaRpcClient}, swqos::SwqosClient, trading::{core::params::PumpFunParams, factory::Protocol, BuyParams, TradeFactory}, }; diff --git a/src/trading/pumpfun/common.rs b/src/trading/pumpfun/common.rs index bab2a1b..65f02ea 100755 --- a/src/trading/pumpfun/common.rs +++ b/src/trading/pumpfun/common.rs @@ -8,10 +8,10 @@ use solana_sdk::{ }; use spl_associated_token_account::get_associated_token_address; use pumpfun_program::accounts::BondingCurveAccount as PumpfunBondingCurveAccount; -use crate::{accounts::{self, BondingCurveAccount}, common::{PriorityFee, SolanaRpcClient}, constants::{self, pumpfun::{self, global_constants::{CREATOR_FEE, FEE_BASIS_POINTS}, trade::DEFAULT_SLIPPAGE}}, event_parser::protocols::pumpfun::PumpFunTradeEvent}; +use crate::{common::{bonding_curve::BondingCurveAccount, global::GlobalAccount, PriorityFee, SolanaRpcClient}, constants::{self, pumpfun::{self, global_constants::{CREATOR_FEE, FEE_BASIS_POINTS}, trade::DEFAULT_SLIPPAGE}}, event_parser::protocols::pumpfun::PumpFunTradeEvent}; lazy_static::lazy_static! { - static ref ACCOUNT_CACHE: RwLock>> = RwLock::new(HashMap::new()); + static ref ACCOUNT_CACHE: RwLock>> = RwLock::new(HashMap::new()); } pub async fn transfer_sol(rpc: &SolanaRpcClient, payer: &Keypair, receive_wallet: &Pubkey, amount: u64) -> Result<(), anyhow::Error> { @@ -185,13 +185,13 @@ pub fn get_metadata_pda(mint: &Pubkey) -> Pubkey { } #[inline] -pub async fn get_global_account(/*rpc: &SolanaRpcClient*/) -> Result, anyhow::Error> { +pub async fn get_global_account(/*rpc: &SolanaRpcClient*/) -> Result, anyhow::Error> { // let global = constants::global_constants::GLOBAL_ACCOUNT; // if let Some(account) = ACCOUNT_CACHE.read().await.get(&global) { // return Ok(account.clone()); // } - let global_account = accounts::GlobalAccount::new(); + let global_account = GlobalAccount::new(); // let account = rpc.get_account(&global).await?; // let global_account = bincode::deserialize::(&account.data)?; @@ -202,7 +202,7 @@ pub async fn get_global_account(/*rpc: &SolanaRpcClient*/) -> Result, amount_sol: u64) -> Result { +pub async fn get_initial_buy_price(global_account: &Arc, amount_sol: u64) -> Result { let buy_amount = global_account.get_initial_buy_price(amount_sol); Ok(buy_amount) } @@ -211,7 +211,7 @@ pub async fn get_initial_buy_price(global_account: &Arc pub async fn get_bonding_curve_account( rpc: &SolanaRpcClient, mint: &Pubkey, -) -> Result<(Arc, Pubkey), anyhow::Error> { +) -> Result<(Arc, Pubkey), anyhow::Error> { let bonding_curve_pda = get_bonding_curve_pda(mint) .ok_or(anyhow!("Bonding curve not found"))?; @@ -220,7 +220,7 @@ pub async fn get_bonding_curve_account( return Err(anyhow!("Bonding curve not found")); } - let bonding_curve = Arc::new(bincode::deserialize::(&account.data)?); + let bonding_curve = Arc::new(bincode::deserialize::(&account.data)?); Ok((bonding_curve, bonding_curve_pda)) }