From b414facadc297cdf81e98031814d44bb2bf67471 Mon Sep 17 00:00:00 2001 From: zizou <111426680+flopell@users.noreply.github.com> Date: Tue, 18 Feb 2025 14:12:35 +0100 Subject: [PATCH] feat(example): add base into the `price-printer` example --- examples/price_printer/Readme.md | 6 +-- examples/price_printer/main.rs | 92 +++++++++++++++++++++----------- examples/price_printer/utils.rs | 11 ++++ 3 files changed, 76 insertions(+), 33 deletions(-) diff --git a/examples/price_printer/Readme.md b/examples/price_printer/Readme.md index e72fc865..e607d621 100644 --- a/examples/price_printer/Readme.md +++ b/examples/price_printer/Readme.md @@ -1,11 +1,11 @@ # Price Printer This example allows you to list all pools over a certain tvl threshold and explore -quotes from each pool in Ethereum. +quotes from each pool. ## How to run ```bash -export RPC_URL= -cargo run --release --example price_printer -- --tvl-threshold 1000 +export RPC_URL= +cargo run --release --example price_printer -- --tvl-threshold 1000 --chain ``` diff --git a/examples/price_printer/main.rs b/examples/price_printer/main.rs index eff2ba02..05617c51 100644 --- a/examples/price_printer/main.rs +++ b/examples/price_printer/main.rs @@ -2,8 +2,7 @@ mod ui; pub mod utils; extern crate tycho_simulation; - -use std::env; +use std::{env, str::FromStr}; use clap::Parser; use futures::{future::select_all, StreamExt}; @@ -25,12 +24,57 @@ use tycho_simulation::{ protocol::models::BlockUpdate, utils::load_all_tokens, }; +use utils::get_default_url; #[derive(Parser)] struct Cli { /// The tvl threshold to filter the graph by #[arg(short, long, default_value_t = 1000.0)] tvl_threshold: f64, + /// The target blockchain + #[clap(long, default_value = "ethereum")] + pub chain: String, +} + +fn register_exchanges( + mut builder: ProtocolStreamBuilder, + chain: &Chain, + tvl_filter: ComponentFilter, +) -> ProtocolStreamBuilder { + match chain { + Chain::Ethereum => { + builder = builder + .exchange::("uniswap_v2", tvl_filter.clone(), None) + .exchange::("uniswap_v3", tvl_filter.clone(), None) + .exchange::>( + "vm:balancer_v2", + tvl_filter.clone(), + Some(balancer_pool_filter), + ) + .exchange::>( + "vm:curve", + tvl_filter.clone(), + Some(curve_pool_filter), + ) + .exchange::( + "uniswap_v4", + tvl_filter.clone(), + Some(uniswap_v4_pool_with_hook_filter), + ); + } + Chain::Base => { + builder = builder + .exchange::("uniswap_v2", tvl_filter.clone(), None) + .exchange::("uniswap_v3", tvl_filter.clone(), None) + .exchange::( + "uniswap_v4", + tvl_filter.clone(), + Some(uniswap_v4_pool_with_hook_filter), + ) + } + Chain::ZkSync | Chain::Starknet | Chain::Arbitrum => {} + } + builder } #[tokio::main] @@ -38,9 +82,13 @@ async fn main() { utils::setup_tracing(); // Parse command-line arguments into a Cli struct let cli = Cli::parse(); + let chain = + Chain::from_str(&cli.chain).unwrap_or_else(|_| panic!("Unknown chain {}", cli.chain)); + + let tycho_url = env::var("TYCHO_URL").unwrap_or_else(|_| { + get_default_url(&chain).unwrap_or_else(|| panic!("Unknown URL for chain {}", cli.chain)) + }); - let tycho_url = - env::var("TYCHO_URL").unwrap_or_else(|_| "tycho-beta.propellerheads.xyz".to_string()); let tycho_api_key: String = env::var("TYCHO_API_KEY").unwrap_or_else(|_| "sampletoken".to_string()); @@ -57,37 +105,21 @@ async fn main() { tycho_url.as_str(), false, Some(tycho_api_key.as_str()), - Chain::Ethereum, + chain, None, None, ) .await; let tvl_filter = ComponentFilter::with_tvl_range(cli.tvl_threshold, cli.tvl_threshold); - let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum) - .exchange::("uniswap_v2", tvl_filter.clone(), None) - .exchange::("uniswap_v3", tvl_filter.clone(), None) - .exchange::>( - "vm:balancer_v2", - tvl_filter.clone(), - Some(balancer_pool_filter), - ) - .exchange::>( - "vm:curve", - tvl_filter.clone(), - Some(curve_pool_filter), - ) - .exchange::( - "uniswap_v4", - tvl_filter.clone(), - Some(uniswap_v4_pool_with_hook_filter), - ) - .auth_key(Some(tycho_api_key.clone())) - .skip_state_decode_failures(true) - .set_tokens(all_tokens) - .await - .build() - .await - .expect("Failed building protocol stream"); + let mut protocol_stream = + register_exchanges(ProtocolStreamBuilder::new(&tycho_url, chain), &chain, tvl_filter) + .auth_key(Some(tycho_api_key.clone())) + .skip_state_decode_failures(true) + .set_tokens(all_tokens) + .await + .build() + .await + .expect("Failed building protocol stream"); // Loop through block updates while let Some(msg) = protocol_stream.next().await { diff --git a/examples/price_printer/utils.rs b/examples/price_printer/utils.rs index 4b498a3a..371f6121 100644 --- a/examples/price_printer/utils.rs +++ b/examples/price_printer/utils.rs @@ -1,4 +1,5 @@ use tracing_subscriber::{fmt, EnvFilter}; +use tycho_core::dto::Chain; pub fn setup_tracing() { let writer = tracing_appender::rolling::daily("logs", "price_printer.log"); @@ -10,3 +11,13 @@ pub fn setup_tracing() { // Set the subscriber as the global default tracing::subscriber::set_global_default(subscriber).unwrap(); } + +pub(super) fn get_default_url(chain: &Chain) -> Option { + match chain { + Chain::Ethereum => Some("tycho-beta.propellerheads.xyz".to_string()), + Chain::Starknet => None, + Chain::ZkSync => None, + Chain::Arbitrum => None, + Chain::Base => Some("tycho-base-beta.propellerheads.xyz".to_string()), + } +}