tycho_simulation/evm/mod.rs
1use alloy::primitives::U256;
2use tycho_common::keccak256;
3
4pub mod account_storage;
5pub mod decoder;
6pub mod engine_db;
7pub mod protocol;
8pub mod query_pool_swap;
9pub mod simulation;
10pub mod stream;
11pub mod traces;
12pub mod tycho_models;
13
14pub type SlotId = U256;
15/// Enum representing the type of contract compiler.
16#[derive(Debug, PartialEq, Copy, Clone)]
17pub enum ContractCompiler {
18 Solidity,
19 Vyper,
20}
21
22impl ContractCompiler {
23 /// Computes the storage slot for a given mapping based on the base storage slot of the map and
24 /// the key.
25 ///
26 /// # Arguments
27 ///
28 /// * `map_base_slot` - A byte slice representing the base storage slot of the mapping.
29 /// * `key` - A byte slice representing the key for which the storage slot is being computed.
30 ///
31 /// # Returns
32 ///
33 /// A `SlotId` representing the computed storage slot.
34 ///
35 /// # Notes
36 ///
37 /// - For `Solidity`, the slot is computed as `keccak256(key + map_base_slot)`.
38 /// - For `Vyper`, the slot is computed as `keccak256(map_base_slot + key)`.
39 pub fn compute_map_slot(&self, map_base_slot: &[u8], key: &[u8]) -> SlotId {
40 let concatenated = match &self {
41 ContractCompiler::Solidity => [key, map_base_slot].concat(),
42 ContractCompiler::Vyper => [map_base_slot, key].concat(),
43 };
44
45 let slot_bytes = keccak256(&concatenated);
46
47 SlotId::from_be_slice(&slot_bytes)
48 }
49}