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
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.The first sixteen-byte value occupies the low-order half of slot zero. Low-order bytes are shown on the right.The second uint128 fits in the remaining sixteen bytes. Both values now share slot zero.C needs a complete thirty-two-byte slot, so it begins at slot one. Two packed small values plus C use two slots.
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.