tycho_simulation/evm/protocol/vm/
decoder.rs1use std::{
2 collections::{HashMap, HashSet},
3 str::FromStr,
4};
5
6use alloy::primitives::{Address, U256};
7use revm::state::Bytecode;
8use tycho_client::feed::{synchronizer::ComponentWithState, BlockHeader};
9use tycho_common::{models::token::Token, simulation::errors::SimulationError, Bytes};
10
11use super::{state::EVMPoolState, state_builder::EVMPoolStateBuilder};
12use crate::{
13 evm::{
14 engine_db::{tycho_db::PreCachedDB, SHARED_TYCHO_DB},
15 protocol::vm::{constants::get_adapter_file, utils::json_deserialize_address_list},
16 simulation::BlockEnvOverrides,
17 },
18 protocol::{
19 errors::InvalidSnapshotError,
20 models::{DecoderContext, TryFromWithBlock},
21 },
22};
23
24impl TryFromWithBlock<ComponentWithState, BlockHeader> for EVMPoolState<PreCachedDB> {
25 type Error = InvalidSnapshotError;
26
27 #[allow(deprecated)]
32 async fn try_from_with_header(
33 snapshot: ComponentWithState,
34 _block: BlockHeader,
35 account_balances: &HashMap<Bytes, HashMap<Bytes, Bytes>>,
36 all_tokens: &HashMap<Bytes, Token>,
37 decoder_context: &DecoderContext,
38 ) -> Result<Self, Self::Error> {
39 let id = snapshot.component.id.clone();
40 let tokens = snapshot.component.tokens.clone();
41
42 let mut stateless_contracts = HashMap::new();
44 let mut index = 0;
45
46 loop {
47 let address_key = format!("stateless_contract_addr_{index}");
48 if let Some(encoded_address_bytes) = snapshot
49 .state
50 .attributes
51 .get(&address_key)
52 {
53 let encoded_address = hex::encode(encoded_address_bytes);
54 let address_hex = encoded_address
56 .strip_prefix("0x")
57 .unwrap_or(&encoded_address);
58
59 let decoded = match hex::decode(address_hex) {
60 Ok(decoded_bytes) => match String::from_utf8(decoded_bytes) {
61 Ok(decoded_string) => decoded_string,
62 Err(_) => continue,
63 },
64 Err(_) => continue,
65 };
66
67 let code_key = format!("stateless_contract_code_{index}");
68 let code = snapshot
69 .state
70 .attributes
71 .get(&code_key)
72 .map(|value| value.to_vec());
73
74 stateless_contracts.insert(decoded, code);
75 index += 1;
76 } else {
77 break;
78 }
79 }
80 let involved_contracts = snapshot
81 .component
82 .contract_addresses
83 .iter()
84 .map(|bytes: &Bytes| Address::from_slice(bytes.as_ref()))
85 .collect::<HashSet<Address>>();
86
87 let potential_rebase_tokens: HashSet<Address> = if let Some(bytes) = snapshot
88 .component
89 .static_attributes
90 .get("rebase_tokens")
91 {
92 if let Ok(vecs) = json_deserialize_address_list(bytes) {
93 vecs.into_iter()
94 .map(|addr| Address::from_slice(&addr))
95 .collect()
96 } else {
97 HashSet::new()
98 }
99 } else {
100 HashSet::new()
101 };
102
103 let balance_owner = snapshot
105 .state
106 .attributes
107 .get("balance_owner")
108 .map(|owner| Address::from_slice(owner.as_ref()));
109 let component_balances = snapshot
110 .state
111 .balances
112 .iter()
113 .map(|(k, v)| (Address::from_slice(k), U256::from_be_slice(v)))
114 .collect::<HashMap<_, _>>();
115 let account_balances = account_balances
116 .iter()
117 .filter(|(k, _)| involved_contracts.contains(&Address::from_slice(k)))
118 .map(|(k, v)| {
119 let addr = Address::from_slice(k);
120 let balances = v
121 .iter()
122 .map(|(k, v)| (Address::from_slice(k), U256::from_be_slice(v)))
123 .collect();
124 (addr, balances)
125 })
126 .collect::<HashMap<_, _>>();
127
128 let manual_updates = snapshot
129 .component
130 .static_attributes
131 .contains_key("manual_updates");
132
133 let protocol_name = snapshot
134 .component
135 .protocol_system
136 .strip_prefix("vm:")
137 .unwrap_or({
138 snapshot
139 .component
140 .protocol_system
141 .as_str()
142 });
143 let adapter_bytecode;
144 if let Some(adapter_bytecode_path) = &decoder_context.adapter_path {
145 let bytecode_bytes = std::fs::read(adapter_bytecode_path).map_err(|e| {
146 SimulationError::FatalError(format!(
147 "Failed to read adapter bytecode from {adapter_bytecode_path}: {e}"
148 ))
149 })?;
150 adapter_bytecode = Bytecode::new_raw(bytecode_bytes.into());
151 } else {
152 adapter_bytecode = Bytecode::new_raw(get_adapter_file(protocol_name)?.into());
153 }
154 let adapter_contract_address = Address::from_str(&format!(
155 "{hex_protocol_name:0>40}",
156 hex_protocol_name = hex::encode(protocol_name)
157 ))
158 .map_err(|_| {
159 InvalidSnapshotError::ValueError(
160 "Error converting protocol name to address".to_string(),
161 )
162 })?;
163 let mut vm_traces = false;
164 if let Some(trace) = &decoder_context.vm_traces {
165 vm_traces = *trace;
166 }
167 let block_number = snapshot
171 .state
172 .attributes
173 .get("override_block_number")
174 .map(|block_number| {
175 <[u8; 8]>::try_from(block_number.as_ref())
176 .map(u64::from_be_bytes)
177 .map_err(|_| {
178 InvalidSnapshotError::ValueError(
179 "override_block_number attribute must be an 8-byte big-endian u64"
180 .to_string(),
181 )
182 })
183 })
184 .transpose()?;
185 let block_timestamp = snapshot
186 .state
187 .attributes
188 .get("override_block_timestamp")
189 .map(|block_timestamp| {
190 <[u8; 8]>::try_from(block_timestamp.as_ref())
191 .map(u64::from_be_bytes)
192 .map_err(|_| {
193 InvalidSnapshotError::ValueError(
194 "override_block_timestamp attribute must be an 8-byte big-endian u64"
195 .to_string(),
196 )
197 })
198 })
199 .transpose()?;
200 let block_overrides = if block_number.is_some() || block_timestamp.is_some() {
201 Some(BlockEnvOverrides { number: block_number, timestamp: block_timestamp })
202 } else {
203 None
204 };
205 let mut pool_state_builder =
206 EVMPoolStateBuilder::new(id.clone(), tokens.clone(), adapter_contract_address)
207 .balances(component_balances)
208 .disable_overwrite_tokens(potential_rebase_tokens)
209 .account_balances(account_balances)
210 .adapter_contract_bytecode(adapter_bytecode)
211 .involved_contracts(involved_contracts)
212 .stateless_contracts(stateless_contracts)
213 .manual_updates(manual_updates)
214 .trace(vm_traces)
215 .block_overrides(block_overrides);
216
217 if let Some(balance_owner) = balance_owner {
218 pool_state_builder = pool_state_builder.balance_owner(balance_owner)
219 };
220
221 let mut pool_state = pool_state_builder
222 .build(SHARED_TYCHO_DB.clone())
223 .await
224 .map_err(InvalidSnapshotError::VMError)?;
225
226 pool_state.set_spot_prices(all_tokens)?;
227
228 Ok(pool_state)
229 }
230}
231
232#[cfg(test)]
233mod tests {
234 use std::{collections::HashSet, fs, path::Path};
235
236 use chrono::DateTime;
237 use revm::{primitives::KECCAK_EMPTY, state::AccountInfo};
238 use serde_json::Value;
239 use tycho_common::models::{
240 protocol::{ProtocolComponent, ProtocolComponentState},
241 Chain, ChangeType,
242 };
243
244 use super::*;
245 use crate::evm::{
246 engine_db::{create_engine, engine_db_interface::EngineDatabaseInterface},
247 protocol::vm::constants::{BALANCER_V2, CURVE},
248 tycho_models::AccountUpdate,
249 };
250
251 #[test]
252 fn test_to_adapter_file_name() {
253 assert_eq!(get_adapter_file("balancer_v2").unwrap(), BALANCER_V2);
254 assert_eq!(get_adapter_file("curve").unwrap(), CURVE);
255 }
256
257 fn vm_component() -> ProtocolComponent {
258 let creation_time = DateTime::from_timestamp(1622526000, 0)
259 .unwrap()
260 .naive_utc(); let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
263 static_attributes.insert("manual_updates".to_string(), Bytes::from_str("0x01").unwrap());
264
265 let dai_addr = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
266 let bal_addr = Bytes::from_str("0xba100000625a3754423978a60c9317c58a424e3d").unwrap();
267 let tokens = vec![dai_addr, bal_addr];
268
269 ProtocolComponent {
270 id: "0x4626d81b3a1711beb79f4cecff2413886d461677000200000000000000000011".to_string(),
271 protocol_system: "vm:balancer_v2".to_string(),
272 protocol_type_name: "balancer_v2_pool".to_string(),
273 chain: Chain::Ethereum,
274 tokens,
275 contract_addresses: vec![
276 Bytes::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap()
277 ],
278 static_attributes,
279 change: ChangeType::Creation,
280 creation_tx: Bytes::from_str("0x0000").unwrap(),
281 created_at: creation_time,
282 }
283 }
284
285 fn load_balancer_account_data() -> Vec<AccountUpdate> {
286 let project_root = env!("CARGO_MANIFEST_DIR");
287 let asset_path =
288 Path::new(project_root).join("tests/assets/decoder/balancer_v2_snapshot.json");
289 let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
290 let data: Value = serde_json::from_str(&json_data).expect("Failed to parse JSON");
291
292 let accounts: Vec<AccountUpdate> = serde_json::from_value(data["accounts"].clone())
293 .expect("Expected accounts to match AccountUpdate structure");
294 accounts
295 }
296
297 #[tokio::test]
298 async fn test_try_from_with_header() {
299 let attributes: HashMap<String, Bytes> = vec![
300 (
301 "balance_owner".to_string(),
302 Bytes::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap(),
303 ),
304 ("override_block_number".to_string(), Bytes::from(123_u64.to_be_bytes().to_vec())),
305 ("override_block_timestamp".to_string(), Bytes::from(456_u64.to_be_bytes().to_vec())),
306 ("reserve1".to_string(), Bytes::from(200_u64.to_le_bytes().to_vec())),
307 ]
308 .into_iter()
309 .collect();
310 let tokens = [
311 Token::new(
312 &Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(),
313 "DAI",
314 18,
315 0,
316 &[Some(10_000)],
317 tycho_common::models::Chain::Ethereum,
318 100,
319 ),
320 Token::new(
321 &Bytes::from_str("0xba100000625a3754423978a60c9317c58a424e3d").unwrap(),
322 "BAL",
323 18,
324 0,
325 &[Some(10_000)],
326 tycho_common::models::Chain::Ethereum,
327 100,
328 ),
329 ]
330 .into_iter()
331 .map(|t| (t.address.clone(), t))
332 .collect::<HashMap<_, _>>();
333 let snapshot = ComponentWithState {
334 state: ProtocolComponentState {
335 component_id: "0x4626d81b3a1711beb79f4cecff2413886d461677000200000000000000000011"
336 .to_owned(),
337 attributes,
338 balances: HashMap::new(),
339 },
340 component: vm_component(),
341 component_tvl: None,
342 entrypoints: Vec::new(),
343 };
344 let block = BlockHeader::default();
346 let accounts = load_balancer_account_data();
347 let db = SHARED_TYCHO_DB.clone();
348 let engine = create_engine(db.clone(), false).unwrap();
349 for account in accounts.clone() {
350 engine
351 .state
352 .init_account(
353 account.address,
354 AccountInfo {
355 balance: account.balance.unwrap_or_default(),
356 nonce: 0u64,
357 code_hash: KECCAK_EMPTY,
358 code: account
359 .code
360 .clone()
361 .map(|arg0: Vec<u8>| Bytecode::new_raw(arg0.into())),
362 },
363 None,
364 false,
365 )
366 .expect("Failed to init account");
367 }
368 db.update(accounts, Some(block.clone()))
369 .unwrap();
370 let account_balances = HashMap::from([(
371 Bytes::from("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
372 HashMap::from([
373 (
374 Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"),
375 Bytes::from(100_u64.to_le_bytes().to_vec()),
376 ),
377 (
378 Bytes::from("0xba100000625a3754423978a60c9317c58a424e3d"),
379 Bytes::from(100_u64.to_le_bytes().to_vec()),
380 ),
381 ]),
382 )]);
383
384 let decoder_context = DecoderContext::new();
385 let res = EVMPoolState::try_from_with_header(
386 snapshot,
387 block,
388 &account_balances,
389 &tokens,
390 &decoder_context,
391 )
392 .await
393 .unwrap();
394
395 let res_pool = res;
396
397 assert_eq!(
398 res_pool.get_balance_owner(),
399 Some(Address::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap())
400 );
401 let mut exp_involved_contracts = HashSet::new();
402 exp_involved_contracts
403 .insert(Address::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap());
404 assert_eq!(res_pool.get_involved_contracts(), exp_involved_contracts);
405 assert!(res_pool.get_manual_updates());
406 assert_eq!(
407 res_pool.get_block_overrides(),
408 Some(BlockEnvOverrides { number: Some(123), timestamp: Some(456) })
409 );
410 }
411}