Skip to content

Commit 87812a4

Browse files
committed
optimize: get tip amounts via https for cli
1 parent e539340 commit 87812a4

5 files changed

Lines changed: 61 additions & 34 deletions

File tree

src/jito/api.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
44
use reqwest::Proxy;
55
use serde::{Deserialize, Serialize};
66

7-
use super::BLOCK_ENGINE_URL;
7+
use super::{TipPercentileData, BLOCK_ENGINE_URL};
88

99
#[derive(Serialize)]
1010
struct RpcRequest {
@@ -62,3 +62,20 @@ impl TryFrom<RpcResponse> for TipAccountResult {
6262
Ok(TipAccountResult { accounts })
6363
}
6464
}
65+
66+
pub async fn get_tip_amounts() -> Result<Vec<TipPercentileData>> {
67+
let mut client_builder = reqwest::Client::builder();
68+
if let Ok(http_proxy) = env::var("HTTP_PROXY") {
69+
let proxy = Proxy::all(http_proxy)?;
70+
client_builder = client_builder.proxy(proxy);
71+
}
72+
let client = client_builder.build()?;
73+
74+
let result = client
75+
.get("https://bundles.jito.wtf/api/v1/bundles/tip_floor")
76+
.send()
77+
.await?
78+
.json::<Vec<TipPercentileData>>()
79+
.await?;
80+
Ok(result)
81+
}

src/jito/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@ use tokio::{
1212
time::{sleep, Instant},
1313
};
1414
use tracing::{debug, error, info, warn};
15-
use ws::TIPS_PERCENTILE;
1615

1716
use crate::get_env_var;
1817

1918
pub mod api;
2019
pub mod ws;
2120

21+
pub static TIPS_PERCENTILE: LazyLock<RwLock<Option<TipPercentileData>>> =
22+
LazyLock::new(|| RwLock::new(None));
23+
24+
#[derive(Debug, Deserialize, Clone)]
25+
pub struct TipPercentileData {
26+
pub time: String,
27+
pub landed_tips_25th_percentile: f64,
28+
pub landed_tips_50th_percentile: f64,
29+
pub landed_tips_75th_percentile: f64,
30+
pub landed_tips_95th_percentile: f64,
31+
pub landed_tips_99th_percentile: f64,
32+
pub ema_landed_tips_50th_percentile: f64,
33+
}
34+
2235
pub static BLOCK_ENGINE_URL: LazyLock<String> =
2336
LazyLock::new(|| get_env_var("JITO_BLOCK_ENGINE_URL"));
2437
pub static TIP_STREAM_URL: LazyLock<String> = LazyLock::new(|| get_env_var("JITO_TIP_STREAM_URL"));
@@ -47,6 +60,14 @@ pub async fn get_tip_account() -> Result<Pubkey> {
4760
None => Err(anyhow!("jito: no tip accounts available")),
4861
}
4962
}
63+
64+
pub async fn init_tip_amounts() -> Result<()> {
65+
let tip_percentiles = api::get_tip_amounts().await?;
66+
*TIPS_PERCENTILE.write().await = tip_percentiles.first().cloned();
67+
68+
Ok(())
69+
}
70+
5071
// unit sol
5172
pub async fn get_tip_value() -> Result<f64> {
5273
// If TIP_VALUE is set, use it

src/jito/ws.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
use std::sync::LazyLock;
2-
3-
use crate::jito::TIP_STREAM_URL;
1+
use crate::jito::{TipPercentileData, TIPS_PERCENTILE, TIP_STREAM_URL};
42
use anyhow::{Context, Result};
53
use futures_util::StreamExt;
6-
use serde::Deserialize;
7-
use tokio::sync::RwLock;
84
use tokio_tungstenite::{connect_async, tungstenite::Message};
95
use tracing::{debug, error, info, warn};
106

11-
pub static TIPS_PERCENTILE: LazyLock<RwLock<Option<TipPercentileData>>> =
12-
LazyLock::new(|| RwLock::new(None));
13-
14-
#[derive(Debug, Deserialize, Clone)]
15-
pub struct TipPercentileData {
16-
pub time: String,
17-
pub landed_tips_25th_percentile: f64,
18-
pub landed_tips_50th_percentile: f64,
19-
pub landed_tips_75th_percentile: f64,
20-
pub landed_tips_95th_percentile: f64,
21-
pub landed_tips_99th_percentile: f64,
22-
pub ema_landed_tips_50th_percentile: f64,
23-
}
24-
257
pub async fn tip_stream() -> Result<()> {
268
let (ws_stream, _) = connect_async(TIP_STREAM_URL.to_string())
279
.await
@@ -39,7 +21,7 @@ pub async fn tip_stream() -> Result<()> {
3921
match serde_json::from_str::<Vec<TipPercentileData>>(&text) {
4022
Ok(data) => {
4123
if !data.is_empty() {
42-
*TIPS_PERCENTILE.write().await = Some(data[0].clone());
24+
*TIPS_PERCENTILE.write().await = data.first().cloned();
4325
} else {
4426
warn!("Received an empty data.")
4527
}

src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,20 @@ async fn main() -> Result<()> {
107107
);
108108
// jito
109109
if *jito {
110-
jito::init_tip_accounts().await.unwrap();
111-
tokio::spawn(async {
112-
if let Err(e) = jito::ws::tip_stream().await {
113-
println!("Error: {:?}", e);
114-
}
115-
});
116-
info!("waiting 5s for get tip percentiles data");
117-
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
110+
jito::init_tip_accounts()
111+
.await
112+
.map_err(|err| {
113+
info!("failed to get tip accounts: {:?}", err);
114+
err
115+
})
116+
.unwrap();
117+
jito::init_tip_amounts()
118+
.await
119+
.map_err(|err| {
120+
info!("failed to init tip amounts: {:?}", err);
121+
err
122+
})
123+
.unwrap();
118124
}
119125

120126
swap::swap(

src/tx.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ pub async fn new_signed_and_send(
8080
if use_jito {
8181
// jito
8282
let tip_account = get_tip_account().await?;
83-
let jito_client = Arc::new(JitoRpcClient::new(format!(
84-
"{}/api/v1/bundles",
85-
jito::BLOCK_ENGINE_URL.to_string()
86-
)));
8783
// jito tip, the upper limit is 0.1
8884
let mut tip = get_tip_value().await?;
8985
tip = tip.min(0.1);
@@ -92,6 +88,11 @@ pub async fn new_signed_and_send(
9288
"tip account: {}, tip(sol): {}, lamports: {}",
9389
tip_account, tip, tip_lamports
9490
);
91+
92+
let jito_client = Arc::new(JitoRpcClient::new(format!(
93+
"{}/api/v1/bundles",
94+
jito::BLOCK_ENGINE_URL.to_string()
95+
)));
9596
// tip tx
9697
let mut bundle: Vec<VersionedTransaction> = vec![];
9798
bundle.push(VersionedTransaction::from(txn));

0 commit comments

Comments
 (0)