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)]
59pub struct DecoderContext {
60 pub adapter_path: Option<String>,
61 pub vm_traces: Option<bool>,
62 pub block_position: BlockPositionAssumption,
68 pub(crate) live_override: Option<watch::Receiver<OverrideSnapshot>>,
73 pub chain: Option<Chain>,
80}
81
82impl DecoderContext {
83 pub fn new() -> Self {
84 Self {
85 adapter_path: None,
86 vm_traces: None,
87 block_position: BlockPositionAssumption::default(),
88 live_override: None,
89 chain: None,
90 }
91 }
92
93 pub fn chain(mut self, chain: Chain) -> Self {
99 self.chain = Some(chain);
100 self
101 }
102
103 pub fn block_position_assumption(mut self, assumption: BlockPositionAssumption) -> Self {
104 self.block_position = assumption;
105 self
106 }
107
108 pub fn vm_adapter_path<S: Into<String>>(mut self, path: S) -> Self {
109 self.adapter_path = Some(path.into());
110 self
111 }
112
113 pub fn vm_traces(mut self, trace: bool) -> Self {
114 self.vm_traces = Some(trace);
115 self
116 }
117}
118
119impl Default for DecoderContext {
120 fn default() -> Self {
121 Self::new()
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
132pub struct ProtocolComponent {
133 #[deprecated(since = "0.73.0", note = "Use `id` instead")]
134 pub address: Bytes,
135 pub id: Bytes,
136 pub tokens: Vec<Token>,
137 pub protocol_system: String,
138 pub protocol_type_name: String,
139 pub chain: Chain,
140 pub contract_ids: Vec<Bytes>,
141 pub static_attributes: HashMap<String, Bytes>,
142 pub creation_tx: Bytes,
143 pub created_at: NaiveDateTime,
144}
145
146impl ProtocolComponent {
147 #[allow(deprecated)]
148 #[allow(clippy::too_many_arguments)]
149 pub fn new(
150 id: Bytes,
151 protocol_system: String,
152 protocol_type_name: String,
153 chain: Chain,
154 tokens: Vec<Token>,
155 contract_ids: Vec<Bytes>,
156 static_attributes: HashMap<String, Bytes>,
157 creation_tx: Bytes,
158 created_at: NaiveDateTime,
159 ) -> Self {
160 ProtocolComponent {
161 address: Default::default(),
162 id,
163 tokens,
164 protocol_system,
165 protocol_type_name,
166 chain,
167 contract_ids,
168 static_attributes,
169 creation_tx,
170 created_at,
171 }
172 }
173
174 pub fn from_with_tokens(
175 core_model: tycho_common::models::protocol::ProtocolComponent,
176 tokens: Vec<Token>,
177 ) -> Self {
178 let id = Bytes::from(core_model.id.as_str());
179 ProtocolComponent::new(
180 id.clone(),
181 core_model.protocol_system,
182 core_model.protocol_type_name,
183 core_model.chain,
184 tokens,
185 core_model.contract_addresses,
186 core_model.static_attributes,
187 core_model.creation_tx,
188 core_model.created_at,
189 )
190 }
191}
192
193impl From<ProtocolComponent> for tycho_common::models::protocol::ProtocolComponent {
194 fn from(component: ProtocolComponent) -> Self {
195 tycho_common::models::protocol::ProtocolComponent {
196 id: hex::encode(component.id),
197 protocol_system: component.protocol_system,
198 protocol_type_name: component.protocol_type_name,
199 chain: component.chain,
200 tokens: component
201 .tokens
202 .into_iter()
203 .map(|t| t.address)
204 .collect(),
205 static_attributes: component.static_attributes,
206 change: Default::default(),
207 creation_tx: component.creation_tx,
208 created_at: component.created_at,
209 contract_addresses: component.contract_ids,
210 }
211 }
212}
213
214pub trait TryFromWithBlock<T, H>
215where
216 H: HeaderLike,
217{
218 type Error;
219
220 fn try_from_with_header(
221 value: T,
222 block: H,
223 account_balances: &HashMap<Bytes, HashMap<Bytes, Bytes>>,
224 all_tokens: &HashMap<Bytes, Token>,
225 decoder_context: &DecoderContext,
226 ) -> impl Future<Output = Result<Self, Self::Error>> + Send + Sync
227 where
228 Self: Sized;
229}
230
231#[derive(Debug, Clone, Serialize, Deserialize)]
232pub struct Update {
233 pub block_number_or_timestamp: u64,
234 #[serde(default)]
236 pub is_partial: bool,
237 pub sync_states: HashMap<String, SynchronizerState>,
239 #[serde(with = "crate::serde_helpers::protocol_states")]
243 pub states: HashMap<String, Box<dyn ProtocolSim>>,
244 pub new_pairs: HashMap<String, ProtocolComponent>,
246 pub removed_pairs: HashMap<String, ProtocolComponent>,
248}
249
250impl Update {
251 pub fn new(
252 block_number: u64,
253 states: HashMap<String, Box<dyn ProtocolSim>>,
254 new_pairs: HashMap<String, ProtocolComponent>,
255 ) -> Self {
256 Update {
257 block_number_or_timestamp: block_number,
258 is_partial: false,
259 sync_states: HashMap::new(),
260 states,
261 new_pairs,
262 removed_pairs: HashMap::new(),
263 }
264 }
265
266 pub fn set_is_partial(mut self, is_partial: bool) -> Self {
267 self.is_partial = is_partial;
268 self
269 }
270
271 pub fn set_removed_pairs(mut self, pairs: HashMap<String, ProtocolComponent>) -> Self {
272 self.removed_pairs = pairs;
273 self
274 }
275
276 pub fn set_sync_states(mut self, sync_states: HashMap<String, SynchronizerState>) -> Self {
277 self.sync_states = sync_states;
278 self
279 }
280
281 pub fn merge(mut self, other: Update) -> Self {
282 self.states.extend(other.states);
283 self.new_pairs.extend(other.new_pairs);
284 self.removed_pairs
285 .extend(other.removed_pairs);
286 self
287 }
288}