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

Nested mappings explained

Apply the mapping derivation once for each key layer.

LET’S PICTURE IT

A directory lookup can lead to another directory that needs a second lookup name.

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.

Read the key order

For mapping(address => mapping(address => uint256)), the outer address selects one inner mapping. Reversing owner and spender selects a different logical entry.

Derive the outer base

Combine the properly padded outer address with the declared slot p and hash them. The result becomes the base used for the selected inner mapping.

Derive the inner location

Combine the padded inner address with the derived outer base and hash again. This second result identifies the uint256 value slot for that ordered key pair.

Do not flatten both keys casually

Hashing two addresses and p together once is not the standard nested mapping formula. Preserve the two stages and each intermediate base.

Inspect the layout

outer = keccak256(abi.encode(owner, p))
entry = keccak256(abi.encode(spender, outer))

Both address keys use full padded words; the base values are uint256-sized words.

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

How many mapping hash stages are needed for two nested 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.