From b1a76a862714a6e33c1cb0a51b38796cab9c150c Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Fri, 7 Feb 2025 13:44:07 +0200 Subject: [PATCH 1/2] feat: add filter args to load_all_tokens And chain specific defaults to the activity filter --- examples/price_printer/main.rs | 2 ++ examples/quickstart/main.rs | 12 +++++++++--- src/utils.rs | 27 ++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/examples/price_printer/main.rs b/examples/price_printer/main.rs index 6d75f649..eff2ba02 100644 --- a/examples/price_printer/main.rs +++ b/examples/price_printer/main.rs @@ -58,6 +58,8 @@ async fn main() { false, Some(tycho_api_key.as_str()), Chain::Ethereum, + None, + None, ) .await; let tvl_filter = ComponentFilter::with_tvl_range(cli.tvl_threshold, cli.tvl_threshold); diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index d63bb598..e0e40a9f 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -36,9 +36,15 @@ async fn main() { let tvl_threshold = 10_000.0; let tvl_filter = ComponentFilter::with_tvl_range(tvl_threshold, tvl_threshold); - let all_tokens = - load_all_tokens(tycho_url.as_str(), false, Some(tycho_api_key.as_str()), Chain::Ethereum) - .await; + let all_tokens = load_all_tokens( + tycho_url.as_str(), + false, + Some(tycho_api_key.as_str()), + Chain::Ethereum, + None, + None, + ) + .await; let mut pairs: HashMap> = HashMap::new(); let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum) diff --git a/src/utils.rs b/src/utils.rs index 1e8782fb..7d5f506b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use tracing::info; use tycho_client::{rpc::RPCClient, HttpRPCClient}; use tycho_core::{dto::Chain, Bytes}; @@ -36,19 +37,43 @@ pub fn hexstring_to_vec(hexstring: &str) -> Result, SimulationError> { } /// Loads all tokens from Tycho and returns them as a Hashmap of address->Token. +/// +/// # Arguments +/// +/// * `tycho_url` - The URL of the Tycho RPC (do not include the url prefix e.g. 'https://'). +/// * `no_tls` - Whether to use HTTP instead of HTTPS. +/// * `auth_key` - The API key to use for authentication. +/// * `chain` - The chain to load tokens from. +/// * `quality_filter` - The minimum quality of tokens to load. Defaults to 100 if not provided. +/// * `activity_filter` - The max number of days since the token was last traded. Defaults are chain +/// specific and applied if not provided. pub async fn load_all_tokens( tycho_url: &str, no_tls: bool, auth_key: Option<&str>, chain: Chain, + quality_filter: Option, + activity_filter: Option, ) -> HashMap { + info!("Loading tokens from Tycho..."); let rpc_url = if no_tls { format!("http://{tycho_url}") } else { format!("https://{tycho_url}") }; let rpc_client = HttpRPCClient::new(rpc_url.as_str(), auth_key).unwrap(); + // Chain specific defaults for special case chains. Otherwise defaults to 42 days. + let default_activity_filter = HashMap::from([(Chain::Base, 10_u64)]); + #[allow(clippy::mutable_key_type)] rpc_client - .get_all_tokens(chain, Some(100), Some(42), 3_000) + .get_all_tokens( + chain, + quality_filter.or(Some(100)), + activity_filter.or(default_activity_filter + .get(&chain) + .or(Some(&42)) + .copied()), + 3_000, + ) .await .expect("Unable to load tokens") .into_iter() From cfc4f5193d595b6ec079886f949d8d1ef8a14df6 Mon Sep 17 00:00:00 2001 From: Louise Poole Date: Fri, 7 Feb 2025 16:10:08 +0200 Subject: [PATCH 2/2] fix: improve load_all_tokens arg names --- src/utils.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 7d5f506b..9786dab7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -44,16 +44,16 @@ pub fn hexstring_to_vec(hexstring: &str) -> Result, SimulationError> { /// * `no_tls` - Whether to use HTTP instead of HTTPS. /// * `auth_key` - The API key to use for authentication. /// * `chain` - The chain to load tokens from. -/// * `quality_filter` - The minimum quality of tokens to load. Defaults to 100 if not provided. -/// * `activity_filter` - The max number of days since the token was last traded. Defaults are chain -/// specific and applied if not provided. +/// * `min_quality` - The minimum quality of tokens to load. Defaults to 100 if not provided. +/// * `max_days_since_last_trade` - The max number of days since the token was last traded. Defaults +/// are chain specific and applied if not provided. pub async fn load_all_tokens( tycho_url: &str, no_tls: bool, auth_key: Option<&str>, chain: Chain, - quality_filter: Option, - activity_filter: Option, + min_quality: Option, + max_days_since_last_trade: Option, ) -> HashMap { info!("Loading tokens from Tycho..."); let rpc_url = @@ -61,14 +61,14 @@ pub async fn load_all_tokens( let rpc_client = HttpRPCClient::new(rpc_url.as_str(), auth_key).unwrap(); // Chain specific defaults for special case chains. Otherwise defaults to 42 days. - let default_activity_filter = HashMap::from([(Chain::Base, 10_u64)]); + let default_min_days = HashMap::from([(Chain::Base, 10_u64)]); #[allow(clippy::mutable_key_type)] rpc_client .get_all_tokens( chain, - quality_filter.or(Some(100)), - activity_filter.or(default_activity_filter + min_quality.or(Some(100)), + max_days_since_last_trade.or(default_min_days .get(&chain) .or(Some(&42)) .copied()),