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

172 lines
6.5 KiB
Rust
Raw Normal View History

2025-07-06 22:06:44 +08:00
2025-07-07 01:01:38 +08:00
use crate::swqos::common::{poll_transaction_confirmation, serialize_transaction_and_encode, FormatBase64VersionedTransaction};
use rand::seq::IndexedRandom;
use reqwest::Client;
use serde_json::json;
use std::{sync::Arc, time::Instant};
2025-07-06 22:06:44 +08:00
2025-07-07 01:01:38 +08:00
use std::time::Duration;
use solana_transaction_status::UiTransactionEncoding;
use anyhow::Result;
use solana_sdk::transaction::VersionedTransaction;
2025-07-06 23:36:18 +08:00
use crate::swqos::{SwqosType, TradeType};
2025-07-06 22:06:44 +08:00
use crate::swqos::SwqosClientTrait;
2025-07-07 02:06:52 +08:00
use crate::{common::SolanaRpcClient, constants::swqos::JITO_TIP_ACCOUNTS};
2025-07-06 22:06:44 +08:00
pub struct JitoClient {
2025-07-07 01:01:38 +08:00
pub endpoint: String,
pub auth_token: String,
2025-07-06 22:06:44 +08:00
pub rpc_client: Arc<SolanaRpcClient>,
2025-07-07 01:01:38 +08:00
pub http_client: Client,
2025-07-06 22:06:44 +08:00
}
#[async_trait::async_trait]
impl SwqosClientTrait for JitoClient {
async fn send_transaction(&self, trade_type: TradeType, transaction: &VersionedTransaction, wait_confirmation: bool) -> Result<()> {
self.send_transaction_impl(trade_type, transaction, wait_confirmation).await
2025-07-06 22:06:44 +08:00
}
async fn send_transactions(&self, trade_type: TradeType, transactions: &Vec<VersionedTransaction>, wait_confirmation: bool) -> Result<()> {
self.send_transactions_impl(trade_type, transactions, wait_confirmation).await
2025-07-06 22:06:44 +08:00
}
fn get_tip_account(&self) -> Result<String> {
2025-07-07 01:01:38 +08:00
if let Some(acc) = JITO_TIP_ACCOUNTS.choose(&mut rand::rng()) {
2025-07-06 22:06:44 +08:00
Ok(acc.to_string())
} else {
2025-07-07 01:01:38 +08:00
Err(anyhow::anyhow!("no valid tip accounts found"))
2025-07-06 22:06:44 +08:00
}
}
2025-07-06 23:36:18 +08:00
fn get_swqos_type(&self) -> SwqosType {
SwqosType::Jito
2025-07-06 22:06:44 +08:00
}
}
impl JitoClient {
2025-07-07 01:01:38 +08:00
pub fn new(rpc_url: String, endpoint: String, auth_token: String) -> Self {
2025-07-06 22:06:44 +08:00
let rpc_client = SolanaRpcClient::new(rpc_url);
2025-07-07 01:01:38 +08:00
let http_client = Client::builder()
2025-10-06 23:40:27 +08:00
// Optimized connection pool settings for high performance
.pool_idle_timeout(Duration::from_secs(120))
.pool_max_idle_per_host(256) // Increased from 64 to 256
.tcp_keepalive(Some(Duration::from_secs(60))) // Reduced from 1200 to 60
.tcp_nodelay(true) // Disable Nagle's algorithm for lower latency
.http2_keep_alive_interval(Duration::from_secs(10))
.http2_keep_alive_timeout(Duration::from_secs(5))
.http2_adaptive_window(true) // Enable adaptive flow control
.timeout(Duration::from_millis(3000)) // Reduced from 10s to 3s
.connect_timeout(Duration::from_millis(2000)) // Reduced from 5s to 2s
2025-07-07 01:01:38 +08:00
.build()
.unwrap();
Self { rpc_client: Arc::new(rpc_client), endpoint, auth_token, http_client }
2025-07-06 22:06:44 +08:00
}
pub async fn send_transaction_impl(&self, trade_type: TradeType, transaction: &VersionedTransaction, wait_confirmation: bool) -> Result<()> {
2025-07-07 01:01:38 +08:00
let start_time = Instant::now();
let (content, signature) = serialize_transaction_and_encode(transaction, UiTransactionEncoding::Base64).await?;
let request_body = serde_json::to_string(&json!({
"id": 1,
"jsonrpc": "2.0",
2025-07-07 01:01:38 +08:00
"method": "sendTransaction",
"params": [
content,
{
"encoding": "base64"
}
]
}))?;
let endpoint = if self.auth_token.is_empty() {
format!("{}/api/v1/transactions", self.endpoint)
} else {
format!("{}/api/v1/transactions?uuid={}", self.endpoint, self.auth_token)
};
let response = if self.auth_token.is_empty() {
self.http_client.post(&endpoint)
} else {
self.http_client.post(&endpoint)
.header("x-jito-auth", &self.auth_token)
};
let response_text = response
2025-07-07 01:01:38 +08:00
.body(request_body)
.header("Content-Type", "application/json")
.send()
.await?
.text()
.await?;
if let Ok(response_json) = serde_json::from_str::<serde_json::Value>(&response_text) {
if response_json.get("result").is_some() {
println!(" [jito] {} submitted: {:?}", trade_type, start_time.elapsed());
2025-07-07 01:01:38 +08:00
} else if let Some(_error) = response_json.get("error") {
eprintln!(" [jito] {} submission failed: {:?}", trade_type, _error);
2025-07-07 01:01:38 +08:00
}
2025-08-27 14:27:26 +08:00
} else {
eprintln!(" [jito] {} submission failed: {:?}", trade_type, response_text);
2025-07-07 01:01:38 +08:00
}
let start_time: Instant = Instant::now();
match poll_transaction_confirmation(&self.rpc_client, signature, wait_confirmation).await {
2025-07-07 01:01:38 +08:00
Ok(_) => (),
Err(e) => {
2025-09-03 22:38:41 +08:00
println!(" signature: {:?}", signature);
println!(" [jito] {} confirmation failed: {:?}", trade_type, start_time.elapsed());
return Err(e);
},
2025-07-07 01:01:38 +08:00
}
if wait_confirmation {
println!(" signature: {:?}", signature);
println!(" [jito] {} confirmed: {:?}", trade_type, start_time.elapsed());
}
2025-07-07 01:01:38 +08:00
Ok(())
}
pub async fn send_transactions_impl(&self, trade_type: TradeType, transactions: &Vec<VersionedTransaction>, _wait_confirmation: bool) -> Result<()> {
2025-07-07 01:01:38 +08:00
let start_time = Instant::now();
let txs_base64 = transactions.iter().map(|tx| tx.to_base64_string()).collect::<Vec<String>>();
let body = serde_json::json!({
"jsonrpc": "2.0",
"method": "sendBundle",
"params": [
txs_base64,
{ "encoding": "base64" }
],
"id": 1,
});
let endpoint = if self.auth_token.is_empty() {
format!("{}/api/v1/bundles", self.endpoint)
} else {
format!("{}/api/v1/bundles?uuid={}", self.endpoint, self.auth_token)
};
let response = if self.auth_token.is_empty() {
self.http_client.post(&endpoint)
} else {
self.http_client.post(&endpoint)
.header("x-jito-auth", &self.auth_token)
};
let response_text = response
2025-07-07 01:01:38 +08:00
.body(body.to_string())
.header("Content-Type", "application/json")
.send()
.await?
.text()
.await?;
if let Ok(response_json) = serde_json::from_str::<serde_json::Value>(&response_text) {
if response_json.get("result").is_some() {
2025-08-27 14:27:26 +08:00
println!(" jito {} submitted: {:?}", trade_type, start_time.elapsed());
2025-07-07 01:01:38 +08:00
} else if let Some(_error) = response_json.get("error") {
2025-08-27 14:27:26 +08:00
eprintln!(" jito {} submission failed: {:?}", trade_type, _error);
2025-07-07 01:01:38 +08:00
}
}
Ok(())
2025-07-06 22:06:44 +08:00
}
}