Skip to content

Commit

Permalink
Merge pull request #133 from propeller-heads/lp/support-switching-chains
Browse files Browse the repository at this point in the history
feat: better support switching chains
  • Loading branch information
louise-poole authored Feb 5, 2025
2 parents 3ecd553 + 87e4e27 commit 29684cc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 58 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ mini-moka = "0.10"
lazy_static = "1.4.0"

# Tycho dependencies
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.46.0" }
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", tag = "0.46.0" }
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.55.2" }
tycho-client = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-client", tag = "0.55.2" }

# EVM dependencies
foundry-config = { git = "https://github.com/foundry-rs/foundry", rev = "57bb12e", optional = true }
Expand Down
9 changes: 7 additions & 2 deletions examples/price_printer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ async fn main() {
let (tick_tx, tick_rx) = mpsc::channel::<BlockUpdate>(12);

let tycho_message_processor: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
let all_tokens =
load_all_tokens(tycho_url.as_str(), false, Some(tycho_api_key.as_str())).await;
let all_tokens = load_all_tokens(
tycho_url.as_str(),
false,
Some(tycho_api_key.as_str()),
Chain::Ethereum,
)
.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::<UniswapV2State>("uniswap_v2", tvl_filter.clone(), None)
Expand Down
4 changes: 3 additions & 1 deletion examples/quickstart/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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())).await;
let all_tokens =
load_all_tokens(tycho_url.as_str(), false, Some(tycho_api_key.as_str()), Chain::Ethereum)
.await;
let mut pairs: HashMap<String, Vec<Token>> = HashMap::new();

let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum)
Expand Down
52 changes: 4 additions & 48 deletions src/evm/tycho_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, fmt::Display};
use alloy_primitives::{Address, B256, U256};
use chrono::{NaiveDateTime, Utc};
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
pub use tycho_core::dto::{Chain, ChangeType};
use uuid::Uuid;

use super::engine_db::simulation_db::BlockHeader;
Expand Down Expand Up @@ -110,50 +110,6 @@ impl BlockAccountChanges {
}
}

#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, EnumString, Display, Default,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Chain {
#[default]
Ethereum,
ZkSync,
Starknet,
Arbitrum,
}

impl From<tycho_core::dto::Chain> for Chain {
fn from(value: tycho_core::dto::Chain) -> Self {
match value {
tycho_core::dto::Chain::Ethereum => Chain::Ethereum,
tycho_core::dto::Chain::ZkSync => Chain::ZkSync,
tycho_core::dto::Chain::Starknet => Chain::Starknet,
tycho_core::dto::Chain::Arbitrum => Chain::Arbitrum,
}
}
}

#[derive(Debug, PartialEq, Default, Copy, Clone, Deserialize, Serialize, Display, EnumString)]
pub enum ChangeType {
#[default]
Update,
Deletion,
Creation,
Unspecified,
}

impl From<tycho_core::dto::ChangeType> for ChangeType {
fn from(value: tycho_core::dto::ChangeType) -> Self {
match value {
tycho_core::dto::ChangeType::Update => ChangeType::Update,
tycho_core::dto::ChangeType::Deletion => ChangeType::Deletion,
tycho_core::dto::ChangeType::Creation => ChangeType::Creation,
tycho_core::dto::ChangeType::Unspecified => ChangeType::Unspecified,
}
}
}

#[derive(PartialEq, Serialize, Deserialize, Clone, Debug)]
pub struct AccountUpdate {
pub address: Address,
Expand Down Expand Up @@ -182,14 +138,14 @@ impl AccountUpdate {
impl From<tycho_core::dto::AccountUpdate> for AccountUpdate {
fn from(value: tycho_core::dto::AccountUpdate) -> Self {
Self {
chain: value.chain.into(),
chain: value.chain,
address: Address::from_slice(&value.address[..20]), // Convert address field to Address
slots: u256_num::map_slots_to_u256(value.slots),
balance: value
.balance
.map(|balance| u256_num::bytes_to_u256(balance.into())),
code: value.code.map(|code| code.to_vec()),
change: value.change.into(),
change: value.change,
}
}
}
Expand Down Expand Up @@ -304,7 +260,7 @@ impl std::fmt::Debug for ResponseAccount {
impl From<tycho_core::dto::ResponseAccount> for ResponseAccount {
fn from(value: tycho_core::dto::ResponseAccount) -> Self {
Self {
chain: value.chain.into(),
chain: value.chain,
address: Address::from_slice(&value.address[..20]), // Convert address field to Address
title: value.title.clone(),
slots: u256_num::map_slots_to_u256(value.slots),
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ pub async fn load_all_tokens(
tycho_url: &str,
no_tls: bool,
auth_key: Option<&str>,
chain: Chain,
) -> HashMap<Bytes, Token> {
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();

#[allow(clippy::mutable_key_type)]
rpc_client
.get_all_tokens(Chain::Ethereum, Some(100), Some(42), 3_000)
.get_all_tokens(chain, Some(100), Some(42), 3_000)
.await
.expect("Unable to load tokens")
.into_iter()
Expand Down

0 comments on commit 29684cc

Please sign in to comment.