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