DEV Community

krinza.eth (🧱, 🚀)
krinza.eth (🧱, 🚀)

Posted on • Originally published at ankr.hashnode.dev on

NFT 2.0: Creating a Soulbound Token

We laud NFTs for their transferability but Vitalik's extended idea in a blog post titled "Soulbound", over the long haul is showing up in literally every conversation happening around identity, reputation, and individuation in web3. If you haven't checked it out yet, go give it a read. If you've already and need a refresher, here's a TLDR:

Soulbound Tokens (SBT) are just non-fungible tokens (NFTs) that, "once picked up, cannot be transferred or sold to anyone else."

o.jpg

Provoking name and the Kardashian clan's shenanigans aside, there are (IRL) some things you just cannot sell. Think of your drivers license, university degree, and formal identification. What if all of it could be encoded on the blockchain and verified by the token, coined as soulbound?

Sounds interesting? Lets SHIPPPPP one!


Building a Soulbound NFT

In this tutorial, we will learn how to:

  1. Create a non-transferable NFT smart contract on Solidity
  2. Deploy it on Ethereum Rinkeby Testnet
  3. and display the minted Soulbound token on Opensea

screely-1657621556883.png

If you followed along the tutorial I published about building an ERC721 Smart Contract, then you already know most of the code necessary to build them. If not, you can always refer to that video to explore more. So, let's get building!

Step 1: Writing NFT Smart Contract

Before we implement the non-transferring logic in our smart contract, we would need a base ERC-721 code for NFTs. To start, well navigate to the OpenZeppelin Wizard, select ERC721 as our contract type and opt for a few features like "Mintable", "Auto-increment IDs" and "Base URI" from the sidebar as shown.

screely-1657577275934.png

Step 2: Customizing Code to Make it Soulbound

Now that we have the base code ready, let's head over to Remix Solidity Compiler by clicking, "Open in Remix".

screely-1657610347231.png

Once we're on Remix, you should be able to see the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts@4.7.0/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.7.0/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts@4.7.0/access/Ownable.sol";
import "@openzeppelin/contracts@4.7.0/utils/Counters.sol"; 

contract SoulBoundTest is ERC721, ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("SoulBoundTest", "SBT") {}

    function safeMint(address to, string memory uri) public onlyOwner {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

    // The following functions are overrides required by Solidity.

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }
}

Enter fullscreen mode Exit fullscreen mode

Now let's go ahead and add a new code snippet to the existing smart contract in order for us to block the transfer of NFT and eventually making it "Soulbound." There already exist an API in the OpenZeppelin's ERC721 Contract, using which we can actually control the transferability of our tokens.

If you navigate to the OpenZeppelin API Docs, you will find the following functions:

  • _beforeTokenTransfer ( address from, address to, uint256 tokenId)

To block token transfers, add the following to your code:

File: openzeppelin-contracts/contracts/token/ERC721/ERC721.sol

function _beforeTokenTransfer(
address from, 
address to, 
uint256 tokenId
) internal override virtual {
require(from == address(0), "Err: token transfer is BLOCKED");   
super._beforeTokenTransfer(from, to, tokenId);  
}

Enter fullscreen mode Exit fullscreen mode

Now let's understand the logic behind this code.

Every time this code will run, the require statement will check: if the from address parameter in the function is set to zero. If yes, it will allow the action to happen and block all the other transfers to make it a non-transferable token.

Note: When the address from == 0, it means the token is being issued or minted and not transferred.

Step 3: Deploying The Soulbound NFT Smart Contract

To deploy the smart contract directly from remix, head over to side bar and select " Deploy and Run Transaction" from the tab. Set Environment to Injected Web3 , select the Soulbound smart contract we just created from contracts dropdown and click Deploy.

Make sure your MetaMask is connected to the Ethereum Rinkeby Testnet and you've some test ether to make transactions happen.

screely-1657616462343.png

Step 4: Adding NFT Asset To IPFS Via Pinata

