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