Skip to content

Commit

Permalink
Merge pull request #140 from propeller-heads/lp/update-load-all-tokens
Browse files Browse the repository at this point in the history
feat: add filter args to load_all_tokens
  • Loading branch information
louise-poole authored Feb 10, 2025
2 parents f49c386 + cfc4f51 commit 28c183d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/price_printer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions examples/quickstart/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Vec<Token>> = HashMap::new();

let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum)
Expand Down
27 changes: 26 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use tracing::info;
use tycho_client::{rpc::RPCClient, HttpRPCClient};
use tycho_core::{dto::Chain, Bytes};

Expand Down Expand Up @@ -36,19 +37,43 @@ pub fn hexstring_to_vec(hexstring: &str) -> Result<Vec<u8>, 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.
/// * `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,
min_quality: Option<i32>,
max_days_since_last_trade: Option<u64>,
) -> HashMap<Bytes, Token> {
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_min_days = 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,
min_quality.or(Some(100)),
max_days_since_last_trade.or(default_min_days
.get(&chain)
.or(Some(&42))
.copied()),
3_000,
)
.await
.expect("Unable to load tokens")
.into_iter()
Expand Down

0 comments on commit 28c183d

Please sign in to comment.