Skip to main content

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