Skip to main content

sol_trade_sdk/common/
types.rs

1use crate::swqos::SwqosConfig;
2use solana_commitment_config::CommitmentConfig;
3use std::hash::{Hash, Hasher};
4
5/// Infrastructure-only configuration (wallet-independent)
6/// Can be shared across multiple wallets using the same RPC/SWQOS setup
7#[derive(Debug, Clone)]
8pub struct InfrastructureConfig {
9    pub rpc_url: String,
10    pub swqos_configs: Vec<SwqosConfig>,
11    pub commitment: CommitmentConfig,
12}
13
14impl InfrastructureConfig {
15    pub fn new(
16        rpc_url: String,
17        swqos_configs: Vec<SwqosConfig>,
18        commitment: CommitmentConfig,
19    ) -> Self {
20        Self {
21            rpc_url,
22            swqos_configs,
23            commitment,
24        }
25    }
26
27    /// Create from TradeConfig (extract infrastructure-only settings)
28    pub fn from_trade_config(config: &TradeConfig) -> Self {
29        Self {
30            rpc_url: config.rpc_url.clone(),
31            swqos_configs: config.swqos_configs.clone(),
32            commitment: config.commitment.clone(),
33        }
34    }
35
36    /// Generate a cache key for this infrastructure configuration
37    pub fn cache_key(&self) -> String {
38        use std::collections::hash_map::DefaultHasher;
39        let mut hasher = DefaultHasher::new();
40        self.hash(&mut hasher);
41        format!("{:x}", hasher.finish())
42    }
43}
44
45// Manual Hash implementation since CommitmentConfig doesn't implement Hash
46impl Hash for InfrastructureConfig {
47    fn hash<H: Hasher>(&self, state: &mut H) {
48        self.rpc_url.hash(state);
49        self.swqos_configs.hash(state);
50        // Hash commitment level as string since CommitmentConfig doesn't impl Hash
51        format!("{:?}", self.commitment).hash(state);
52    }
53}
54
55impl PartialEq for InfrastructureConfig {
56    fn eq(&self, other: &Self) -> bool {
57        self.rpc_url == other.rpc_url
58            && self.swqos_configs == other.swqos_configs
59            && self.commitment == other.commitment
60    }
61}
62
63impl Eq for InfrastructureConfig {}
64
65#[derive(Debug, Clone)]
66pub struct TradeConfig {
67    pub rpc_url: String,
68    pub swqos_configs: Vec<SwqosConfig>,
69    pub commitment: CommitmentConfig,
70    /// Whether to create WSOL ATA on startup (default: true)
71    /// If true, SDK will check WSOL ATA on initialization and create if not exists
72    pub create_wsol_ata_on_startup: bool,
73    /// Whether to use seed optimization for all ATA operations (default: true)
74    pub use_seed_optimize: bool,
75}
76
77impl TradeConfig {
78    pub fn new(
79        rpc_url: String,
80        swqos_configs: Vec<SwqosConfig>,
81        commitment: CommitmentConfig,
82    ) -> Self {
83        println!("🔧 TradeConfig create_wsol_ata_on_startup default value: true");
84        println!("🔧 TradeConfig use_seed_optimize default value: true");
85        Self {
86            rpc_url,
87            swqos_configs,
88            commitment,
89            create_wsol_ata_on_startup: true,  // 默认:启动时检查并创建
90            use_seed_optimize: true,           // 默认:使用seed优化
91        }
92    }
93
94    /// Create a TradeConfig with custom WSOL ATA settings
95    pub fn with_wsol_ata_config(
96        mut self,
97        create_wsol_ata_on_startup: bool,
98        use_seed_optimize: bool,
99    ) -> Self {
100        self.create_wsol_ata_on_startup = create_wsol_ata_on_startup;
101        self.use_seed_optimize = use_seed_optimize;
102        self
103    }
104}
105
106pub type SolanaRpcClient = solana_client::nonblocking::rpc_client::RpcClient;
107pub type AnyResult<T> = anyhow::Result<T>;