Problem: Collecting ERC20 token holders with some balance (the bigger, the better)
Solution Detect index of the 'balances' mapping slot to change the balance for some user and use the tokens
Solidity documentation explains how the mappings are stored in a contract storage.
Formula for getting holder's slot hash:
v[key] = keccak256(uint256(key) . uint256(v's slot index))
- Find the slot containing the mapping with balances (brute force).
- Calculate the hash of our holder's slot in mapping.
- Change the balance with using
setStorageAt
method.
Script for searching storage slot containing balances mappnig in script/findSlot.ts
file.
Folder contracts
contains custom mock ERC20 token implementations for testing.
There are three mock tokens:
- balances mapping in zero slot
- balancs mapping in second slot
- balances mapping in slot thirteen
Run npx hardhat test
to run tests on multiple mock contracts