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

feat(progress-bar): Add progress bar to know sync progress #188

Open
wants to merge 2 commits into
base: main
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 @@ -37,6 +37,7 @@ alloy = { version = "0.2", features = [
"signer-local",
] }
reqwest = "0.12"
indicatif = "0.17.8"

[features]
default = ["filters", "state-space"]
Expand Down
6 changes: 3 additions & 3 deletions benches/uniswapv2_simulate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use alloy::primitives::{address, U256};
use criterion::{Criterion, criterion_group, criterion_main};
use criterion::{criterion_group, criterion_main, Criterion};

use amms::amm::AutomatedMarketMaker;
use amms::amm::uniswap_v2::UniswapV2Pool;
use amms::amm::AutomatedMarketMaker;

/// Generate a random ether amount between `from` and `to`
fn random_ether(from: f32, to: f32) -> u128 {
Expand All @@ -27,7 +27,7 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
pool.reserve_0 = random_ether(1.0, 1000.0);
pool.reserve_1 = random_ether(1.0, 1000.0);
let swap_amount = U256::from(random_ether(1.0, 10.0));
let swap_amount = U256::from(random_ether(1.0, 10.0));
let _ = pool.simulate_swap(token_a, swap_amount).unwrap();
})
});
Expand Down
22 changes: 20 additions & 2 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};

use alloy::{network::Network, providers::Provider, transports::Transport};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

use std::{panic::resume_unwind, sync::Arc};

Expand All @@ -35,24 +36,37 @@ where

let current_block = provider.get_block_number().await?;

let multi_progress = MultiProgress::new();
let style = ProgressStyle::default_bar()
.template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
.unwrap()
.progress_chars("##-");

// Aggregate the populated pools from each thread
let mut aggregated_amms: Vec<AMM> = vec![];
let mut handles = vec![];

// For each dex supplied, get all pair created events and get reserve values
for factory in factories.clone() {
let provider = provider.clone();

let pb = multi_progress.add(ProgressBar::new(0));
pb.set_style(style.clone());
// Spawn a new thread to get all pools and sync data for each dex
handles.push(tokio::spawn(async move {
tracing::info!(?factory, "Getting all AMMs from factory");

// Get all of the amms from the factory
let mut amms = factory
.get_all_amms(Some(current_block), provider.clone(), step)
.await?;

pb.set_length(amms.len() as u64);

tracing::info!(?factory, "Populating AMMs from factory");
populate_amms(&mut amms, current_block, provider.clone()).await?;

pb.set_message(format!("Syncing {}", factory.address()));

populate_amms(&mut amms, current_block, provider.clone(), &pb).await?;

// Clean empty pools
amms = filters::filter_empty_amms(amms);
Expand Down Expand Up @@ -115,6 +129,7 @@ pub async fn populate_amms<T, N, P>(
amms: &mut [AMM],
block_number: u64,
provider: Arc<P>,
pb: &ProgressBar,
) -> Result<(), AMMError>
where
T: Transport + Clone,
Expand All @@ -132,6 +147,7 @@ where
provider.clone(),
)
.await?;
pb.inc(amm_chunk.len() as u64);
}
}

Expand All @@ -145,13 +161,15 @@ where
provider.clone(),
)
.await?;
pb.inc(amm_chunk.len() as u64);
}
}

// TODO: Implement batch request
AMM::ERC4626Vault(_) => {
for amm in amms {
amm.populate_data(None, provider.clone()).await?;
pb.inc(1);
}
}
}
Expand Down
Loading