Nft Smart Contract Using Solidity

Photo by PiggyBank on Unsplash

Nft Smart Contract Using Solidity

So today I will be writing about how you can create an Erc721 smart contract using solidity. Firstly what is ERC721? ERC721 Is the standard for representing ownership of non-fungible tokens which is why they are unique, the ERC721 is a bit more complex/standard than ERC20.

What is a Non-Fungible Token?

Non-fungible tokens (NFTs) are cryptographic assets that exist on a blockchain with their individual identification codes and metadata that distinguish them from each other. This is to say that every nft is unique characteristics.

We refer to as Erc721 as an open standard (open standards are, specifications: formal descriptions of software interfaces) that describes how to build Non-fungible token contracts on Ethereum virtual machine (EVM) compatible blockchains. y The functions of a non-fungible token smart contract:

name

... is used to refer to the name of the token. This name can be called by other contracts and applications.

symbol

... is used to refer to the symbol or to represent the name of the token.

balanceOf

... this is a call function that returns the number of NFTs an address owns.

totalSupply

... is a call function that returns the total number of tokens on the blockchain.

###ownerOf ... is a call function that returns the address of the owner of the token(after passing in the tokenId of that nft). Every nft is unique as such is represented on the blockchain with an id.

approve

... is a function that gives the power to an entity to transfer the token on behalf of the owner.

transfer

... is a function that enables the owner to transfer his/her Nfts to another person.

tokenMetaData

... it provides us with an interface to locate the information pertaining to that particular token.

Steps To Creating And Deploying An Erc721 Contracts

I guess we are a bit familiar with the concept of non-fungible tokens and Erc721 tokens, so it's time we dive into creating and deploying your Erc721 contract.

  1. Getting Test Ether: We'd use the Goerli Testnet Blockchain to deploy our contract. You will need the Metamask extension for chrome. Create a wallet copy the address of your newly created wallet, then visit goerlifaucet.com and paste your wallet address to receive 0.2 Ether.

Screenshot 2022-09-30 at 13.09.44.png

  1. Adding Your Files To Cloud: There are several web3 cloud sources that we can use for uploading our NFT artwork, IPFS, and pinta. For the sake of this lesson, I would be using the pinta cloud. I would be hosting the NFT art picture on pinta, and hosting the metadata on jsonkeeper.com.

Next, create an account with pinata.cloud and sign in, and upload your NFT artwork on pinta.

Screenshot 2022-09-30 at 13.37.58.png To be sure it was hosted perfectly copy the CID from pinta and ipfs.io/ipfs/CID. Replace the CID with the CID you copied from pinta.

So the next thing is to host our tokenMetaData, for this purpose I would be using jsonkeeper.com. As described earlier a tokenMetaData gives more information about our NFT. Here is what I mean:

{
"name": "Kai Havertz",
"description":  "King Kai is my best Chelsea player",
"image": "the URL"
}

Host it on jsonkeeper.com and you'd get a link for your metadata. Save that link as it would be used later when you want to mint your NFT.

  1. Creating An NFT: For this contract, we would not write the Erc721 interface this is because I would be using the ERC-721 contract for a smooth process. Yeah, I would import the contract so I can use their functions and library.

Open up your remix and create a file with an extension of .sol

The first step is the license of our contract.

// SPDX-License-Identifier: MIT

Next, declare your solidity version.

pragma solidity ^0.8.0;

Next, I would be importing the Erc721 contracts so we don't have to write all of this library from the beginning. Here is how I would do it.

import "github.com/0xcert/ethereum-erc721/src/contr.."

import "github.com/0xcert/ethereum-erc721/src/contr.."

Next, is to create the contract structure the name of my contract is going to be havertzNft and this contract is going to inherit properties from our imported contracts.

contract havertzNft is NFTokenMetadata, Ownable { }

Next, is to give a name and symbol to our nft contract.

constructor () { nftName = "Kai Havertz Nft"; nftSymbol = "Kai; }

Next, is the mint function this declares the function mint with three arguments, variable _uri of type string, the method of storing the URI of the JSON file, variable _to of type address which can help in storing the address of the receiver of NFT token, variable _tokenId of uint256 type which can hold the token id.

function mint(address _to, uint256 _tokenId, string calldata _uri).

Next, gives the command of token minting with the assistance of the token id and receiver’s address. super._mint(_to, _tokenId);

Next, states about fixing Token URI with the assistance of URI of JSON file and token id. super._setTokenUri(_tokenId, _uri);

This is what our contract is going to look like:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

contract havertzNft is NFTokenMetadata, Ownable {

  constructor() {
    nftName = "Kai Havertz Nft";
    nftSymbol = "Kai";
  }

  function mint(address _to, uint256 _tokenId, string calldata _uri) external onlyOwner {
    super._mint(_to, _tokenId);
    super._setTokenUri(_tokenId, _uri);
  }

}

```

With the help of injected Web3, deploy the compiled smart contract then approve the transaction from the metamask extension.

You are now good to go with deploying your nft smart contract, click on the deploy button. Toggle the deployed contract on remix and you would see a list of functions and method you can interact with.