kabu_evm_utils/
remv_db_direct_access.rs1use alloy::primitives::{keccak256, Address, U256};
2use eyre::{eyre, Result};
3use revm::DatabaseRef;
4use tracing::debug;
5
6pub fn calc_hashmap_cell<U0: Into<U256>, U1: Into<U256>>(offset: U0, cell: U1) -> U256 {
7 let offset: U256 = offset.into();
8 let cell: U256 = cell.into();
9 let mut buf: Vec<u8> = Vec::new();
10 buf.extend(cell.to_be_bytes_vec());
11 buf.extend(offset.to_be_bytes_vec());
12 debug!("Reading cell : {} {} {:?}", offset, cell, buf);
13
14 keccak256(buf).into()
15}
16
17pub fn try_read_cell<DB: DatabaseRef>(db: &DB, account: &Address, cell: &U256) -> Result<U256> {
18 db.storage_ref(*account, *cell).map_err(|_| eyre!("READ_CELL_FAILED"))
19}
20
21pub fn try_read_hashmap_cell<DB: DatabaseRef>(db: &DB, account: &Address, hashmap_offset: &U256, item: &U256) -> Result<U256> {
22 let mut buf = item.to_be_bytes::<32>().to_vec();
23 buf.append(&mut hashmap_offset.to_be_bytes::<32>().to_vec());
24 let cell: U256 = keccak256(buf.as_slice()).into();
25 db.storage_ref(*account, cell).map_err(|_| eyre!("READ_HASHMAP_CELL_ERROR"))
26}