In this track: Storage, memory and layout
advanced · 4 MIN READ

Mapping entry slot calculations

Hash the properly encoded key with the declared mapping slot.

LET’S PICTURE IT

A directory combines the lookup name with the directory identifier to select one drawer.

SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
Pack small values into Solidity storage slotsFirstuint128 A16 bytesSeconduint128 B16 bytesThirduint256 C32 bytesSimple elementary types only; not a complete compiler layoutSlot 0 · 32 bytesUnused · 16 BA · 16 BHigh-order bytes ← → low-order bytesSlot 0 · fullB · 16 BA · 16 BDeclaration order and sizes determine this packingSlot 0BASlot 1C · 32 bytesTry different sequences in the storage-layout tool

In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Start from the declaration

A mapping reserves a declared slot p used in deriving entry locations. That slot does not store an enumerable list of keys or a mapping length.

Encode the key under its type

For an address key, use its padded thirty-two-byte representation, then the thirty-two-byte slot number. Do not concatenate a twenty-byte address with a word and assume the same result.

Hash the combined input

For mapping(address => uint256), Keccak-256 of those two words identifies the entry’s slot. Other key types can require different key encoding rules.

Keep network state separate

A local slot calculation identifies a location under a supplied layout. It does not read that location, confirm the implementation or prove a live balance.

Inspect the layout

mapping(address => uint256) balances;
suppose p = 5, key = address(1)
slot = keccak256(abi.encode(key, uint256(5)))
slot = 0x1471eb6eb2c5e789fc3de43f8ce62938c7d1836ec861730447e2ada8fd81017b

This local example uses an address key and uint256 value; string keys have different key preprocessing.

TURN UNDERSTANDING INTO PRACTICEOpen the Storage layout →
QUICK CHECK · NO PRESSURE

Does a mapping base slot contain a list of keys?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.