ERC-6823 · Draft · ERC · 2023-03-29
ERC-6823: Token Mapping Slot Retrieval Extension
A draft one-function extension, getTokenLocationRoot(), that tells off-chain tools which storage slot holds a token's balance or ownership mapping, so simulations can read state instead of trusting events.
What problem does it solve?
Wallets and security tools simulate a transaction before you sign it to show "you will send 100 USDC, receive NFT #42". Today those previews mostly rely on events, because events have a standard shape. But events are just logs that a contract chooses to emit; a buggy or malicious contract can emit a Transfer that does not match what really happened. The real source of truth is storage.
Reading storage generically is hard. A Solidity mapping such as balanceOf sits at a base slot p that depends on how many variables were declared before it, which differs for every implementation. The value for key k lives at keccak256(h(k) . p), so without knowing p a tool cannot find, say, an account's balance in an arbitrary ERC-20.
ERC-6823 asks token contracts to simply publish p.
How it works
ERC-20, ERC-721 and ERC-1155 contracts add one function:
function getTokenLocationRoot() external pure returns (bytes32 slot);
It returns the storage slot of the mapping that tracks ownership (balances for ERC-20/1155, owners for ERC-721), typically implemented with inline assembly slot := _balances.slot. Because it is pure, calling it costs nothing for off-chain callers and adds only a little deployment bytecode.
A simulator can then:
- Call
getTokenLocationRoot()to getp. - Compute the slot for the key it cares about.
- Read that slot before and after a simulated transaction (
eth_getStorageAt, or a state diff from a tracer) and compare it with what the events claim.
The function name is intentionally the same for all three standards, so one selector works for any compliant token.
Sample code
Contract side:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
contract SimpleToken {
string public name = "Simple";
mapping(address => uint256) public balanceOf; // the ownership mapping
function getTokenLocationRoot() external pure returns (bytes32 slot) {
assembly { slot := balanceOf.slot }
}
}
Reading a balance straight from storage (ethers v6):
import { AbiCoder, Contract, JsonRpcProvider, keccak256 } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const token = new Contract(tokenAddr, ["function getTokenLocationRoot() pure returns (bytes32)"], provider);
const root: string = await token.getTokenLocationRoot();
// Solidity layout for mapping(address => uint256): keccak256(abi.encode(key, slot))
const slot = keccak256(AbiCoder.defaultAbiCoder().encode(["address", "bytes32"], [holder, root]));
const raw = await provider.getStorage(tokenAddr, slot);
console.log("balance from storage:", BigInt(raw));
Things to know
- Status is Draft.
- It only reveals the base slot. Callers still need to know how keys are hashed: ERC-1155 balances are usually a nested mapping (
id => account => amount), which requires hashing twice, and Vyper or packed layouts may differ from the Solidity rule above. - Behind a proxy (ERC-1967), storage belongs to the proxy, so read the slot at the proxy's address, not the implementation's.
- A malicious contract could return a wrong slot, so tools should treat it as a hint and cross-check.