From 38f9cbca680f5a4e617c111f9647ce7622e2adf0 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Wed, 5 Feb 2025 17:46:45 +0000 Subject: [PATCH] feat: Extend ProtocolComponent to have the attributes of the core model Also implement From for tycho_core::dto::ProtocolComponent. This will be useful for tycho-execution Deprecate address in ProtocolComponent. It should be id instead --- don't change below this line --- ENG-4207 Took 1 hour 15 minutes --- examples/price_printer/ui.rs | 8 +++--- src/evm/decoder.rs | 6 ++--- src/protocol/models.rs | 52 +++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/examples/price_printer/ui.rs b/examples/price_printer/ui.rs index df2a2278..b1a6bb13 100644 --- a/examples/price_printer/ui.rs +++ b/examples/price_printer/ui.rs @@ -140,7 +140,7 @@ impl App { pub fn update_data(&mut self, update: BlockUpdate) { for (id, comp) in update.new_pairs.iter() { - let name = format!("{:#042x}", comp.address); + let name = format!("{:#042x}", comp.id); let tokens = comp .tokens .iter() @@ -163,7 +163,7 @@ impl App { }); } None => { - warn!("Received update for unknown pool {}", comp.address) + warn!("Received update for unknown pool {}", comp.id) } }; } @@ -173,7 +173,7 @@ impl App { let entry = self .items .iter() - .find_position(|e| e.component.address == eth_address); + .find_position(|e| e.component.id == eth_address); if let Some((index, _)) = entry { let row = self.items.get_mut(index).unwrap(); let price = state.spot_price(&row.component.tokens[0], &row.component.tokens[1]); @@ -187,7 +187,7 @@ impl App { .items .iter() .enumerate() - .find(|(_, e)| e.component.address == comp.address); + .find(|(_, e)| e.component.id == comp.id); if let Some((idx, _)) = entry { self.items.remove(idx); } diff --git a/src/evm/decoder.rs b/src/evm/decoder.rs index dbea83b0..3d7c3aaa 100644 --- a/src/evm/decoder.rs +++ b/src/evm/decoder.rs @@ -207,7 +207,7 @@ impl TychoStreamDecoder { }) .collect::, StreamDecodeError>>()? .into_iter() - .flat_map(|(id, address, comp)| { + .flat_map(|(id, _, comp)| { let tokens = comp .tokens .iter() @@ -215,7 +215,7 @@ impl TychoStreamDecoder { .collect::>(); if tokens.len() == comp.tokens.len() { - Some((id.clone(), ProtocolComponent::new(address, tokens))) + Some((id.clone(), ProtocolComponent::new(tokens, comp.clone()))) } else { // We may reach this point if the removed component // contained low quality tokens, in this case the component @@ -274,7 +274,7 @@ impl TychoStreamDecoder { } new_pairs.insert( id.clone(), - ProtocolComponent::new(Bytes::from(id.as_str()), component_tokens), + ProtocolComponent::new(component_tokens, snapshot.component.clone()), ); // Construct state from snapshot diff --git a/src/protocol/models.rs b/src/protocol/models.rs index 5b14d654..d5f32c9c 100644 --- a/src/protocol/models.rs +++ b/src/protocol/models.rs @@ -24,11 +24,12 @@ //! It's worth emphasizing that although the term "pair" used in this //! module refers to a trading pair, it does not necessarily imply two //! tokens only. Some pairs might have more than two tokens. -use std::{collections::HashMap, future::Future}; +use std::{collections::HashMap, default::Default, future::Future}; +use chrono::NaiveDateTime; use num_bigint::BigUint; use tycho_client::feed::Header; -use tycho_core::Bytes; +use tycho_core::{dto::Chain, Bytes}; use super::state::ProtocolSim; use crate::models::Token; @@ -41,14 +42,57 @@ use crate::models::Token; /// * `tokens`: `Vec`, the tokens of the trading pair #[derive(Debug, Clone, PartialEq, Eq)] pub struct ProtocolComponent { + #[deprecated(since = "0.73.0", note = "Use `id` instead")] pub address: Bytes, + pub id: Bytes, pub tokens: Vec, + pub protocol_system: String, + pub protocol_type_name: String, + pub chain: Chain, + pub contract_ids: Vec, + pub static_attributes: HashMap, + pub creation_tx: Bytes, + pub created_at: NaiveDateTime, } impl ProtocolComponent { - pub fn new(address: Bytes, mut tokens: Vec) -> Self { + #[allow(deprecated)] + pub fn new(mut tokens: Vec, core_model: tycho_core::dto::ProtocolComponent) -> Self { tokens.sort_unstable_by_key(|t| t.address.clone()); - ProtocolComponent { address, tokens } + let id = Bytes::from(core_model.id.as_str()); + ProtocolComponent { + id: id.clone(), + address: id, + tokens, + protocol_system: core_model.protocol_system, + protocol_type_name: core_model.protocol_type_name, + chain: core_model.chain, + contract_ids: core_model.contract_ids, + static_attributes: core_model.static_attributes, + creation_tx: core_model.creation_tx, + created_at: core_model.created_at, + } + } +} + +impl From for tycho_core::dto::ProtocolComponent { + fn from(component: ProtocolComponent) -> Self { + tycho_core::dto::ProtocolComponent { + id: hex::encode(component.id), + protocol_system: component.protocol_system, + protocol_type_name: component.protocol_type_name, + chain: component.chain, + tokens: component + .tokens + .into_iter() + .map(|t| t.address) + .collect(), + contract_ids: component.contract_ids, + static_attributes: component.static_attributes, + change: Default::default(), + creation_tx: component.creation_tx, + created_at: component.created_at, + } } }