foundry_common/utils.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//! Uncategorised utilities.
use alloy_primitives::{keccak256, B256, U256};
/// Block on a future using the current tokio runtime on the current thread.
pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
block_on_handle(&tokio::runtime::Handle::current(), future)
}
/// Block on a future using the current tokio runtime on the current thread with the given handle.
pub fn block_on_handle<F: std::future::Future>(
handle: &tokio::runtime::Handle,
future: F,
) -> F::Output {
tokio::task::block_in_place(|| handle.block_on(future))
}
/// Computes the storage slot as specified by `ERC-7201`, using the `erc7201` formula ID.
///
/// This is defined as:
///
/// ```text
/// erc7201(id: string) = keccak256(keccak256(id) - 1) & ~0xff
/// ```
///
/// # Examples
///
/// ```
/// use alloy_primitives::b256;
/// use foundry_common::erc7201;
///
/// assert_eq!(
/// erc7201("example.main"),
/// b256!("183a6125c38840424c4a85fa12bab2ab606c4b6d0e7cc73c0c06ba5300eab500"),
/// );
/// ```
pub fn erc7201(id: &str) -> B256 {
let x = U256::from_be_bytes(keccak256(id).0) - U256::from(1);
keccak256(x.to_be_bytes::<32>()) & B256::from(!U256::from(0xff))
}