pub struct StorageSlotFinder<P, N>where
N: Network,{ /* private fields */ }contract only.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:
- Creating an access list to find all storage slots accessed by a function call
- Systematically overriding each slot with an expected value
- 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>
impl<P, N> StorageSlotFinder<P, N>
Sourcepub fn new(
provider: P,
contract: Address,
calldata: Bytes,
expected_value: Uint<256, 4>,
) -> StorageSlotFinder<P, N>
pub fn new( provider: P, contract: Address, calldata: Bytes, expected_value: Uint<256, 4>, ) -> StorageSlotFinder<P, N>
Creates a new storage slot finder for a generic function call.
§Arguments
provider- The provider to use for making callscontract- The address of the contract to analyzecalldata- The encoded function call to executeexpected_value- The value we expect the function to return
For common ERC20 use cases, consider using Self::balance_of instead.
Sourcepub fn balance_of(
provider: P,
token_address: Address,
user: Address,
) -> StorageSlotFinder<P, N>
pub fn balance_of( provider: P, token_address: Address, user: Address, ) -> StorageSlotFinder<P, N>
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 callstoken_address- The address of the ERC20 token contractuser- The address of the user whose balance slot we’re finding
Sourcepub const fn with_expected_value(
self,
value: Uint<256, 4>,
) -> StorageSlotFinder<P, N>
pub const fn with_expected_value( self, value: Uint<256, 4>, ) -> StorageSlotFinder<P, N>
Configures a specific value that should be used in the state override to identify the slot.
Sourcepub fn with_request(
self,
base_request: <N as Network>::TransactionRequest,
) -> StorageSlotFinder<P, N>
pub fn with_request( self, base_request: <N as Network>::TransactionRequest, ) -> StorageSlotFinder<P, N>
Overrides the base request object that will be used for slot detection.
For slot detection the target address of that request is set to the configured contract and the input to the configured input.
Sourcepub async fn find_slot(
self,
) -> Result<Option<FixedBytes<32>>, RpcError<TransportErrorKind>>
pub async fn find_slot( self, ) -> Result<Option<FixedBytes<32>>, RpcError<TransportErrorKind>>
Finds the storage slot containing the expected value.
This method:
- Creates an access list for the function call to identify all storage slots accessed
- Iterates through each accessed slot on the target contract
- Overrides each slot with the expected value using state overrides
- Checks if the function returns the expected value when that slot is overridden
- Returns the first slot that causes the function to return the expected value
§Returns
Ok(Some(slot))- The storage slot that contains the valueOk(None)- No storage slot was found containing the valueErr(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, N> Clone for StorageSlotFinder<P, N>
impl<P, N> Clone for StorageSlotFinder<P, N>
Source§fn clone(&self) -> StorageSlotFinder<P, N>
fn clone(&self) -> StorageSlotFinder<P, N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto 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,
impl<P, N> UnwindSafe for StorageSlotFinder<P, N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
Source§fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
impl<T> ErasedDestructor for Twhere
T: 'static,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.