Assets, Ethereum

How Do You Code a Crypto Collectible ERC 721 NFT Tutorial Ethereum?

In this tutorial, we’re going to learn how to code a crypto collectible ERC 721 NFT on the Ethereum blockchain. We’ll first need to set up our development environment by installing the dependencies and setting up our project.

Then, we’ll need to code our smart contract in Solidity. Finally, we’ll deploy our contract to the Ethereum blockchain.

let’s get started!

Setting up our development environment

Before we can start coding our ERC 721 NFT, we need to set up our development environment. For this tutorial, we’ll be using the Truffle Framework and Ganache CLI.

First, let’s install the dependencies:

npm install -g truffle
npm install -g ganache-cli

Next, let’s create a new project directory and initialize Truffle:

mkdir erc721-nft-tutorial
cd erc721-nft-tutorial
truffle init

Now that our project is set up, we can start coding our smart contract!

Coding our smart contract

Open up the file `contracts/ERC721Token.sol` in your favorite text editor and let’s get started. First, we’ll need to import the `ERC721` library from the OpenZeppelin team:

import “openzeppelin-solidity/contracts/token/ERC721/ERC721Token.sol”;

Next, we’ll define our contract name and inherits from the `ERC721Token` contract:

contract MyERC721Token is ERC721Token { . } // define your contract name here! // ^– notice how we’re inheriting from `ERC721Token` here! . } // remember to close your contract with `}`! Now that we’ve defined our contract name and inherits from the `ERC721Token` contract, let’s add some properties: “`solidity string public name = “My ERC721 Token”; string public symbol = “MET”; uint8 public decimals = 18; “` These properties are optional, but they’re recommended so that your token can be easily identified by users. Now that we’ve defined our properties, let’s add some functions: “`solidity function mint(address _to, uint256 _tokenId) public onlyOwner { _mint(_to, _tokenId); emit Transfer(_ownerOf(_tokenId), _to, _tokenId); } “` This function will mint a new token and transfer it to the specified address. Notice how we’re using the `_mint()` function from the `ERC721Token` contract and emitting a `Transfer` event – these are both required for an ERC 721 token! “`solidity function burn(uint256 _tokenId) public onlyOwner { require(balanceOf(_ownerOf(_tokenId)) > 0); _burn(_tokenId); emit Transfer(_ownerOf(_tokenId), address(0), _tokenId); } “` This function will “burn” a token – that is, it will remove it from circulation. Notice how we’re using the `_burn()` function from the `ERC721Token` contract and emitting a `Transfer` event – these are both required for an ERC 721 token! That’s it for coding our smart contract! Now let’s deploy it to the Ethereum blockchain. Deploying our smart contract First, let’s start Ganache CLI in a new terminal window: ganache-cli –account=”0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3″ –account=”0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f” –account=”0x0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1″ –account=”0xc88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c” –account=”0x388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bedbc6af1″ –unlock 0 –unlock 1 –unlock 2 –unlock 3 –unlock 4 –networkid 1579442752152 First, you’ll need to unlock each of your accounts with the `–unlock` flag. The number after each flag corresponds to an account index – for example, if you have 10 accounts unlocked in Ganache GUI , then you would use `–unlock 0`, `–unlock 1`, etc., all the way up to `–unlock 9`. You can also specify which network you want Ganache CLI to connect to with the `–networkid` flag – for example, if you’re using mainnet , then you would use `–networkid 1`. If you’re using ropsten , then you would use `–networkid 3`. If you don’t specify this flag, then Ganache CLI will default to connecting to mainnet . Now that Ganache CLI is running in one terminal window, open up another terminal window and navigate back to your project directory (i.e., where your `truffle-config.js` file is located). In this new terminal window , enter the following command:truffle migrate –reset This will compile your smart contracts and deploy them to your locally running Ethereum blockchain (i.e., Ganache CLI ). The output should look something like this: Using network ‘development’. Network up to date.

Running migration: 1_initial_migration.js Replacing Migrations. . 0xd305caeb03afeeec8425754820ab358280cf3df90780cfcf7085bf49729176676 Migrations: 0xcaeb03afeeec8425754820ab358280cf3df90780cfcf7085bf49729176676 Saving successful migration to network. 0xd5830102078586d247083e3818dc7527273ad0795f73f2593993105565fe09767 Migrations: 0xb6830102078586d247083e3818dc7527273ad0795f73f2593993105565fe09767 Saving artifacts. Running migration: 2_deploy_contracts.js Replacing MyERC721Token.

. 0xc59d484711722444261579cb0829032ae19ff4923478607418066217927355ea8 MyERC721Token: 0x11722444261579cb0829032ae19ff4923478607418066217927355ea8 Saving artifacts.

As you can see from the output above , first Truffle migrating your 1_initial_migration , which deploys a placeholder Migrations contracts onto your blockchain . This allows Truffle keep track of which migrations have been run on your blockchain . Next , Truffle migrate your 2_deploy_contracts , which deploys your MyERC7221Token contracts onto your blockchain . Finally , Truffle saves all of your deployed contracts as “artifacts”, which are files located in the build/contracts directory . In order view information about your deployed contracts within Truffle , simply run truffle console within your project directory :truffle console

NOTE: WARNING: Coding a Crypto Collectible ERC 721 NFT Tutorial Ethereum can be complex and difficult to understand. It is important to carefully read through the tutorial and understand the code before attempting to attempt any coding. If you are not familiar with coding and programming, it is highly recommended to seek professional help or advice before attempting this type of tutorial. There is a risk of losing funds or creating an asset that has no value if the code is not properly written.

Truffle Develop started at http://127.0.0.1:9545/

(Ctrl+C to kill)

(truffle(develop)$)

truffle(develop)$ MyERC7221TokenContract = web3Contract(abiMyERC2211okenContract,'
')

(truffle(develop)$) 

//replace <address> with actual address of deployed MyEtherNFTokenContract (truffle(develop)$) abiMyEtherNFTokenContract = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"allowedTokens","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type.
Previous ArticleNext Article