Building NFTs using truffle and revise
Tags: web3, blockchain, tutorial, javascript
Published: 2022-09-27 12:51:25

Index for the post:

- Overview
- Prerequisites
    - Setting up Ganache
- Installing truffle command-line app
- Building the NFT ERC721 smart contract
- Adding NFT data using Revise

This is a quick guide to building NFTs using truffle and javascript (revise-sdk).

Prerequisites: Node version 15 or above, NPM version 7 or above

We’ll start by installing Ganache. [Ganache](https://trufflesuite.com/ganache/) will run a sample blockchain locally on your development machine. This will allow us to build and test our NFT smart contracts.

Click on [this link](https://trufflesuite.com/ganache/) to visit the download page. Choose your platform and download the installer. Once it is installed, open the ganache app and click on the “Quickstart Ethereum” button. This will start a simulation of the Ethereum blockchain. We can now start developing and testing our NFT smart contracts in [Truffle](https://trufflesuite.com/).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9ykhoax4t9woct3nl3w.png)

Let’s install Truffle next. We’ll use NPM to download and setup the truffle command-line app. Run `npm install truffle -g` in your terminal. Once this command is done installing Truffle you’ll be able to use it from any folder on your computer.

Make a new directory, call it `truffle-revise-tutorial`. Once you `cd` into that directory run `truffle init` this will setup all the boilerplate required to start building smart contracts. Make a new file in the contracts folder to write the smart contract. You can also run the truffle scaffolding command `truffle create contract ReviseNFT` from the terminal to create the “ReviseNFT.sol” file (smart contract file).

Paste the following code in the file you just created:

```solidity
// Let’s start by importing the Openzeppelin ERC-721 template into our file
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// Next, let’s add our NFT smart contract and name the NFT token (Dynamic NFT)
contract ReviseNFT is ERC721 {
    string baseuri = "";
    constructor(string memory _baseuri) ERC721("Dynamic NFT", "dNFT") {
        baseuri = _baseuri;
    }
    // Last but not the least, let’s add functions to enable minting and to enable setting the _baseURI().
    function mint(address to, uint256 tokenId) public {
        _safeMint(to, tokenId);
    }
    function _baseURI() internal view override(ERC721) returns (string memory) {
        return baseuri;
    }
}
```

Once you’re done pasting the above code run `npm install @openzeppelin/contracts` to install the missing dependencies.

This is boilerplate for an NFT smart contract. We’re using the Openzeppelin ERC721 template as the base and adding a couple of functions on top to enable minting and setting the “_baseuri”. The “_baseuri” allows you to add data (name, image, description, attributes, etc) to your NFTs.

We’ll use [Revise](https://revise.network?utm=article)  ([https://revise.network](https://revise.network?utm=article)) to setup the backend and add the data to our NFTs. Let’s grab a development key from [Revise](https://revise.network?utm=article). Visit [https://revise.network](https://revise.network/?utm=article) and click on “Get started”, once you make an account click on “Generate API key” in the header. Once you generate the key, copy the key and save it somewhere.

[Clone this repo](https://github.com/revise-network/nft-tutorial-1). This is a very basic starter kit, it has the [revise-sdk](https://www.npmjs.com/package/revise-sdk) setup. Once you clone it, run `npm install`. Open the index.js and paste the following code:

```jsx
const { Revise } = require("revise-sdk");
const AUTH_TOKEN = "...PASTE YOUR AUTH TOKEN HERE...";
const revise = new Revise({auth: AUTH_TOKEN});

async function run() {

	const collectionId = await revise.addCollection("Collection Name", "Collection_URI")

	// Collection Name : Use any name you want for your collection (this gets shown in the marketplace))
	// Collection_URI  : Use a unique name (no spaces or special characters)
	//                   this will generate a unique link for your collection
	//                   for e.g. if you choose "myuniquecollection"
	//                   your baseURI wil be "myuniquecollection.revise.link"
	const nft = await revise.addNFT({
    image: 'https://revise-testing.fra1.digitaloceanspaces.com/sample-collection/1.jpg',
    name: 'Star Lord',
    tokenId: '1',
    description: 'This is a test description'
  }, [
    {attack: "80"}, {color: "maroon"}, {stamina: "90"}
  ], collectionID.id)

	console.log(nft)
}
run()
```

Replace the `AUTH_TOKEN` with the auth token you’ve generated from Revise. In the above snippet we’re just importing the revise-sdk, setting up the authentication token and setting up the function to run the revise-sdk function calls. In the above code we’re create a collection for our NFT (all NFTs belong to a collection - think of it as files in a folder) and adding some data into the NFT. Run the index.js file `node index.js`. Now we’ve added our first NFT! We can test this out, visit `revise.link/p/myuniquecollection/1` (replace “myuniquecollection” with the “collectionURI” you chose earlier).

Now all we have to do is replace the “_baseuri” in the above smartcontract with the link we’ve generated via [Revise](https://revise.network/?utm_source=forums&utm_medium=essay&utm_campaign=ua&utm_id=ap). We’ve successfully built our first NFT!

In the next article I’ll walk you through running the NFT smart contract we built on Ganache and an Ethereum testnet. See you soon.