Learn how to code your own SoulBound NFT Smart Contract

Before writing our SoulBound contract, let us understand what SoulBound NFT is and how is it useful to us.

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

Soulbound tokens can help you:

  • create an openly verifiable system where people can issue their digital resume as SoulBound tokens which will help verifying an individual’s work histories and removes falsification.

  • make it possible for protocols to verify borrowers’ credit profiles assess their credit scores to determine how much they can obtain in loan.

  • can help issue educational certificates like driver’s license, certificates of incorporation, and other core personal and professional proofs.

and many more

Ideas where you can use SoulBound Tokens:

  • In your college, right now, for adding test scores

  • Using as an identification token in your college

  • Using Soulbound token as your Resume

Thus, lets not wait anymore and start with our Day 3 ⚒️

1. Create a project and install dependencies

1.1: Use the following commands on your CLI to initialize your project.

mkdir soulbound-NFT-with-buildbear && cd soulbound-NFT--with-buildbear

1.2: Run the command npx hardhat on your CLI and choose Create a Javascript Object; choose yes for all the options.

Once you have successfully executed the above and if you were to open this repo in VS Code, you should have a directory structure, similar to the following:

post image

2. Coding your Soulbound NFT contract

2.1: Create a file named Soulbound.sol in the contracts folder.

2.2 We have used the code of ERC-721 contract which we have already learnt in Dude! I just deployed my own NFT!!! ….WT.F!

You can use the following code:

post image

You can even get the above code from our Github over here

3. Deployment and Testing why SoulBound Tokens are different

Now, let us deploy it on private testnet using BuildBear to play around with the contract

3.1: Creating a Private Testnet on BuildBear 🐻‍❄️ *(why BuildBear, you ask? Have a look over here: **Where Localhost Fails and *Win Web3 Hackathons, using BuildBear Testnet’s analytics)

3.1.1: Visit the BuildBear App. Once you login with your Github account, you will see a page similar to the image added below

post image

Here we have to create a simple node for our staking application so we will click on the create an endpoint and we will be redirected to the node configuration page. But for this article, we will be using the default configuration for creating our node.

So we will simply click on the create button as shown below.

post image

Congratulations! You have created your private testnet node!

Your page should be updated to something similar to the following:

post image

Click on the RPC URL (either copy of click to view) to get the RPC to your private testnet.

3.2: In order to perform transactions, we need funds from faucet. Don’t worry! We don’t have to find random faucets to get test ether.

Click on the Open Faucet option and add your choose your account.

post image

After that, click on the Add to Metamask option in the right corner of the faucet page.

Your metamask would be provided with 1,000 BB ETH immediately.

3.3: Update thehardhat.config.js to the following:

post image

In the networks, we have created a network called buildbear and added our RPC URL there and then added the private key of the metamask account (please do not expose your private key to anyone, ever).

Now our config is ready! Let’s write our smart contract.

4. Deploy our Soulbound Contract using BuildBear

4.1: Use the following script to deploy your souolbound contract:

const { ethers } = require("hardhat");
const hre = require("hardhat");async function main() {
  const Soulbound = await hre.ethers.getContractFactory("Soulbound");
  const soulbound = await Staking.deploy();  await soulbound.deployed();  console.log(
    "Soulbound contract deployed to:", soulbound.address
  );
}main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Open your CLI and run the following command:

npx hardhat run scripts/deploy.js --network buildbear

4.2 Now if you go back to the BuildBear Explorer (link available from the BuildBear App, once you have created your private testnet). You will see your deployed contract.

post image

4.3 Now open your deployed address and open the write contract tab under the contracts tab

post image

You can see here we have all the functions which we have included in our contract.

To mint our soulbound token, we are going to call the safeMint function which requires the address of the user and the tokenURI of the NFT which includes all the metadata of the token.

4.4 (optional): If you’re not aware of how to upload tokenURI on IPFS then you must follow along.

IPFS allows you to store your data like images, and other data on the blockchain. In order to store your own data, open Pinata which is a platform that provides you to store data on IPFS.

Create a file .json file on your system and add the following data to it.

{
    "title": "John Doe",
    "type": "Identity Card",
    "properties": {
        "age": {
            "type": "string",
            "description": "19"
        },
        "image": {
            "type": "string",
            "description": "hhttps://gateway.pinata.cloud/ipfs/QmQPo8X32C1NnpqjwckxyAHpmXyoJk1G1x4fu5GLcxLqVG"
        }, 
        "course": {
            "type": "string",
            "description": "Computer Science"
        }
    }
}

And upload this file on Pinata. This will provide you with an IPFS link where the metadata is stored.

Use that link to mint your NFT token.

4.5 Pass your wallet address and tokenURI to the safeMint function to mint your Soulbound token.

post image

And click the write button.

You can now see that our Soulbound token is minted. Use the transaction hash provided to see the token information.

post image

Here you can see in the token transferred, we got ERC721 Transfer found

4.6 If you’ll see the advanced tab, you can see the token has been minted at the address provided by the user.

post image

You can use the URI value and search it on your browser, you will receive the metadata of your token, which you just provided.

post image

USP of SoulBond Tokens. This is what makes SoulBond the “SoulBond”

Soulbound tokens are majorly used as identification tokens which can only have one owner.

But even in the identification tokens, we might need to update any new data to it. So for that, we are not going to mint a new token, instead update the metadata of the same token.

Let us understand how we can update the metadata of the minted token.

4.7 Open Pinata and click on the edit metadata option and add the key value pair you want to add to the metadata.

post image

This will add the above key-value pair to your metadata in some time and you don’t need to mint another token to add the metadata 😃

Soulbounds tokens are not just simple NFTs but have their own advantages which makes it so attractive.

To learn more about BuildBear, read here docs

Get the above Github code from here

If you appreciate what we are doing, please follow us on Twitter and Join the Telegram group if you haven’t done yet.

Author:

Pari Tomar (Twitter || LinkedIn), always open to feedback and learning.

BTW, if you would know anyone who would be keen to working with BuildBear. Please have a look over here!!!