Vyper theorytheory 0/50 · 0%
Web3 integration · medium

32. Building an ERC-721 token

NFT ownership tracking in Vyper.

An ERC-721 tracks a mapping from token id to owner and a mapping from owner to balance, plus `Transfer` and `Approval` events matching the standard's signatures.

owner_of: public(HashMap[uint256, address])
balance_of: public(HashMap[address, uint256])

@external
def mint(to: address, token_id: uint256):
    assert self.owner_of[token_id] == empty(address), "already minted"
    self.owner_of[token_id] = to
    self.balance_of[to] += 1
    log Transfer(sender=empty(address), receiver=to, token_id=token_id)

Full standard compliance also needs `safeTransferFrom` with a receiver-contract check, and `supportsInterface` (ERC-165) so wallets and marketplaces can detect the standard.

Check your understanding

  1. 1. What does owner_of[token_id] track?

  2. 2. Which extra check does full ERC-721 compliance require for transfers to contracts?

  3. 3. Which standard lets wallets detect ERC-721 support?