Skip to main content

tycho_simulation/evm/protocol/vm/
decoder.rs

1use 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    /// Decodes a `ComponentWithState`, block `BlockHeader` and HashMap of all available tokens into
28    /// an `EVMPoolState`.
29    ///
30    /// Errors with a `InvalidSnapshotError`.
31    #[expect(
32        deprecated,
33        reason = "indexed snapshots may carry a balance_owner, which only the deprecated builder setter accepts"
34    )]
35    async fn try_from_with_header(
36        snapshot: ComponentWithState,
37        _block: BlockHeader,
38        account_balances: &HashMap<Bytes, HashMap<Bytes, Bytes>>,
39        all_tokens: &HashMap<Bytes, Token>,
40        decoder_context: &DecoderContext,
41    ) -> Result<Self, Self::Error> {
42        let id = snapshot.component.id.clone();
43        let tokens = snapshot.component.tokens.clone();
44
45        // Decode involved contracts
46        let mut stateless_contracts = HashMap::new();
47        let mut index = 0;
48
49        loop {
50            let address_key = format!("stateless_contract_addr_{index}");
51            if let Some(encoded_address_bytes) = snapshot
52                .state
53                .attributes
54                .get(&address_key)
55            {
56                let encoded_address = hex::encode(encoded_address_bytes);
57                // Stateless contracts address are UTF-8 encoded
58                let address_hex = encoded_address
59                    .strip_prefix("0x")
60                    .unwrap_or(&encoded_address);
61
62                let decoded = match hex::decode(address_hex) {
63                    Ok(decoded_bytes) => match String::from_utf8(decoded_bytes) {
64                        Ok(decoded_string) => decoded_string,
65                        Err(_) => continue,
66                    },
67                    Err(_) => continue,
68                };
69
70                let code_key = format!("stateless_contract_code_{index}");
71                let code = snapshot
72                    .state
73                    .attributes
74                    .get(&code_key)
75                    .map(|value| value.to_vec());
76
77                stateless_contracts.insert(decoded, code);
78                index += 1;
79            } else {
80                break;
81            }
82        }
83        let involved_contracts = snapshot
84            .component
85            .contract_addresses
86            .iter()
87            .map(|bytes: &Bytes| Address::from_slice(bytes.as_ref()))
88            .collect::<HashSet<Address>>();
89
90        let potential_rebase_tokens: HashSet<Address> = if let Some(bytes) = snapshot
91            .component
92            .static_attributes
93            .get("rebase_tokens")
94        {
95            if let Ok(vecs) = json_deserialize_address_list(bytes) {
96                vecs.into_iter()
97                    .map(|addr| Address::from_slice(&addr))
98                    .collect()
99            } else {
100                HashSet::new()
101            }
102        } else {
103            HashSet::new()
104        };
105
106        // Tokens whose protocol does not emit token contract storage. Simulation handles their
107        // transfers entirely in the proxy's local bookkeeping (custom approval + recipient balance)
108        // so they never delegate to an implementation another protocol may have left in the shared
109        // DB.
110        let self_contained_tokens: HashSet<Address> = if let Some(bytes) = snapshot
111            .component
112            .static_attributes
113            .get("self_contained_tokens")
114        {
115            if let Ok(vecs) = json_deserialize_address_list(bytes) {
116                vecs.into_iter()
117                    .map(|addr| Address::from_slice(&addr))
118                    .collect()
119            } else {
120                HashSet::new()
121            }
122        } else {
123            HashSet::new()
124        };
125
126        // Decode balances
127        let balance_owner = snapshot
128            .state
129            .attributes
130            .get("balance_owner")
131            .map(|owner| Address::from_slice(owner.as_ref()));
132        let component_balances = snapshot
133            .state
134            .balances
135            .iter()
136            .map(|(k, v)| (Address::from_slice(k), U256::from_be_slice(v)))
137            .collect::<HashMap<_, _>>();
138        let account_balances = account_balances
139            .iter()
140            .filter(|(k, _)| involved_contracts.contains(&Address::from_slice(k)))
141            .map(|(k, v)| {
142                let addr = Address::from_slice(k);
143                let balances = v
144                    .iter()
145                    .map(|(k, v)| (Address::from_slice(k), U256::from_be_slice(v)))
146                    .collect();
147                (addr, balances)
148            })
149            .collect::<HashMap<_, _>>();
150
151        let manual_updates = snapshot
152            .component
153            .static_attributes
154            .contains_key("manual_updates");
155
156        let protocol_name = snapshot
157            .component
158            .protocol_system
159            .strip_prefix("vm:")
160            .unwrap_or({
161                snapshot
162                    .component
163                    .protocol_system
164                    .as_str()
165            });
166        let adapter_bytecode;
167        if let Some(adapter_bytecode_path) = &decoder_context.adapter_path {
168            let bytecode_bytes = std::fs::read(adapter_bytecode_path).map_err(|e| {
169                SimulationError::FatalError(format!(
170                    "Failed to read adapter bytecode from {adapter_bytecode_path}: {e}"
171                ))
172            })?;
173            adapter_bytecode = Bytecode::new_raw(bytecode_bytes.into());
174        } else {
175            adapter_bytecode = Bytecode::new_raw(get_adapter_file(protocol_name)?.into());
176        }
177        let adapter_contract_address = Address::from_str(&format!(
178            "{hex_protocol_name:0>40}",
179            hex_protocol_name = hex::encode(protocol_name)
180        ))
181        .map_err(|e| {
182            InvalidSnapshotError::ValueError(format!(
183                "Error converting protocol name to address: {e}"
184            ))
185        })?;
186        let mut vm_traces = false;
187        if let Some(trace) = &decoder_context.vm_traces {
188            vm_traces = *trace;
189        }
190        // A protocol may override only one block env field. In that case the VM call will use the
191        // overridden field together with the current block's other field, so block.number and
192        // block.timestamp may not correspond to the same real chain block.
193        let block_number = snapshot
194            .state
195            .attributes
196            .get("override_block_number")
197            .map(|block_number| {
198                <[u8; 8]>::try_from(block_number.as_ref())
199                    .map(u64::from_be_bytes)
200                    .map_err(|_wrong_length| {
201                        InvalidSnapshotError::ValueError(
202                            "override_block_number attribute must be an 8-byte big-endian u64"
203                                .to_string(),
204                        )
205                    })
206            })
207            .transpose()?;
208        let block_timestamp = snapshot
209            .state
210            .attributes
211            .get("override_block_timestamp")
212            .map(|block_timestamp| {
213                <[u8; 8]>::try_from(block_timestamp.as_ref())
214                    .map(u64::from_be_bytes)
215                    .map_err(|_wrong_length| {
216                        InvalidSnapshotError::ValueError(
217                            "override_block_timestamp attribute must be an 8-byte big-endian u64"
218                                .to_string(),
219                        )
220                    })
221            })
222            .transpose()?;
223        let block_overrides = if block_number.is_some() || block_timestamp.is_some() {
224            Some(BlockEnvOverrides { number: block_number, timestamp: block_timestamp })
225        } else {
226            None
227        };
228        let mut pool_state_builder =
229            EVMPoolStateBuilder::new(id.clone(), tokens.clone(), adapter_contract_address)
230                .balances(component_balances)
231                .disable_overwrite_tokens(potential_rebase_tokens)
232                .self_contained_tokens(self_contained_tokens)
233                .account_balances(account_balances)
234                .adapter_contract_bytecode(adapter_bytecode)
235                .involved_contracts(involved_contracts)
236                .stateless_contracts(stateless_contracts)
237                .manual_updates(manual_updates)
238                .trace(vm_traces)
239                .block_overrides(block_overrides)
240                .spot_price_caller(spot_price_caller(protocol_name));
241
242        if let Some(balance_owner) = balance_owner {
243            pool_state_builder = pool_state_builder.balance_owner(balance_owner)
244        };
245
246        let mut pool_state = pool_state_builder
247            .build(SHARED_TYCHO_DB.clone())
248            .await
249            .map_err(InvalidSnapshotError::VMError)?;
250
251        if let Some(receiver) = decoder_context.live_override.clone() {
252            pool_state.set_live_overrides(receiver);
253        }
254
255        pool_state.set_spot_prices(all_tokens)?;
256
257        Ok(pool_state)
258    }
259}
260
261/// The caller (`tx.origin`) for a protocol's adapter `price()` query.
262///
263/// Balancer V3's price path calls `querySwapExactIn`, which reverts with `NotStaticCall()` unless
264/// `tx.origin == address(0)` (its off-chain query-mode check). Every other adapter uses the
265/// default `EXTERNAL_ACCOUNT` caller (`None`).
266fn spot_price_caller(protocol_name: &str) -> Option<Address> {
267    (protocol_name == "balancer_v3").then_some(Address::ZERO)
268}
269
270#[cfg(test)]
271mod tests {
272    use std::{collections::HashSet, fs, path::Path};
273
274    use chrono::DateTime;
275    use revm::{primitives::KECCAK_EMPTY, state::AccountInfo};
276    use serde_json::Value;
277    use tycho_common::models::{
278        protocol::{ProtocolComponent, ProtocolComponentState},
279        Chain, ChangeType,
280    };
281
282    use super::*;
283    use crate::evm::{
284        engine_db::{create_engine, engine_db_interface::EngineDatabaseInterface},
285        protocol::vm::constants::{BALANCER_V2, CURVE},
286        tycho_models::AccountUpdate,
287    };
288
289    #[test]
290    fn test_to_adapter_file_name() {
291        assert_eq!(get_adapter_file("balancer_v2").unwrap(), BALANCER_V2);
292        assert_eq!(get_adapter_file("curve").unwrap(), CURVE);
293    }
294
295    fn vm_component() -> ProtocolComponent {
296        let creation_time = DateTime::from_timestamp(1622526000, 0)
297            .unwrap()
298            .naive_utc(); //Sample timestamp
299
300        let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
301        static_attributes.insert("manual_updates".to_string(), Bytes::from_str("0x01").unwrap());
302
303        let dai_addr = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
304        let bal_addr = Bytes::from_str("0xba100000625a3754423978a60c9317c58a424e3d").unwrap();
305        let tokens = vec![dai_addr, bal_addr];
306
307        ProtocolComponent {
308            id: "0x4626d81b3a1711beb79f4cecff2413886d461677000200000000000000000011".to_string(),
309            protocol_system: "vm:balancer_v2".to_string(),
310            protocol_type_name: "balancer_v2_pool".to_string(),
311            chain: Chain::Ethereum,
312            tokens,
313            contract_addresses: vec![
314                Bytes::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap()
315            ],
316            static_attributes,
317            change: ChangeType::Creation,
318            creation_tx: Bytes::from_str("0x0000").unwrap(),
319            created_at: creation_time,
320        }
321    }
322
323    fn load_balancer_account_data() -> Vec<AccountUpdate> {
324        let project_root = env!("CARGO_MANIFEST_DIR");
325        let asset_path =
326            Path::new(project_root).join("tests/assets/decoder/balancer_v2_snapshot.json");
327        let json_data = fs::read_to_string(asset_path).expect("Failed to read test asset");
328        let data: Value = serde_json::from_str(&json_data).expect("Failed to parse JSON");
329
330        let accounts: Vec<AccountUpdate> = serde_json::from_value(data["accounts"].clone())
331            .expect("Expected accounts to match AccountUpdate structure");
332        accounts
333    }
334
335    #[tokio::test]
336    async fn test_try_from_with_header() {
337        let attributes: HashMap<String, Bytes> = vec![
338            (
339                "balance_owner".to_string(),
340                Bytes::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap(),
341            ),
342            ("override_block_number".to_string(), Bytes::from(123_u64.to_be_bytes().to_vec())),
343            ("override_block_timestamp".to_string(), Bytes::from(456_u64.to_be_bytes().to_vec())),
344            ("reserve1".to_string(), Bytes::from(200_u64.to_le_bytes().to_vec())),
345        ]
346        .into_iter()
347        .collect();
348        let tokens = [
349            Token::new(
350                &Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(),
351                "DAI",
352                18,
353                0,
354                &[Some(10_000)],
355                tycho_common::models::Chain::Ethereum,
356                100,
357            ),
358            Token::new(
359                &Bytes::from_str("0xba100000625a3754423978a60c9317c58a424e3d").unwrap(),
360                "BAL",
361                18,
362                0,
363                &[Some(10_000)],
364                tycho_common::models::Chain::Ethereum,
365                100,
366            ),
367        ]
368        .into_iter()
369        .map(|t| (t.address.clone(), t))
370        .collect::<HashMap<_, _>>();
371        let snapshot = ComponentWithState {
372            state: ProtocolComponentState {
373                component_id: "0x4626d81b3a1711beb79f4cecff2413886d461677000200000000000000000011"
374                    .to_owned(),
375                attributes,
376                balances: HashMap::new(),
377            },
378            component: vm_component(),
379            component_tvl: None,
380            entrypoints: Vec::new(),
381        };
382        // Initialize engine with balancer storage
383        let block = BlockHeader::default();
384        let accounts = load_balancer_account_data();
385        let db = SHARED_TYCHO_DB.clone();
386        let engine = create_engine(db.clone(), false).unwrap();
387        for account in accounts.clone() {
388            engine
389                .state
390                .init_account(
391                    account.address,
392                    AccountInfo {
393                        balance: account.balance.unwrap_or_default(),
394                        nonce: 0u64,
395                        code_hash: KECCAK_EMPTY,
396                        code: account
397                            .code
398                            .clone()
399                            .map(|arg0: Vec<u8>| Bytecode::new_raw(arg0.into())),
400                    },
401                    None,
402                    false,
403                )
404                .expect("Failed to init account");
405        }
406        db.update(accounts, Some(block.clone()))
407            .unwrap();
408        let account_balances = HashMap::from([(
409            Bytes::from("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
410            HashMap::from([
411                (
412                    Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"),
413                    Bytes::from(100_u64.to_le_bytes().to_vec()),
414                ),
415                (
416                    Bytes::from("0xba100000625a3754423978a60c9317c58a424e3d"),
417                    Bytes::from(100_u64.to_le_bytes().to_vec()),
418                ),
419            ]),
420        )]);
421
422        let decoder_context = DecoderContext::new();
423        let res = EVMPoolState::try_from_with_header(
424            snapshot,
425            block,
426            &account_balances,
427            &tokens,
428            &decoder_context,
429        )
430        .await
431        .unwrap();
432
433        let res_pool = res;
434
435        assert_eq!(
436            res_pool.get_balance_owner(),
437            Some(Address::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap())
438        );
439        let mut exp_involved_contracts = HashSet::new();
440        exp_involved_contracts
441            .insert(Address::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap());
442        assert_eq!(res_pool.get_involved_contracts(), exp_involved_contracts);
443        assert!(res_pool.get_manual_updates());
444        // vm_component() is vm:balancer_v2, so the price query uses the default caller.
445        assert_eq!(res_pool.get_spot_price_caller(), None);
446        assert_eq!(
447            res_pool.get_block_overrides(),
448            Some(BlockEnvOverrides { number: Some(123), timestamp: Some(456) })
449        );
450    }
451
452    #[test]
453    fn test_spot_price_caller() {
454        // Balancer V3 must query with tx.origin == address(0); everything else uses the default.
455        assert_eq!(spot_price_caller("balancer_v3"), Some(Address::ZERO));
456        assert_eq!(spot_price_caller("balancer_v2"), None);
457        assert_eq!(spot_price_caller("curve"), None);
458    }
459}