Minting NFTs with NestJS and web3.js

Cláudio Rapôso
3 min readJul 1, 2023

In this article, we will explore how to create (or “mint”) a Non-Fungible Token (NFT) using Nest.js, and Web3.js. NFTs are unique tokens on the Ethereum blockchain that can represent ownership of unique digital assets such as digital art, game items, and more.

This code is based on this article:
Integrating MetaMask with NestJS and Web3.js: A Detailed Guide | by Cláudio Rapôso | Jun, 2023 | Medium

What is an NFT?

A NFT, or Non-Fungible Token, is a special type of cryptographic token that represents something unique. This contrasts with fungible tokens, like Bitcoin or Ethereum, where each token is identical to every other one. NFTs are used to create verifiable proofs of authenticity and ownership within the digital realm, making them ideal for collecting and trading digital assets such as art, music, and more.

What is web3.js?

web3.js is a JavaScript library that allows you to interact with the Ethereum blockchain from JavaScript applications. It’s used to build applications that can interact with smart contracts and other aspects of the Ethereum blockchain.

Creating the Web3 Module and Service

We’re using NestJS to structure our application. We’ll create a module and a service for interacting with the Ethereum blockchain.

Here is the basic code for the module:

import { Module } from '@nestjs/common';
import { Web3Service } from './web3.service';
import Web3 from 'web3';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
imports: [ConfigModule],
providers: [
{
provide: 'Web3',
useFactory: (configService: ConfigService) => {
return new Web3(configService.get('INFURA_URL'));
},
inject: [ConfigService],
},
{
provide: 'Config',
useFactory: (configService: ConfigService) => {
return {
wallet: configService.get('WALLET'),
privateKey: configService.get('PRIVATE_KEY'),
contractAbi: configService.get('CONTRACT_ABI'),
contractAddress: configService.get('CONTRACT_ADDRESS'),
};
},
inject: [ConfigService],
},
Web3Service,
],
exports: [Web3Service],
})
export class Web3Module {}

In this code, we’re setting up the connection to the Ethereum blockchain and the information about our Ethereum wallet. We’re using the Config service from NestJS to get these pieces of information from our environment variables.

Now, here is the code for the service:

...

@Injectable()
export class Web3Service {
constructor(
@Inject('Web3')
private readonly web3: Web3,
@Inject('Config')
private readonly config: { wallet: string; privateKey: string; contractAbi: any; contractAddress: string },
) {}

...

async mint(recipient: string, tokenId: string, tokenURI: string) {
const contract = new this.web3.eth.Contract(this.config.contractAbi, this.config.contractAddress);
const nonce = await this.web3.eth.getTransactionCount(this.config.wallet, 'latest');
const tx = {
from: this.config.wallet,
to: this.config.contractAddress,
data: contract.methods.mint(recipient, tokenId, tokenURI).encodeABI(),
gas: 5000000,
nonce,
};

const signedTx = await this.web3.eth.accounts.signTransaction(tx, this.config.privateKey);

const result = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction);

return result.transactionHash;
}
}

In this code, we’re creating an instance of the Ethereum contract that we’re going to interact with. We have a “mint” function that can be used to create a new NFT.

What’s Happening in the mint Method?

Let’s break down what’s happening in this function.

  1. Fetch the nonce: The nonce is the number of transactions that have been sent from an address. It’s used to prevent double spending and ensure the order of transactions.
  2. Create the transaction: We build a transaction object. This object includes the contract address, the method call (in this case, mint), and the gas we’re willing to spend to execute the transaction.
  3. Sign the transaction: We sign the transaction with the private key of our wallet. This proves that we are the sender of the transaction.
  4. Send the transaction: We send the transaction to the blockchain.

Conclusion

This is a basic example of how you can mint an NFT with Node.js, NestJS, and web3.js. In the real world, you’ll have to deal with more details such as handling gas fees and securely storing private keys. Additionally, each NFT contract may have its own unique methods and requirements that you’ll need to fulfill. But we hope this tutorial has given you a starting point to explore further.

If you want to listen in portuguese, follow the link:

Minting de NFTs com NestJS e web3.js | by Cláudio Rapôso | Jul, 2023 | Medium

--

--

Cláudio Rapôso

Microsoft MVP | Software Architect | Teacher | Book Author | MCT | 12x Microsoft Certified Connect with me in https://www.linkedin.com/in/cfraposo