Vyper ERC-721 Contract Walkthrough
The ERC-721 standard is used to hold the ownership of Non-Fungible Tokens (NFT). ERC-20 tokens behave as a commodity, because there is no difference between individual tokens. In contrast to that, ERC-721 tokens are designed for assets that are similar but not identical, such as different cat cartoons or titles to different pieces of real estate.
In this article we will analyze Ryuya Nakamura's ERC-721 contract. This contract is written in Vyper, a Python-like contract language designed to make it harder to write insecure code than it is in Solidity.
1# @dev Implementation of ERC-721 non-fungible token standard.2# @author Ryuya Nakamura (@nrryuya)3# Modified from: https://github.com/vyperlang/vyper/blob/de74722bf2d8718cca46902be165f9fe0e3641dd/examples/tokens/ERC721.vy4പകര്പ്പ്
Comments in Vyper, as in Python, start with a hash (
#) and continue to the end of the line. Comments that include
@<keyword> are used by NatSpec to produce human-readable
1from vyper.interfaces import ERC72123implements: ERC7214പകര്പ്പ്
The ERC-721 interface is built into the Vyper language. You can see the code definition here. The interface definition is written in Python, rather than Vyper, because interfaces are used not only within the blockchain, but also when sending the blockchain a transaction from an external client, which may be written in Python.
The first line imports the interface, and the second specifies that we are implementing it here.
1# Interface for the contract called by safeTransferFrom()2interface ERC721Receiver:3 def onERC721Received(4പകര്പ്പ്
ERC-721 supports two types of transfer:
transferFrom, which lets the sender specify any destination address and places the responsibility for the transfer on the sender. This means that you can transfer to an invalid address, in which case the NFT is lost for good.
safeTransferFrom, which checks if the destination address is a contract. If so, the ERC-721 contract asks the receiving contract if it wants to receive the NFT.
safeTransferFrom requests a receiving contract has to implement
1 _operator: address,2 _from: address,3പകര്പ്പ്
_from address is the current owner of the token. The
_operator address is the one that
requested the transfer (those two may not be the same, because of allowances).
1 _tokenId: uint256,2