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.
10 Related Question Answers Found
Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of fraud or third party interference. In order to run a smart contract, you need a “world computer” that is distributed across a network of nodes, each of which executes and stores a copy of the code. This world computer is Ethereum.
If you’re interested in mining Ethereum, you need to know how to set your computer up to mine it. Luckily, it’s not too difficult to do. Here’s a step-by-step guide to setting your computer up to mine Ethereum.
1.
To start mining Ethereum with Ethminer, you need to have a computer with an AMD or NVIDIA graphics card. You also need to have the latest version of Ethminer installed on your computer. Once you have all of that, you can start mining Ethereum by running the following command:
ethminer -G -F http://eth-pool.org:8008/your_wallet_address
Replace “your_wallet_address” with your actual Ethereum wallet address.
First, you need a place to store your Ether. An Ethereum address is like a bank account number, and just like your physical bank account, you need a place to keep it safe. There are many wallets that you can use to store your Ether.
If you’re new to the world of Ethereum, Etherscan can be a confusing place. Here’s a quick guide on how to send Ethereum using Etherscan. When you first log in to Etherscan, you’ll be given a default view of the ETH blockchain.
An Ethereum address is necessary in order to hold or transact ETH. You can create an ETH address by following these steps:
1) Go to myetherwallet.com and create a new wallet.
2) Enter a strong password and click “Create New Wallet”.
3) Store your private key in a safe place.
4) Click “Save Your Address”.
5) Your ETH address will be displayed. Copy and paste it into your transaction software.
There are many different algorithms that can be used for Ethereum mining, but which one is the best? The most popular algorithm for Ethereum mining is called Ethash. This algorithm is designed to be memory-hard, meaning that it is difficult to produce ASICs (Application-Specific Integrated Circuits) for it.
There are a few different ways to get Ethereum Bitstamp. The easiest way is to use an exchange that supports ETH/USD trading pairs. Some of the most popular exchanges that offer this include Coinbase, Kraken, and Gemini.
If you want to get Ethereum on your Ledger Nano S, then there are a few things that you need to do. First, you need to make sure that you have the latest firmware installed on your device. Second, you need to install the Ledger Ethereum app on your device.
Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of fraud or third party interference. Ethereum is powered by Ether, a cryptocurrency that can be used to pay for gas, a unit of computation used in executing smart contracts on the Ethereum blockchain. The Ethereum Virtual Machine (EVM) is a Turing-complete virtual machine that allows anyone to execute arbitrary code on the Ethereum network.