Files
sol-trade-sdk/src/swqos/mod.rs
T

92 lines
2.5 KiB
Rust
Raw Normal View History

2025-04-02 13:39:59 +08:00
pub mod common;
2025-07-06 22:06:44 +08:00
pub mod solana_rpc;
pub mod jito;
pub mod nextblock;
pub mod zeroslot;
2025-07-06 23:36:18 +08:00
pub mod temporal;
pub mod define;
2025-07-06 22:06:44 +08:00
use solana_sdk::transaction::VersionedTransaction;
use tokio::sync::RwLock;
use anyhow::Result;
2025-02-13 20:44:20 +08:00
2025-07-06 23:36:18 +08:00
use crate::swqos::define::{SWQOS_ENDPOINTS_JITO, SWQOS_ENDPOINTS_NEXTBLOCK, SWQOS_ENDPOINTS_TEMPORAL, SWQOS_ENDPOINTS_ZERO_SLOT};
2025-04-02 13:39:59 +08:00
lazy_static::lazy_static! {
static ref TIP_ACCOUNT_CACHE: RwLock<Vec<String>> = RwLock::new(Vec::new());
2025-01-14 19:18:48 +08:00
}
2025-04-02 13:39:59 +08:00
#[derive(Debug, Clone, Copy)]
pub enum TradeType {
Create,
CreateAndBuy,
Buy,
Sell,
}
impl std::fmt::Display for TradeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
TradeType::Create => "创建",
TradeType::CreateAndBuy => "创建并买入",
TradeType::Buy => "买入",
TradeType::Sell => "卖出",
};
write!(f, "{}", s)
}
}
2025-07-06 23:36:18 +08:00
#[derive(Debug, Clone, PartialEq)]
pub enum SwqosType {
2025-04-02 13:39:59 +08:00
Jito,
NextBlock,
ZeroSlot,
2025-07-06 23:36:18 +08:00
Temporal,
Rpc,
2025-04-02 13:39:59 +08:00
}
2025-07-06 22:06:44 +08:00
pub type SwqosClient = dyn SwqosClientTrait + Send + Sync + 'static;
2025-04-02 13:39:59 +08:00
#[async_trait::async_trait]
2025-07-06 22:06:44 +08:00
pub trait SwqosClientTrait {
2025-07-07 01:01:38 +08:00
async fn send_transaction(&self, trade_type: TradeType, transaction: &VersionedTransaction) -> Result<()>;
async fn send_transactions(&self, trade_type: TradeType, transactions: &Vec<VersionedTransaction>) -> Result<()>;
fn get_tip_account(&self) -> Result<String>;
2025-07-06 23:36:18 +08:00
fn get_swqos_type(&self) -> SwqosType;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SwqosRegion {
NewYork,
Frankfurt,
2025-07-07 01:19:12 +08:00
Amsterdam,
SLC,
Tokyo,
London,
LosAngeles,
Default,
2025-07-06 23:36:18 +08:00
}
#[derive(Debug, Clone)]
pub struct SwqosConfig {
pub endpoint: String,
pub auth_token: String,
pub swqos_type: SwqosType,
}
impl SwqosConfig {
pub fn new(endpoint: Option<String>, auth_token: Option<String>, swqos_type: SwqosType, region: SwqosRegion) -> Self {
2025-07-07 01:26:24 +08:00
let auth_token = auth_token.unwrap_or_else(|| "".to_string());
2025-07-06 23:36:18 +08:00
let endpoint = endpoint.unwrap_or_else(|| match swqos_type {
SwqosType::Jito => SWQOS_ENDPOINTS_JITO[region as usize].to_string(),
SwqosType::NextBlock => SWQOS_ENDPOINTS_NEXTBLOCK[region as usize].to_string(),
SwqosType::ZeroSlot => SWQOS_ENDPOINTS_ZERO_SLOT[region as usize].to_string(),
SwqosType::Temporal => SWQOS_ENDPOINTS_TEMPORAL[region as usize].to_string(),
SwqosType::Rpc => "".to_string(),
});
Self { endpoint, auth_token, swqos_type }
}
2025-04-02 13:39:59 +08:00
}