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

181 lines
6.0 KiB
Rust
Raw Normal View History

2026-03-14 18:16:55 +02:00
use crate::swqos::common::{
default_http_client_builder, poll_transaction_confirmation, serialize_transaction_and_encode,
FormatBase64VersionedTransaction,
};
2025-07-07 01:01:38 +08:00
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 solana_transaction_status::UiTransactionEncoding;
2026-03-14 18:16:55 +02:00
use crate::swqos::SwqosClientTrait;
use crate::swqos::{SwqosType, TradeType};
2025-07-07 01:01:38 +08:00
use anyhow::Result;
use solana_sdk::transaction::VersionedTransaction;
2025-07-06 22:06:44 +08:00
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 {
2026-03-14 18:16:55 +02:00
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
}
2026-03-14 18:16:55 +02: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);
2026-02-27 02:30:52 +08:00
let http_client = default_http_client_builder().build().unwrap();
2025-07-07 01:01:38 +08:00
Self { rpc_client: Arc::new(rpc_client), endpoint, auth_token, http_client }
2025-07-06 22:06:44 +08:00
}
2026-03-14 18:16:55 +02: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();
2026-03-14 18:16:55 +02:00
let (content, signature) =
serialize_transaction_and_encode(transaction, UiTransactionEncoding::Base64)?;
2025-07-07 01:01:38 +08:00
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 {
2026-03-14 18:16:55 +02:00
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);
2026-03-14 18:16:55 +02:00
}
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(())
}
2026-03-14 18:16:55 +02:00
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();
2026-03-14 18:16:55 +02:00
let txs_base64 =
transactions.iter().map(|tx| tx.to_base64_string()).collect::<Vec<String>>();
2025-07-07 01:01:38 +08:00
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 {
2026-03-14 18:16:55 +02:00
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
}
2026-03-14 18:16:55 +02:00
}