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

Ws max reconnects #25

Merged
merged 5 commits into from
Nov 15, 2023
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ If that doesn't work, try double-checking your URLs. Refer back to the [environm
- [rusty-sando](https://github.com/mouseless-eth/rusty-sando)
- [mev-inspect-rs](https://github.com/flashbots/mev-inspect-rs)
- [mev-inspect-py](https://github.com/flashbots/mev-inspect-py)
- [nitepunk](https://soundcloud.com/nitepunk/sets/slices)

## future improvements

Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use hindsight::data::db::DbEngine;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[arg(short, long, default_value = "20")]
pub ws_max_reconnects: Option<usize>,

#[command(subcommand)]
pub command: Option<Commands>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DbEngine {
.map(|engine| engine.to_string())
.reduce(|a, b| format!("{} | {}", a, b))
.expect("failed to reduce db engines to string")
.to_string()

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/data/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn parse_filename(filename: Option<String>) -> Result<String> {
.as_secs()
));
Ok(if filename.ends_with(".json") {
filename.to_owned()
filename
} else {
format!("{}.json", filename)
})
Expand Down
1 change: 0 additions & 1 deletion src/data/mongo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ impl ArbDb for MongoConnect {
}
}

// TODO: move these, generalize connect to test both dbs
#[cfg(test)]
mod test {
use super::*;
Expand Down
17 changes: 7 additions & 10 deletions src/hindsight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
data::arbs::ArbDatabase,
info,
sim::processor::{simulate_backrun_arbs, H256Map},
util::{get_ws_client, WsClient},
util::WsClient,
Result,
};
use ethers::types::Transaction;
Expand All @@ -16,9 +16,8 @@ pub struct Hindsight {
}

impl Hindsight {
pub async fn new(rpc_url_ws: String) -> Result<Self> {
let client = get_ws_client(Some(rpc_url_ws)).await?;
Ok(Self { client })
pub async fn new(ws_client: WsClient) -> Result<Self> {
Ok(Self { client: ws_client })
}

/// For each tx in `txs`, simulates an optimal backrun-arbitrage in a parallel thread,
Expand Down Expand Up @@ -53,14 +52,12 @@ impl Hindsight {
}
let results = future::join_all(handlers).await;
let results = results
// TODO: can this be cleaned up? so ugly
.into_iter()
.filter_map(|res| res.ok())
.flatten()
.collect::<Vec<_>>();
info!("batch results: {:#?}", results);
if let Some(db) = db.to_owned() {
// can't do && with a `let` in the conditional
if !results.is_empty() {
db.to_owned().write_arbs(&results).await?;
}
Expand All @@ -76,20 +73,20 @@ mod tests {
use serde_json::json;

use crate::{
config::Config,
data::{
arbs::ArbFilterParams,
db::{Db, DbEngine},
MongoConfig,
},
util::get_ws_client,
};

use super::*;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn it_processes_orderflow() -> Result<()> {
let config = Config::default();
let hindsight = Hindsight::new(config.rpc_url_ws).await?;
let client = get_ws_client(None, 1).await?;
let hindsight = Hindsight::new(client).await?;

// data from an actual juicy event
let juicy_event: EventHistory = serde_json::from_value(json!({
Expand Down Expand Up @@ -128,7 +125,7 @@ mod tests {
}))?;
let juicy_tx_hash: H256 =
"0xf00df02ad86f04a8b32d9f738394ee1b7ff791647f753923c60522363132f84a".parse::<H256>()?;
let juicy_tx = get_ws_client(None)
let juicy_tx = get_ws_client(None, 1)
.await?
.get_transaction(juicy_tx_hash)
.await?
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ethers::types::U256;
use hindsight::{
commands::{self},
config::Config,
data::{
arbs::{ArbFilterParams, WriteEngine},
db::Db,
Expand All @@ -20,7 +19,6 @@ use cli::{Cli, Commands};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let config = Config::default();
let cli = Cli::parse_args();

ctrlc::set_handler(move || {
Expand All @@ -29,9 +27,10 @@ async fn main() -> anyhow::Result<()> {
})
.expect("Error setting Ctrl-C handler");

let ws_client = get_ws_client(None).await?;
let max_reconnects = cli.ws_max_reconnects.unwrap_or_default();
let ws_client = get_ws_client(None, max_reconnects).await?;
let mevshare = EventClient::default();
let hindsight = Hindsight::new(config.rpc_url_ws).await?;
let hindsight = Hindsight::new(ws_client.clone()).await?;

match cli.command {
Some(Commands::Scan {
Expand Down
6 changes: 3 additions & 3 deletions src/sim/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async fn step_arb(
best_amount_in + band_width
},
];
return step_arb(
step_arb(
client,
user_tx,
block_info,
Expand All @@ -376,7 +376,7 @@ async fn step_arb(
start_pair_variant,
end_pair_variant,
)
.await;
.await
}

/// Find the optimal backrun for a given tx.
Expand Down Expand Up @@ -554,7 +554,7 @@ pub async fn find_optimal_backrun_amount_in_out(
Ok(results
.into_iter()
.filter_map(|res| res.ok())
.filter_map(|res| res.to_owned())
.flatten()
.collect::<Vec<_>>())
}

Expand Down
2 changes: 1 addition & 1 deletion src/sim/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn commit_braindance_swap(
Ok(res) => res,
Err(e) => return Err(anyhow::anyhow!("failed to commit swap: {:?}", e)),
};
let output = match res.to_owned() {
let output = match res {
ExecutionResult::Success { output, .. } => match output {
Output::Call(o) => o,
Output::Create(o, _) => o,
Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ use uniswap_v3_math::{full_math::mul_div, sqrt_price_math::Q96};
pub use ethers::utils::WEI_IN_ETHER as ETH;
pub type WsClient = Arc<Provider<Ws>>;

pub async fn get_ws_client(rpc_url: Option<String>) -> Result<WsClient> {
pub async fn get_ws_client(rpc_url: Option<String>, max_reconnects: usize) -> Result<WsClient> {
let rpc_url = if let Some(rpc_url) = rpc_url {
rpc_url
} else {
Config::default().rpc_url_ws
};
let provider = Provider::<Ws>::connect(rpc_url).await?;
let provider = Provider::<Ws>::connect_with_reconnects(rpc_url, max_reconnects).await?;
Ok(Arc::new(provider))
}

Expand Down Expand Up @@ -233,7 +233,7 @@ pub mod test {
use crate::Result;

pub async fn get_test_ws_client() -> Result<WsClient> {
let ws_client = get_ws_client(None).await?;
let ws_client = get_ws_client(None, 1).await?;
Ok(ws_client)
}
}