Skip to main content

Minting ERC 721 Tokens with FuseBox Web SDK - Smart Contract

In this tutorial, we'll explore how to use the FuseBox Web SDK to build a Next.js application that interacts with the Fuse blockchain. We'll cover connecting to MetaMask, initializing a Smart Contract Wallet, and minting NFT Tokens on the Fuse blockchain.

To build an NFT DApp with Hardhat and a frontend powered by Account Abstraction on the Fuse blockchain, we'll create a simple NFT smart contract using Hardhat and integrate it with the Fuse web SDK for account abstraction. This guide is divided into 2 parts: Part 1: The NFT Smart Contract Part 2: The User Interface.

Part 1: The NFT Smart Contract

1. Initialize Hardhat Project

Create a new directory for your project and initialize a Hardhat project.

mkdir NFTDApp && cd NFTDApp
npm init -y
npm install --save-dev hardhat
npx hardhat

Follow the prompts to set up your Hardhat project.

2. Install Required Packages

Install the necessary packages for the project.

npm install ethers hardhat-waffle @nomiclabs/hardhat-ethers

3. Smart Contract Development

Create a simple NFT contract using Hardhat. Save the file as NFTContract.sol in the contracts directory. This particular NFT allow an EOA to mint no more than 5 times each.

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

import "@openzeppelin/contracts@4.0.0/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.0.0/access/Ownable.sol";

contract LimitedMintERC721 is ERC721, Ownable {
// Counter to keep track of the number of NFTs minted by each address
mapping(address => uint256) private _mintedCount;

// Maximum number of NFTs that an address can mint
uint256 private _maxMintPerAddress = 5;
uint256 private _nextTokenId;
// Event triggered upon a successful minting
event Minted(address indexed to, uint256 tokenId);

// Constructor to initialize the ERC721 contract
constructor(address initialOwner)
ERC721("MyToken", "MTK")
Ownable()
{}

// Function to set the maximum mint limit per address
function setMaxMintPerAddress(uint256 maxMintPerAddress) external onlyOwner {
_maxMintPerAddress = maxMintPerAddress;
}

// Function to get the maximum mint limit per address
function getMaxMintPerAddress() external view returns (uint256) {
return _maxMintPerAddress;
}

// Function to mint a new NFT
function mint() external {
// Ensure the caller is not exceeding the mint limit
require(_mintedCount[msg.sender] < _maxMintPerAddress, "Exceeds mint limit");

// Mint the NFT
uint256 tokenId = _nextTokenId++;
_safeMint(msg.sender, tokenId);

// Increment mint count for the caller
_mintedCount[msg.sender]++;

// Emit the Minted event
emit Minted(msg.sender, tokenId);
}
}

Step 4: Compile the Smart Contract

To compile the contract run npx hardhat compile in your terminal:

npx hardhat compile

Step 5: Deploy the Smart Contract

In Hardhat, deployments are defined through Ignition Modules. These modules are abstractions to describe a deployment; that is, JavaScript functions that specify what you want to deploy.

Open ignition inside the project root's directory, then, enter the directory named modules. Create a deploy.js and paste the following code:

const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");

const NFTContract = buildModule("NFTContract", (m) => {
const contract = m.contract("NFTContract");

return { contract };
});

module.exports = NFTContract;

Step 5: Run Deployment

Update the hardhat.config.js file to include the network information:

// hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");

module.exports = {
solidity: "0.8.0",
networks: {
fuse: {
url: `https://rpc.fuse.io`,
accounts: [`0xPRIVATE_KEY`],
gas: 2100000,
gasPrice: 8000000000,
saveDeployments: true,
},
},
};

Please note: The 0xPRIVATE_KEY is used to sign the Transaction from your EOA without the need to request permission. You must ensure the EOA that owns the Private Key is funded with some Fuse Tokens to pay for Gas when deploying the Smart Contract.

Deploy the smart contract to the Fuse network by running the Hardhat deployment script:

npx hardhat ignition deploy ./ignition/modules/deploy.js --network fuse