Skip to content

Commit

Permalink
optimize: get tip amounts via https for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
wisarmy committed Jan 9, 2025
1 parent e539340 commit 87812a4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
19 changes: 18 additions & 1 deletion src/jito/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use reqwest::Proxy;
use serde::{Deserialize, Serialize};

use super::BLOCK_ENGINE_URL;
use super::{TipPercentileData, BLOCK_ENGINE_URL};

#[derive(Serialize)]
struct RpcRequest {
Expand Down Expand Up @@ -62,3 +62,20 @@ impl TryFrom<RpcResponse> for TipAccountResult {
Ok(TipAccountResult { accounts })
}
}

pub async fn get_tip_amounts() -> Result<Vec<TipPercentileData>> {
let mut client_builder = reqwest::Client::builder();
if let Ok(http_proxy) = env::var("HTTP_PROXY") {
let proxy = Proxy::all(http_proxy)?;
client_builder = client_builder.proxy(proxy);
}
let client = client_builder.build()?;

let result = client
.get("https://bundles.jito.wtf/api/v1/bundles/tip_floor")
.send()
.await?
.json::<Vec<TipPercentileData>>()
.await?;
Ok(result)
}
23 changes: 22 additions & 1 deletion src/jito/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ use tokio::{
time::{sleep, Instant},
};
use tracing::{debug, error, info, warn};
use ws::TIPS_PERCENTILE;

use crate::get_env_var;

pub mod api;
pub mod ws;

pub static TIPS_PERCENTILE: LazyLock<RwLock<Option<TipPercentileData>>> =
LazyLock::new(|| RwLock::new(None));

#[derive(Debug, Deserialize, Clone)]
pub struct TipPercentileData {
pub time: String,
pub landed_tips_25th_percentile: f64,
pub landed_tips_50th_percentile: f64,
pub landed_tips_75th_percentile: f64,
pub landed_tips_95th_percentile: f64,
pub landed_tips_99th_percentile: f64,
pub ema_landed_tips_50th_percentile: f64,
}

pub static BLOCK_ENGINE_URL: LazyLock<String> =
LazyLock::new(|| get_env_var("JITO_BLOCK_ENGINE_URL"));
pub static TIP_STREAM_URL: LazyLock<String> = LazyLock::new(|| get_env_var("JITO_TIP_STREAM_URL"));
Expand Down Expand Up @@ -47,6 +60,14 @@ pub async fn get_tip_account() -> Result<Pubkey> {
None => Err(anyhow!("jito: no tip accounts available")),
}
}

pub async fn init_tip_amounts() -> Result<()> {
let tip_percentiles = api::get_tip_amounts().await?;
*TIPS_PERCENTILE.write().await = tip_percentiles.first().cloned();

Ok(())
}

// unit sol
pub async fn get_tip_value() -> Result<f64> {
// If TIP_VALUE is set, use it
Expand Down
22 changes: 2 additions & 20 deletions src/jito/ws.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
use std::sync::LazyLock;

use crate::jito::TIP_STREAM_URL;
use crate::jito::{TipPercentileData, TIPS_PERCENTILE, TIP_STREAM_URL};
use anyhow::{Context, Result};
use futures_util::StreamExt;
use serde::Deserialize;
use tokio::sync::RwLock;
use tokio_tungstenite::{connect_async, tungstenite::Message};
use tracing::{debug, error, info, warn};

pub static TIPS_PERCENTILE: LazyLock<RwLock<Option<TipPercentileData>>> =
LazyLock::new(|| RwLock::new(None));

#[derive(Debug, Deserialize, Clone)]
pub struct TipPercentileData {
pub time: String,
pub landed_tips_25th_percentile: f64,
pub landed_tips_50th_percentile: f64,
pub landed_tips_75th_percentile: f64,
pub landed_tips_95th_percentile: f64,
pub landed_tips_99th_percentile: f64,
pub ema_landed_tips_50th_percentile: f64,
}

pub async fn tip_stream() -> Result<()> {
let (ws_stream, _) = connect_async(TIP_STREAM_URL.to_string())
.await
Expand All @@ -39,7 +21,7 @@ pub async fn tip_stream() -> Result<()> {
match serde_json::from_str::<Vec<TipPercentileData>>(&text) {
Ok(data) => {
if !data.is_empty() {
*TIPS_PERCENTILE.write().await = Some(data[0].clone());
*TIPS_PERCENTILE.write().await = data.first().cloned();
} else {
warn!("Received an empty data.")
}
Expand Down
22 changes: 14 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ async fn main() -> Result<()> {
);
// jito
if *jito {
jito::init_tip_accounts().await.unwrap();
tokio::spawn(async {
if let Err(e) = jito::ws::tip_stream().await {
println!("Error: {:?}", e);
}
});
info!("waiting 5s for get tip percentiles data");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
jito::init_tip_accounts()
.await
.map_err(|err| {
info!("failed to get tip accounts: {:?}", err);
err
})
.unwrap();
jito::init_tip_amounts()
.await
.map_err(|err| {
info!("failed to init tip amounts: {:?}", err);
err
})
.unwrap();
}

swap::swap(
Expand Down
9 changes: 5 additions & 4 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ pub async fn new_signed_and_send(
if use_jito {
// jito
let tip_account = get_tip_account().await?;
let jito_client = Arc::new(JitoRpcClient::new(format!(
"{}/api/v1/bundles",
jito::BLOCK_ENGINE_URL.to_string()
)));
// jito tip, the upper limit is 0.1
let mut tip = get_tip_value().await?;
tip = tip.min(0.1);
Expand All @@ -92,6 +88,11 @@ pub async fn new_signed_and_send(
"tip account: {}, tip(sol): {}, lamports: {}",
tip_account, tip, tip_lamports
);

let jito_client = Arc::new(JitoRpcClient::new(format!(
"{}/api/v1/bundles",
jito::BLOCK_ENGINE_URL.to_string()
)));
// tip tx
let mut bundle: Vec<VersionedTransaction> = vec![];
bundle.push(VersionedTransaction::from(txn));
Expand Down

0 comments on commit 87812a4

Please sign in to comment.