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

143 lines
4.6 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,
};
2025-07-06 22:06:44 +08:00
use rand::seq::IndexedRandom;
2025-07-07 01:01:38 +08:00
use reqwest::Client;
use serde_json::json;
2025-07-06 22:06:44 +08:00
use std::{sync::Arc, time::Instant};
use solana_transaction_status::UiTransactionEncoding;
2026-03-14 18:16:55 +02:00
use crate::swqos::SwqosClientTrait;
use crate::swqos::{SwqosType, TradeType};
2025-07-06 22:06:44 +08:00
use anyhow::Result;
use solana_sdk::transaction::VersionedTransaction;
2025-07-07 02:06:52 +08:00
use crate::{common::SolanaRpcClient, constants::swqos::NEXTBLOCK_TIP_ACCOUNTS};
2025-07-06 22:06:44 +08:00
#[derive(Clone)]
pub struct NextBlockClient {
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 NextBlockClient {
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(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(trade_type, transactions, wait_confirmation).await
2025-07-06 22:06:44 +08:00
}
fn get_tip_account(&self) -> Result<String> {
2026-03-14 18:16:55 +02:00
let tip_account = *NEXTBLOCK_TIP_ACCOUNTS
.choose(&mut rand::rng())
.or_else(|| NEXTBLOCK_TIP_ACCOUNTS.first())
.unwrap();
2025-07-06 22:06:44 +08:00
Ok(tip_account.to_string())
}
2025-07-06 23:36:18 +08:00
fn get_swqos_type(&self) -> SwqosType {
SwqosType::NextBlock
2025-07-06 22:06:44 +08:00
}
}
impl NextBlockClient {
pub fn new(rpc_url: String, endpoint: String, auth_token: String) -> Self {
2025-09-06 23:16:55 +08:00
// Ensure endpoint ends with /api/v2/submit
let endpoint = if endpoint.ends_with("/api/v2/submit") {
endpoint
} else {
format!("{}/api/v2/submit", endpoint.trim_end_matches('/'))
};
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(
&self,
trade_type: TradeType,
transaction: &VersionedTransaction,
wait_confirmation: bool,
) -> Result<()> {
2025-07-06 22:06:44 +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-06 22:06:44 +08:00
2025-07-07 01:01:38 +08:00
let request_body = serde_json::to_string(&json!({
"transaction": {
"content": content
},
"frontRunningProtection": false
}))?;
2026-03-14 18:16:55 +02:00
let response_text = self
.http_client
.post(&self.endpoint)
2025-07-07 01:01:38 +08:00
.body(request_body)
.header("Authorization", &self.auth_token)
.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!(" [nextblock] {} submitted: {:?}", trade_type, start_time.elapsed());
2025-07-07 01:01:38 +08:00
} else if let Some(_error) = response_json.get("error") {
eprintln!(" [nextblock] {} submission failed: {:?}", trade_type, _error);
2025-07-07 01:01:38 +08:00
}
2025-08-27 14:27:26 +08:00
} else {
eprintln!(" [nextblock] {} submission failed: {:?}", trade_type, response_text);
2025-07-07 01:01:38 +08:00
}
2025-07-06 22:06:44 +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);
2026-03-14 18:16:55 +02:00
println!(
" [nextblock] {} confirmation failed: {:?}",
trade_type,
start_time.elapsed()
);
return Err(e);
2026-03-14 18:16:55 +02:00
}
2025-07-06 22:06:44 +08:00
}
if wait_confirmation {
println!(" signature: {:?}", signature);
println!(" [nextblock] {} confirmed: {:?}", trade_type, start_time.elapsed());
}
2025-07-06 22:06:44 +08:00
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
pub async fn send_transactions(
&self,
trade_type: TradeType,
transactions: &Vec<VersionedTransaction>,
wait_confirmation: bool,
) -> Result<()> {
2025-07-06 22:06:44 +08:00
for transaction in transactions {
self.send_transaction(trade_type, transaction, wait_confirmation).await?;
2025-07-06 22:06:44 +08:00
}
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
}