DEV Community

Anil Dukkipatty
Anil Dukkipatty

Posted on • Updated on

Building NFTs using truffle and revise

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 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 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.

Image description

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:

// 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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) to setup the backend and add the data to our NFTs. Let’s grab a development key from Revise. Visit https://revise.network 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. This is a very basic starter kit, it has the revise-sdk setup. Once you clone it, run npm install. Open the index.js and paste the following code:

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()
Enter fullscreen mode Exit fullscreen mode

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. 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.

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

This looks like a great article! 🙌

In fact, the topic of your post would also work really well in the Meta Punk Community too!

Metapunk Web3 Community 🦙

We’re a community where blockchain builders and makers, web3 devs, and nft creators can connect, learn and share 🦙

favicon metapunk.to

Meta Punk is a really cool international NFT and web3 community where artists, web3 developers, and traders can connect, learn, and share exciting discoveries and ideas. 🦙

Would you consider posting this article there too? Because Meta Punk is built on the same platform as DEV (Forem) you can fairly easily copy the Markdown and post it there as well.

Really hope that you'll share this awesome post with the community there and consider browsing the other Forem communities out there!