How to: Create and Deploy an NFT Smart Contract in 10 Minutes

How to Create and Deploy an NFT Smart Contract in 10 Minutes

Non-Fungible Tokens (NFTs) are all the rage right now and as a developer, you might be wondering how you can create your own NFTs. This article will explain how to do it in the simplest way possible so you’ll be up and running in just a few minutes.

We’ll use the standard OpenZeppelin ERC721 contracts that are the gold standard of smart contracts.

There are various tools for deploying smart contracts, but for this article we’re going to use the web3 CLI tool as it’s the quickest and easiest tool to compile and deploy contracts.

Get Your Environment Set Up

The following stuff just has to be done once, then you can deploy and interact as much as you want without doing this stuff again.

Install the CLI tool

This one liner will install the tool so you can start using it right away. Feel free to read the install.sh code if you’re worried what it’s doing.

curl -LSs https://raw.githubusercontent.com/gochain/web3/master/install.sh | sh

Set the network

We’re going to use GoChain here so we don’t have to pay $100 to deploy the contract then then $50 to mint every NFT. With GoChain, we’ll only have to pay a fraction of a penny and it’s 100% Ethereum compatible so we can use it the same way we would with Ethereum. You can of course change the line below to point to Ethereum and pay the extra cost if you’d like.

export WEB3_NETWORK=gochain
# for ethereum:
# export WEB3_NETWORK=ethereum

Add / Get Gas

You’ll need to use an account that has some gas for these transactions, or create a new one and send gas to it. This will be $GO on GoChain or $ETH for Ethereum.

To create a new account, run:

web3 account create

This will print:

Private key: 0xABC123
Public address: 0xXYZ456

Send some $GO to the “Public address”. If you need some $GO to get started, you can find where to buy some here.

Copy the “Private key” (or use an existing one you already have if you’d like) and run:

export WEB3_PRIVATE_KEY=0xABC123

Also, keep a copy of this private key somewhere safe so you can use it next time.

Let’s Deploy!

Now that we’re all setup with network set and our private key is set with some gas in it, we can start having some fun.

Creating the Contract

The web3 CLI has built in code generators for common contracts such as ERC20 and ERC721’s. Just run:

web3 generate contract erc721 --symbol KATS --name "Krypto Kats" --base-uri https://kryptokats.com/token/

This will generate a file called KATS.sol which is the solidity program for your new NFT contract.

The base-uri argument should point to a server you control so that you can then deliver the NFT metadata when someone queries it . We’ll cover the metadata and image/video files below.

Deploy the Contract

From here we just need to compile and deploy it. Again, the web3 CLI makes this part easy:

web3 contract build KATS.sol
web3 contract deploy KATS.bin

This will print out your new contract address. You’ve now deployed an NFT contract!

Mint NFTs

Now that the contract is deployed we simply need to mint new ones. First let’s set the contract address as environment variable so the rest of the commands will use that contract:

export WEB3_ADDRESS=0xCONTRACT_ADDRESS

You’ll need to provide the address of the person that will own this new NFT when minting:

web3 contract call --wait --abi KATS.abi --gas-limit 2000000 --function mint 0xABC

This function mints a brand new NFT and gives it to the address 0xABC. The new OpenZeppelin presets for ERC721’s have an auto incrementing ID, so you’ll see in the output the new token ID. eg:

Parsed Logs: [
{
"name": "Transfer",
"fields": {
"from": "0x00000",
"to": "0x000ABC",
"tokenId": "0x00001"
}
}
]

That output has been truncated, removed a bunch of zeros, but you can see the new token ID is the number 1 .

Token Metadata and Image / Video Files

When someone queries the contract you deployed for the NFT, they will call the tokenURI(tokenID) function. This function MUST return a URL that points to the token metadata. An HTTP GET call to the token URL MUST return token metadata which consists of the following:

{
"name": "Token X",
"description": "Token X is an awesome NFT",
"image": "https://somewhere.com/tokens/x.png"
}

The “image” field in that JSON returns another URL to the actual digital file. This can be an image or an mp4 video.

You can host the JSON and the image/video or if you want it truly decentralized, you can store them on IPFS, but using IPFS for this is more complicated so we won’t cover that in this article.

Transfer NFTs

Now if you own an NFT and want to transfer it, you can call the transferFrom function:

web3 contract call --wait --abi KATS.abi --gas-limit 2000000 --function transferFrom 0xABC 0xXYZ 1

This will transfer token with ID 1 to a new owner at 0xXYZ .

You can view and search for tokens on the GoChain Explorer. This link is the contract used for this article.

That’s All Folks

The main two things you’ll ever do with an NFT is mint them and then the owners can transfer them. NFT’s are essentially proof of ownership so the main thing you can do with one is prove you own something or sell/transfer it to someone else.

If you are writing an app, you can use the above to deploy and test your contract then you’ll probably want to use a web3 library for your programming language to mint and transfer.

Now that you know how to make your own NFTs and use them… get building!

Let us know if you have any questions in the comments below.

Post a Comment

0 Comments