tycho_simulation/protocol/
models.rs1use std::{collections::HashMap, default::Default, future::Future};
28
29use chrono::NaiveDateTime;
30use serde::{Deserialize, Serialize};
31use tokio::sync::watch;
32use tycho_client::feed::{HeaderLike, SynchronizerState};
33use tycho_common::{
34 models::{token::Token, Chain},
35 simulation::protocol_sim::ProtocolSim,
36 Bytes,
37};
38
39use crate::evm::override_stream::OverrideSnapshot;
40
41#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
44pub enum BlockPositionAssumption {
45 #[default]
48 WorstCase,
49 First,
53}
54
55#[derive(Debug, Clone)]
59#[expect(
60 clippy::partial_pub_fields,
61 reason = "public API; making every field public or private is a breaking change"
62)]
63pub struct DecoderContext {
64 pub adapter_path: Option<String>,
65 pub vm_traces: Option<bool>,
66 pub block_position: BlockPositionAssumption,
72 pub(crate) live_override: Option<watch::Receiver<OverrideSnapshot>>,
77 pub chain: Option<Chain>,
84}
85
86impl DecoderContext {
87 pub fn new() -> Self {
88 Self {
89 adapter_path: None,
90 vm_traces: None,
91 block_position: BlockPositionAssumption::default(),
92 live_override: None,
93 chain: None,
94 }
95 }
96
97 pub fn chain(mut self, chain: Chain) -> Self {
103 self.chain = Some(chain);
104 self
105 }
106
107 pub fn block_position_assumption(mut self, assumption: BlockPositionAssumption) -> Self {
108 self.block_position = assumption;
109 self
110 }
111
112 pub fn vm_adapter_path<S: Into<String>>(mut self, path: S) -> Self {
113 self.adapter_path = Some(path.into());
114 self
115 }
116
117 pub fn vm_traces(mut self, trace: bool) -> Self {
118 self.vm_traces = Some(trace);
119 self
120 }
121}
122
123impl Default for DecoderContext {
124 fn default() -> Self {
125 Self::new()
126 }
127}
128
129#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
136pub struct ProtocolComponent {
137 #[deprecated(since = "0.73.0", note = "Use `id` instead")]
138 pub address: Bytes,
139 pub id: Bytes,
140 pub tokens: Vec<Token>,
141 pub protocol_system: String,
142 pub protocol_type_name: String,
143 pub chain: Chain,
144 pub contract_ids: Vec<Bytes>,
145 pub static_attributes: HashMap<String, Bytes>,
146 pub creation_tx: Bytes,
147 pub created_at: NaiveDateTime,
148}
149
150impl ProtocolComponent {
151 #[expect(
152 deprecated,
153 reason = "fills the deprecated address field so consumers still reading it get a value"
154 )]
155 #[expect(
156 clippy::too_many_arguments,
157 reason = "public API; grouping the arguments into a params struct or builder is a breaking change"
158 )]
159 pub fn new(
160 id: Bytes,
161 protocol_system: String,
162 protocol_type_name: String,
163 chain: Chain,
164 tokens: Vec<Token>,
165 contract_ids: Vec<Bytes>,
166 static_attributes: HashMap<String, Bytes>,
167 creation_tx: Bytes,
168 created_at: NaiveDateTime,
169 ) -> Self {
170 ProtocolComponent {
171 address: Default::default(),
172 id,
173 tokens,
174 protocol_system,
175 protocol_type_name,
176 chain,
177 contract_ids,
178 static_attributes,
179 creation_tx,
180 created_at,
181 }
182 }
183
184 pub fn from_with_tokens(
185 core_model: tycho_common::models::protocol::ProtocolComponent,
186 tokens: Vec<Token>,
187 ) -> Self {
188 let id = Bytes::from(core_model.id.as_str());
189 ProtocolComponent::new(
190 id,
191 core_model.protocol_system,
192 core_model.protocol_type_name,
193 core_model.chain,
194 tokens,
195 core_model.contract_addresses,
196 core_model.static_attributes,
197 core_model.creation_tx,
198 core_model.created_at,
199 )
200 }
201}
202
203impl From<ProtocolComponent> for tycho_common::models::protocol::ProtocolComponent {
204 fn from(component: ProtocolComponent) -> Self {
205 tycho_common::models::protocol::ProtocolComponent {
206 id: hex::encode(component.id),
207 protocol_system: component.protocol_system,
208 protocol_type_name: component.protocol_type_name,
209 chain: component.chain,
210 tokens: component
211 .tokens
212 .into_iter()
213 .map(|t| t.address)
214 .collect(),
215 static_attributes: component.static_attributes,
216 change: Default::default(),
217 creation_tx: component.creation_tx,
218 created_at: component.created_at,
219 contract_addresses: component.contract_ids,
220 }
221 }
222}
223
224pub trait TryFromWithBlock<T, H>
225where
226 H: HeaderLike,
227{
228 type Error;
229
230 fn try_from_with_header(
231 snapshot: T,
232 block: H,
233 account_balances: &HashMap<Bytes, HashMap<Bytes, Bytes>>,
234 all_tokens: &HashMap<Bytes, Token>,
235 decoder_context: &DecoderContext,
236 ) -> impl Future<Output = Result<Self, Self::Error>> + Send + Sync
237 where
238 Self: Sized;
239}
240
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct Update {
243 pub block_number: u64,
244 #[serde(default)]
246 pub is_partial: bool,
247 pub sync_states: HashMap<String, SynchronizerState>,
249 #[serde(with = "crate::serde_helpers::protocol_states")]
253 pub states: HashMap<String, Box<dyn ProtocolSim>>,
254 pub new_pairs: HashMap<String, ProtocolComponent>,
256 pub removed_pairs: HashMap<String, ProtocolComponent>,
258}
259
260impl Update {
261 pub fn new(
262 block_number: u64,
263 states: HashMap<String, Box<dyn ProtocolSim>>,
264 new_pairs: HashMap<String, ProtocolComponent>,
265 ) -> Self {
266 Update {
267 block_number,
268 is_partial: false,
269 sync_states: HashMap::new(),
270 states,
271 new_pairs,
272 removed_pairs: HashMap::new(),
273 }
274 }
275
276 pub fn set_is_partial(mut self, is_partial: bool) -> Self {
277 self.is_partial = is_partial;
278 self
279 }
280
281 pub fn set_removed_pairs(mut self, pairs: HashMap<String, ProtocolComponent>) -> Self {
282 self.removed_pairs = pairs;
283 self
284 }
285
286 pub fn set_sync_states(mut self, sync_states: HashMap<String, SynchronizerState>) -> Self {
287 self.sync_states = sync_states;
288 self
289 }
290
291 pub fn merge(mut self, other: Update) -> Self {
292 self.states.extend(other.states);
293 self.new_pairs.extend(other.new_pairs);
294 self.removed_pairs
295 .extend(other.removed_pairs);
296 self
297 }
298}