A lightweight, dependency-free utility to find and utilize ERC20 token storage slots for balance mappings.
This utility provides an efficient method to find the storage slot used by ERC20 token's balanceOf mapping for a given address. It requires only a single RPC call and works with both standard and proxied ERC20 contracts (like USDC). The approach can be used for a variety of simulation purposes (e.g. transfer erc20 without owning or acquiring it) and can be adapted for other storage slots (allowance, etc.).
The script uses a simple but powerful approach:
- Performs a
debug_traceCallon the token'sbalanceOffunction - Extracts the storage slot from the EVM execution trace
- Demonstrates how to use this slot with
eth_callstate overrides
- An Ethereum provider with the
debugRPC module enabled - Bun or another JavaScript runtime
To access the debug_traceCall method, you have several options:
- Run your own node with debug flag enabled (e.g.,
--http.api eth,debug) - Find a public provider with debug methods enabled on chainlist.org (can be challenging)
- Create an account with a service provider like QuickNode and use their free trial
-
Configure the variables at the top of the script:
providerUrl: Your Ethereum provider URLtoken: The ERC20 token addressaddressToTop: The address to check balance for
-
Run the script:
bun index.ts
MIT