Guide to creating your own NFT with Javascript & Solidity – UPDATED to include video guide -🌎(Part 3 of 3)

Gary George
Coinmonks
Published in
5 min readSep 11, 2021

--

In Part 2 [LINK] we interacted with our contract and prepared our content to be minted, Here we are going to finish off the process and mint an NFT.

What are Gas Fees?

“Gas fees are payments made by users to compensate for the computing energy required to process and validate transactions on the Ethereum blockchain. … A higher gas limit means that you must do more work to execute a transaction using ETH or a smart contract.”

Basically every transaction you make on the blockchain comes at a cost, The cost varies but here is a real life example i have faced.

The price for me to mint 1000 NFTs onto the Ethereum Mainnet was going to cost me the equivalent of £3000, I knew instantly this was out of my budget for a simple pet project so i looked further a field.

Now this is out of the scope of this article but i decided to Mint my NFTs on the Polygon Mainnet (see [HERE] for what this is). The price it ended up costing me to mint all of the NFTs on Polygon was £0.30, literally 30p ! 🚀

So be careful when doing such a task and always experiment on the [TestNets] first.

Minting your content

Ok, so you are all set up, the front end website is hooked up to your contract, your assets have been created and pinned to IPFS. Its time to MINT your NFTs.

You will need to create a script and then run the script using the [TRUFFLE CLI], here is the command i use to run my minting script from the command line:

truffle exec src/utils/mint.js --network polygon

The flag — network polygon points to the network details for polygon network which i added to my truffle-config.js.

it will look something like this:

...polygon: { provider: () => new HDWalletProvider( privateKeys.split(","), `https://polygon-mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`), network_id: 137, confirmations: 2, timeoutBlocks: 200, skipDryRun: true, chainId: 137,},
...

You will need to sign up to [INFURA] or similar to get a Project ID for an ethereum node on your desired network.

Here is a valid example of a mint script that you can call from the above command :

//THIS IS THE PATH TO YOUR ABI FILE
const NFT = artifacts.require("NFT");
module.exports = async function (callback) { // THIS IS CHECKING THAT THE SCRIPT CAN FIND YOUR CONTRACT
const nft = await NFT.deployed();
//THIS WILL MINT AN NFT
try {
await nft.mint(PATH_TO_UR_METADATA_FILE_ON_IPFS); } catch (e) { console.log("failed", e); }}

Wow!, how simple was that 🦄.

Buying an NFT

For a user of your website to purchase an NFT, they must first connect their wallet and secondly they must be on the correct network.

If they are on a network which does not have your contract deployed then they will receive an error message.

The below snippet is an example of a function call in javascript that can facilitate the transfer of ownership of a token (purchase an NFT).

const buyNFT = async (account, contract, showModal) => {  //THE TOKEN ID TO PURCHASE
const tokenID = 1;
const accountAddress = THE_USERS_WALLET_ADDRESS;
// ONE THING TO NOTE THE PRICE WILL BE IN WEI
// -> 18 DECIMAL PLACES
// THE BELOW WORKS OUT AT 0.021 Eth
const price = 21000000000000000;
//THE ACTUAL CALL TO THE CONTRACT FOR PURCHASE await contract.methods
.buy(id)
.send({ from: accountAddress, value: price })
.on("receipt", async () => {
console.log(`You've received the NFT with ID: ${id}`);
}
.on("error", (error) => {
console.log(`error in purchase- ${error}`);
}
}

Verifying NFT ownership

Excellent, we are at the final hurdle.

How can you verify ownership of an NFT? Well we have actually already been over this.

Take a look at the code snippet i shared in [PART2]. The function to focus your attention on is :

const owner = await contract.methods.ownerOf(tokenID).call();

The above function will go off to the contract on the blockchain and return the owners wallet address.

All NFTs have an owner whether they have been purchased or not, the initial owner is the contracts address, upon a purchase the owner gets transferred to the users wallet address.

Free Video [Code Walkthrough]

I have recorded a video walking through the boilerplate code [LINK]. I really hope this helps other developers out who are looking to go into Blockchain development.

Crypto Ghoulz

For research into this topic i created my own blockchain based website on the polygon network, which allows users to purchase NFTs, check it out and if your feeling spicy purchase one of my NFTs. https://cryptoghoulz.com .

If you have any questions surrounding Crypto Ghoulz get in touch via twitter.

Project Code

If you want to get started on this you can find a boiler plate repo [HERE].

This is a good to go project that can help you get a real feel for how everything works.

--

--