TP Ethereum Blockchain - NFT

Non-fungible tokens (NFTs) are cryptographically unique tokens that are linked to digital (and sometimes physical) content, providing are linked to digital (and sometimes physical) content, providing proof of ownership. They are used in many cases, notably for works of art, collectibles art, digital collectibles, music and video game assets.

In this lab we propose to develop a simple smart contract respecting the ERC-721 standard that we will then deploy on the test blockchain Sepolia.

This smart contract will allow us to store a URI pointing to a description file in JSON format, ideally stored in a distributed file system (ex: IPFS). Note that this description file file will also contain a URI pointing to your digital artwork that you have previously created.

Finally, since we are going to respect some de facto standards you will be able to exhibit and sell your work on the well known NFT's marketplace OpenSea.

CryptoPunk

Preliminary step

Before doing this TP we are going to ask you to create a small piece of art as a png or jpeg image that you will upload to IPFS (Interplanetary File System). IPFS is a distributed system for storing and accessing files, websites, applications and data. It is ideally suited for storage and addressing of data for NFTs.

  1. Create a piece of art! :-)
  2. Upload your master piece on a http(s) server. Alternatively you may install IPFS on your computer (IPFS CLI) and add your work on IPFS (ex: ipfs add myPieceOfArt.jpg). Save the CID (hash id). Your work should be available via the gateway ipfs (https://ipfs.io/ipfs/QmVzeepjhqRz3...)

If you have problems installing or using IPFS here are some CIDs

  • QmSgvgwxZGaBLqkGyWemEDqikCqU52XxsYLKtdy3vGZ8uq
  • QmaRwFBUjYhVGjP3XKxeZyNuS6X9u2uhMk9oguP6Za1jx2

The ERC-721 smart contract

In order to simplify our lives and maximize our chances of deploying a well written smart contract we will rely on the audited smart contract from Openzeppelin.

Here are some indications:

  1. Your smart contract should import from Openzeppelin the contract ERC721URIStorage.sol (Note: this one inherits from ERC721.sol)
  2. your contract can then inherit from ERC721URIStorage (ex: contract PieceOfArtCollectible is ERC721URIStorage).
  3. Then declare a public attribute of type uint256 named tokenCounter.
  4. Your constructor will call the ERC721 constructor. Pass it as a name and a string symbol (e.g. constructor()constructor() ERC721("PieceOfArt", "POA")`).
  5. Create a createCollectible function that takes as parameter a string for the URI of the token (ex: function createCollectible(string tokenURI)) and which returns an uint256 which is the index of the last created token. In this function,
  6. declare an uint256 newTokenId initialized to tokenCounter.
  7. call the _safeMint(...) function, then the _setTokenURI(...) function
  8. increment tokenCounter.
  9. return newTokenId (ex: return newTokenId;)

Now your contract is ready. Once you have tested it locally, deploy it to Goerli.

Description file (meta data)

An NFT can only be represented by a unique identifier (token_id) of an ERC-721 contract. In practice we can also associate to this identifier meta data that will provide additional properties to our NFT like a name for example. These metadata can then be exploited by third party applications.

We propose to create a description file in JSON format. Name it with the extension .json (ex: NFT.json) If you respect this format the Opensea marketplace should be able to render it in its user interface. To associate more Meta data to your NFT you can consult this page.

Example of a JSON file:

{
    "name": "POA",
    "description": "A short description of my piece of art...",
    "image": "https://ipfs.io/ipfs/Qm...?filename=image.png",
    "attributes": [
        {
            "trait_type": "Beauty",
            "value": 100
        }
    ]
}

This json file has to be deployed on a web server (or IPFS), then its URL has to be passed as a parameter to the Smart contract's createCollectible() function.

Final step

Now the Smart contract is deployed on Sepolia. The .json file of description and the NFT image are accessible at their respective URLs (on a web server or preferably on a distributed p2p file system like IPFS).

You can go to the OpenSea testnet: https://testnets.opensea.io/ followed by your ethereum account address (see Metamask). Ex: https://testnets.opensea.io/0x52Bf31... You should be able to see all the NFTs associated with your address.

Alternatively, you can view a particular NFT associated with your ERC-721 contract by passing in the URL https://testnets.opensea.io/assets/sepolia/ the address of the contract followed by the token_id. Ex: https://testnets.opensea.io/assets/0x287cb.../0 here the token_id is 0.