Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracing #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde = "1.0.209"
bincode = "1.3.3"
bs58 = "0.4.0"
rand = "0.8"
tracing = "0.1.40"

# Remove the [[example]] section if it exists

Expand Down
72 changes: 50 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::{anyhow, Result};
use rand::seq::SliceRandom;
use reqwest::Client;
use serde_json::{json, Value};
use std::fmt;
use anyhow::{anyhow, Result};
use rand::seq::SliceRandom;
use tracing::{debug, trace};

pub struct JitoJsonRpcSDK {
base_url: String,
Expand Down Expand Up @@ -34,31 +35,43 @@ impl JitoJsonRpcSDK {
}
}

async fn send_request(&self, endpoint: &str, method: &str, params: Option<Value>) -> Result<Value, reqwest::Error> {
async fn send_request(
&self,
endpoint: &str,
method: &str,
params: Option<Value>,
) -> Result<Value, reqwest::Error> {
let url = format!("{}{}", self.base_url, endpoint);

let data = json!({
"jsonrpc": "2.0",
"id": 1,
"method": method,
"params": params.unwrap_or(json!([]))
});

println!("Sending request to: {}", url);
println!("Request body: {}", serde_json::to_string_pretty(&data).unwrap());
trace!("Sending request to: {}", url);
trace!(
"Request body: {}",
serde_json::to_string_pretty(&data).unwrap()
);

let response = self.client
let response = self
.client
.post(&url)
.header("Content-Type", "application/json")
.json(&data)
.send()
.await?;

let status = response.status();
println!("Response status: {}", status);
debug!("Response status: {}", status);

let body = response.json::<Value>().await?;
println!("Response body: {}", serde_json::to_string_pretty(&body).unwrap());
trace!(
"Response body: {}",
serde_json::to_string_pretty(&body).unwrap()
);

Ok(body)
}
Expand All @@ -76,7 +89,7 @@ impl JitoJsonRpcSDK {
// Get a random tip account
pub async fn get_random_tip_account(&self) -> Result<String> {
let tip_accounts_response = self.get_tip_accounts().await?;

let tip_accounts = tip_accounts_response["result"]
.as_array()
.ok_or_else(|| anyhow!("Failed to parse tip accounts as array"))?;
Expand Down Expand Up @@ -110,13 +123,17 @@ impl JitoJsonRpcSDK {
.map_err(|e| anyhow!("Request error: {}", e))
}

pub async fn send_bundle(&self, params: Option<Value>, uuid: Option<&str>) -> Result<Value, anyhow::Error> {
pub async fn send_bundle(
&self,
params: Option<Value>,
uuid: Option<&str>,
) -> Result<Value, anyhow::Error> {
let mut endpoint = "/bundles".to_string();

if let Some(uuid) = uuid {
endpoint = format!("{}?uuid={}", endpoint, uuid);
}

// Ensure params is an array of transactions
let transactions = match params {
Some(Value::Array(transactions)) => {
Expand All @@ -127,20 +144,28 @@ impl JitoJsonRpcSDK {
return Err(anyhow!("Bundle can contain at most 5 transactions"));
}
transactions
},
_ => return Err(anyhow!("Invalid bundle format: expected an array of transactions")),
}
_ => {
return Err(anyhow!(
"Invalid bundle format: expected an array of transactions"
))
}
};

// Wrap the transactions array in another array
let params = json!([transactions]);

// Send the wrapped transactions array
self.send_request(&endpoint, "sendBundle", Some(params))
.await
.map_err(|e| anyhow!("Request error: {}", e))
}

pub async fn send_txn(&self, params: Option<Value>, bundle_only: bool) -> Result<Value, reqwest::Error> {
pub async fn send_txn(
&self,
params: Option<Value>,
bundle_only: bool,
) -> Result<Value, reqwest::Error> {
let mut query_params = Vec::new();

if bundle_only {
Expand All @@ -157,19 +182,23 @@ impl JitoJsonRpcSDK {
let params = match params {
Some(Value::Object(map)) => {
let tx = map.get("tx").and_then(Value::as_str).unwrap_or_default();
let skip_preflight = map.get("skipPreflight").and_then(Value::as_bool).unwrap_or(false);
let skip_preflight = map
.get("skipPreflight")
.and_then(Value::as_bool)
.unwrap_or(false);
json!([
tx,
{
"encoding": "base64",
"skipPreflight": skip_preflight
}
])
},
}
_ => json!([]),
};

self.send_request(&endpoint, "sendTransaction", Some(params)).await
self.send_request(&endpoint, "sendTransaction", Some(params))
.await
}

pub async fn get_in_flight_bundle_statuses(&self, bundle_uuids: Vec<String>) -> Result<Value> {
Expand All @@ -191,5 +220,4 @@ impl JitoJsonRpcSDK {
pub fn prettify(value: Value) -> PrettyJsonValue {
PrettyJsonValue(value)
}

}