Once the contract is successfully deployed, the next thing we will need to do is minting a Soulbound NFT. To do so, we are going to first set the NFT asset and metadata using Pinata.

If you dont have a Pinata account, sign up for a free account here and complete the steps to verify your email.

  • Once you have verified your account on the Pinata, navigate to the " My Files" page and click the " Upload" button
  • Upload your NFT art and you'll see your image asset file under the Files page along with the CID column

screely-1657619551837.png

Copy the CID by clicking the copy button next to it and use it as a postfix to this URL " gateway.pinata.cloud/ipfs" to view your NFT art.

Here's the image that I used.

download (1).png

Now, were going to upload one more document to Pinata - JSON metadata. To do that, create a JSON file and add the following code.

{
    "attributes" : [ {
      "trait_type" : "NFT",
      "value" : "Soulbound"
    }, {
      "trait_type" : "Asset",
      "value" : "Non-transferable Badge"
    }, {
        "trait_type" : "Badge",
        "value" : "Minting First Soulbound Token"
      }, {
        "trait_type" : "Type",
        "value" : "Demo"
      } ],
    "description" : "This is a demo soulbound token mint",
    "image" : "https://gateway.pinata.cloud/ipfs/QmTY1jMAqabHUZFCj91ckwnCu3CPtJChpg345jdkxzj3ac",
    "name" : "Soulbound Token"
}

Enter fullscreen mode Exit fullscreen mode

Feel free to add or remove the attributes. Also, make sure you are providing your own IPFS image URL!

Once youre done editing the JSON file, save it and upload it to Pinata, following the same steps we followed for uploading the image and copy the CID for the JSON file (we will need that in the next step).

screely-1657619929495.png

Step 5: Minting The Soulbound NFT

In this step, we are finally going to mint the NFT and display it on Opensea. To mint the NFT, head back to Remix IDE, find " Deployed Contracts" button, and click on the " SafeMint" function.

screely-1657620320333.png

Under SafeMint, input your wallet address in to and set the JSON file's URL in URI, following the format as " gateway.pinata.cloud/ipfs/{CID_of_JSON_file}". Here's the IPFS link to the Metadata I uploaded to the Pinata.

Once the configurations is set, hit transact , confirm the transaction and wait a few seconds for the NFT to be minted.

screely-1657621006676.png

Now, we are going to head to Opensea (Testnet) to view the minted Soulbound NFT.

screely-1657621356982.png

screely-1657621556883.png

Since we have blocked all the transfer action, if you try to send this NFT to some other wallet it will prompt you an error. This way, we can confirm that the NFT we just minted is now bounded to our souls (realistically, to our wallet address) and can never be transferred or sold to anyone else.

screely-1657621920315.png


Full Code Repository

Wohoo! Weve finally deployed our very first Soulbound NFT on Rinkeyby Testnet. If you want to take a look at the full source code, find the repo below.

https://github.com/kaymomin/soulbound-nft-token-smart-contract/blob/main/soulbound.sol


Whats Next?

A fun project would be to create a platform that lets course creators issue a Soulbound NFT as a credential for students who complete the course.

PS: If you work on this idea, feel free to comment your creation in the comment section below.

You can also drop me a "👋" on Twitter @kayprasla or find me in the Developer DAO discord!


Other Articles Ive Written

https://ankr.hashnode.dev/how-to-deploy-your-own-erc-20-token-with-ankr-and-hardhat-on-eth-goerli-testnet

https://ankr.hashnode.dev/deploy-and-mint-a-cryptokitties-like-nft-with-erc-721-smart-contract

Top comments (2)

Collapse
 
louisvw4 profile image
AyMax

Hey @kayprasla, I'm quit interested in this topic and I wanted to ask, if you maybe wanted to have a conversation on Discord about SBTs and their creation?

You can contact me through:
aymax05@gmail.com

AyMax#1123 (Discord)

Collapse
 
shreyashenoy profile image
Shreya Shenoy

Great piece, thanks for sharing. I'm learning more about NFTs, and seeing a variation of it was very informative.