Struct StorageSlotFinder

Source
pub struct StorageSlotFinder<P, N>
where N: Network,
{ /* private fields */ }
Expand description

A utility for finding storage slots in smart contracts, particularly useful for ERC20 tokens.

This struct helps identify which storage slot contains a specific value by:

  1. Creating an access list to find all storage slots accessed by a function call
  2. Systematically overriding each slot with an expected value
  3. Checking if the function returns the expected value to identify the correct slot

§Example

use alloy_contract::StorageSlotFinder;
use alloy_primitives::{address, U256};
use alloy_provider::ProviderBuilder;

let provider = ProviderBuilder::new().connect_anvil();
let token = address!("0x6B175474E89094C44Da98b954EedeAC495271d0F");
let user = address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

// Find the storage slot for a user's balance
let finder =
    StorageSlotFinder::balance_of(provider, token, user).with_expected_value(U256::from(1000));

if let Some(slot) = finder.find_slot().await? {
    println!("Balance stored at slot: {:?}", slot);
}

Implementations§

Source§

impl<P, N> StorageSlotFinder<P, N>
where P: Provider<N>, N: Network,

Source

pub const fn new( provider: P, contract: Address, calldata: Bytes, expected_value: U256, ) -> Self

Creates a new storage slot finder for a generic function call.

§Arguments
  • provider - The provider to use for making calls
  • contract - The address of the contract to analyze
  • calldata - The encoded function call to execute
  • expected_value - The value we expect the function to return

For common ERC20 use cases, consider using Self::balance_of instead.

Source

pub fn balance_of(provider: P, token_address: Address, user: Address) -> Self

Convenience constructor for finding the storage slot of an ERC20 balanceOf(address) mapping.

Uses a default expected value of 1337. Call Self::with_expected_value to set a different value.

§Arguments
  • provider - The provider to use for making calls
  • token_address - The address of the ERC20 token contract
  • user - The address of the user whose balance slot we’re finding
Source

pub const fn with_expected_value(self, value: U256) -> Self

Configures a specific value that should be used in the state override to identify the slot.

Source

pub async fn find_slot(self) -> Result<Option<B256>, TransportError>

Finds the storage slot containing the expected value.

This method:

  1. Creates an access list for the function call to identify all storage slots accessed
  2. Iterates through each accessed slot on the target contract
  3. Overrides each slot with the expected value using state overrides
  4. Checks if the function returns the expected value when that slot is overridden
  5. Returns the first slot that causes the function to return the expected value
§Returns
  • Ok(Some(slot)) - The storage slot that contains the value
  • Ok(None) - No storage slot was found containing the value
  • Err(TransportError) - An error occurred during RPC calls
§Note

This method assumes that the value is stored directly in a storage slot without any encoding or hashing. For mappings, the actual storage location might be computed using keccak256 hashing.

Trait Implementations§

Source§

impl<P: Clone, N> Clone for StorageSlotFinder<P, N>
where N: Network + Clone,

Source§

fn clone(&self) -> StorageSlotFinder<P, N>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug, N> Debug for StorageSlotFinder<P, N>
where N: Network + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, N> !Freeze for StorageSlotFinder<P, N>

§

impl<P, N> RefUnwindSafe for StorageSlotFinder<P, N>

§

impl<P, N> Send for StorageSlotFinder<P, N>
where P: Send,

§

impl<P, N> Sync for StorageSlotFinder<P, N>
where P: Sync,

§

impl<P, N> Unpin for StorageSlotFinder<P, N>
where P: Unpin, N: Unpin,

§

impl<P, N> UnwindSafe for StorageSlotFinder<P, N>
where P: UnwindSafe, N: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,