The tokens and NFTs are not actually in your wallet

You mint or buy things, and they show up in your wallet — you see them in MetaMask, Rainbow, whatever you have. "Wallet" is a useful concept when explaining web3 to people.

But if you look under the hood, it's not at all how it works.

Your wallet is just an address: a 160-bit number that looks like 0x123a…c8f9. It doesn't really store anything.

The things you have "in your wallet" are actually defined by the contracts.

Each NFT or token you interact with is a contract — a piece of code on Ethereum blockchain. To be considered an NFT or a token, the code has to implement a bunch of standard functions described in the corresponding standard (e.g. ERC721 or ERC20).

For example, ERC20 token standard has a function called balanceOf. The contract baking the token is free to implement this method in any way it wants. Most methods store the balance inside the contract storage as a map from address to balance. Some tokens implement inflationary balance, where the balance reported is your original balance multiplied by the amount of time you held it for.

The wallet software just calls these functions and shows the results in the UI. So does OpenSea, UniSwap and any other web3 platform.

Try it yourself! Here's contract address for WETH token. You can open its Contract tab on Etherscan, find the balanceOf method and call it with any Ethereum wallet address:

The reported number needs to be adjusted 18 decimal points (based on token's decimals)
The reported number needs to be adjusted 18 decimal points (based on token's decimals)

You can also look at WETH's source code and see that indeed it stores the balance inside contract storage (and not in your wallet)

msg.sender is the address of the wallet that called these functions
msg.sender is the address of the wallet that called these functions

How do wallets know which contracts to call?

The simplest way to do that would be to ask the user to manually add every coin or NFT they have. It’s not very popular because it’s a terrible user experience and goes against the “wallet” concept.

Alternatively, the wallet software could call balanceOf on every contract ever deployed to Ethereum. That’s very costly and slow. This could also show millions of spammy tokens.

So “show me the coins I have in my wallet” is actually a non-trivial problem that is not solved by Ethereum out-of-the-box. A lot of third-party services provide a layer on top of Ethereum that aggregates tokens and NFTs “held” by a given wallet address (e.g. zapper.fi)

Zapper is trying to show what Vitalik "has in his wallet"
Zapper is trying to show what Vitalik "has in his wallet"
Subscribe to w1nt3r.eth
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.