Skip to content

Commit

Permalink
feat: Extend ProtocolComponent to have the attributes of the core model
Browse files Browse the repository at this point in the history
Also implement From<ProtocolComponent> 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
  • Loading branch information
dianacarvalho1 committed Feb 5, 2025
1 parent 56ad758 commit 38f9cbc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/price_printer/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -163,7 +163,7 @@ impl App {
});
}
None => {
warn!("Received update for unknown pool {}", comp.address)
warn!("Received update for unknown pool {}", comp.id)
}
};
}
Expand All @@ -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]);
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/evm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ impl TychoStreamDecoder {
})
.collect::<Result<Vec<_>, StreamDecodeError>>()?
.into_iter()
.flat_map(|(id, address, comp)| {
.flat_map(|(id, _, comp)| {
let tokens = comp
.tokens
.iter()
.flat_map(|addr| state_guard.tokens.get(addr).cloned())
.collect::<Vec<_>>();

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
Expand Down Expand Up @@ -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
Expand Down
52 changes: 48 additions & 4 deletions src/protocol/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,14 +42,57 @@ use crate::models::Token;
/// * `tokens`: `Vec<ERC20Token>`, 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<Token>,
pub protocol_system: String,
pub protocol_type_name: String,
pub chain: Chain,
pub contract_ids: Vec<Bytes>,
pub static_attributes: HashMap<String, Bytes>,
pub creation_tx: Bytes,
pub created_at: NaiveDateTime,
}

impl ProtocolComponent {
pub fn new(address: Bytes, mut tokens: Vec<Token>) -> Self {
#[allow(deprecated)]
pub fn new(mut tokens: Vec<Token>, 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<ProtocolComponent> 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,
}
}
}

Expand Down

0 comments on commit 38f9cbc

Please sign in to comment.