Minting NFTs through API using Truffle & Rinkeby
You need the image / art work / clip to be uploaded to IPFS. You can use any of the IPFS clients that allow you to upload the asset and pin it which will make the asset available for anyone to access through a link. I am using Pinata Cloud for IPFS
It is the season of NFTs and DeFi. In case you have been living under a rock then you need read more about NFTs and DeFi using the following links.
Now that you understand the terms, let us understand how NFTs are minted. NFT market is definitely moving from a few minters to tools & techniques for content creators to mint NFTs on their own.
The following are the key steps in minting a NFT.
- You need the image / art work / clip to be uploaded to IPFS. You can use any of the IPFS clients that allow you to upload the asset and pin it which will make the asset available for any one to access it through a link. I am using Pinata Cloud for IPFS.
- You need some test ethers on your Metamask Wallet. Once you installed Metamask Google Extension, load test ethers using the Rinkeby faucet. Also, load some LINK on your Rinkeby testnet address.
I built these APIs on top of an existing repo by Patrick Collins. Check out the repo in the below GitHub link.
The above example deals with minting a collection of 'Dungeons and Dragons' to Rinkeby. It has the following key steps.
Step 1:
truffle migrate --reset --network rinkeby
Step 2:
truffle exec scripts/fund-contract.js --network rinkeby
Step 3:
truffle exec scripts/generate-character.js --network rinkeby
Step 4:
truffle exec scripts/get-character.js --network rinkeby
Step 5:
truffle exec scripts/set-token-uri.js --network rinkeby
Steps 1 & 2 deal with setting up Rinkeby connection and migrating the contracts related to NFT creation to Rinkeby Testnet. Steps 3, 4 & 5 include executing appropriate functions on the migrated contracts to randomly select characters and setting up metadata URI for the minted NFT. Please go through the README.md of the above repo to understand other set up details.
The idea is to build a NodeJS application that will use the above discussed steps. We can a user Node's Child Process to execute truffle commands on the CLI. Below is an example of wrapping up the first step in the Child Process call.
Just like above sample, we can create code to execute the remaining steps mentioned above to complete the minting process. Prior to executing these steps, we need to create the required contract and migrate it to Rinkeby testnet.
We can also create contract needed for minting the NFT using file manipulation in NodeJS. We make changes to the 'template' contract on the fly using NodeJS fs library and then execute the truffle commands to migrate the contracts.
In the above code block, we are copying sample.sol to contracts folder after deleting all the other existing contracts from the contracts folder. After copying sample.sol to contracts folder with desired name, we selectively replace contents of the newly created contract based on the request received in the express API call. The NFTs minted through the above process can be viewed on the opensea Rinkeby testnet gallery.
As discussed above, before we get ready with minting, we need to pin the image / art work to IPFS. We can build APIs for uploading and pinning the image to IPFS using Pinata, there are other ways as well. Please go through their docs to identify the APIs for uploading and pinning the image. Once the image is successfully uploaded Pinata APIs return CID which is a unique identifier for the uploaded file / image.
https://ipfs.io/ipfs/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?filename=filename.pngThe final URI looks something like above. The 'XXX' is where the unique CID will be. We need to embed the image URI inside metadata JSON file before uploading the JSON file to IPFS. Please go through metadata folder in Dungeon & Dragons GitHub repo for more details on how the metadata JSON file should look like.
Apart from the above, you can also plugin the market place from the opensea using the opensea api. Below is the sample ReactJS code to fetch the NFTs from opensea and display in a NFT Gallery.
This approach gives a better understanding of what goes into minting an NFT. This is definitely not a production ready code. For that, we may have to take out truffle and build using web3js.
Happy Minting!!
Read more at http://www.dudistan.com/