NFTs (non-fungible tokens) are tokens that represent unique items. Unlike fungible tokens (like ERC-20), each NFT is distinguishable and can carry metadata that describes the item it represents (e.g., an image URL, attributes, or on-chain generated art).
On EVM-compatible networks like Shardeum, NFTs are typically implemented using the ERC-721 standard (or ERC-1155 for semi-fungible / batch minting use cases).
This guide is a step-by-step reference to help you create and deploy an ERC-721 NFT smart contract on Shardeum using Hardhat. Note: Some older screenshots or references may mention legacy Shardeum networks (e.g., Sphinx / Liberty). Always confirm you are connected to the Shardeum network you intend to deploy to.
We'll use OpenZeppelin Contracts that helps you to minimize risks by using battle-tested libraries of smart contracts for Ethereum and other blockchains. It includes the most used implementations of ERC standards.
Open up the shardeum-nft-dapp project in your preferable editor. Smart contracts are written in a language called Solidity which is what we will use to write our Domains.sol smart contract
Navigate to the "contracts" folder and create a new file called Domains.sol
Below is Domains smart contract from a test NFT dApp that we will be using for this tutorial. Copy and paste in the contents below into your Domains.sol file.
Note: This example mints a domain-style NFT (e.g., name.shm). It stores token metadata on-chain using a Base64-encoded tokenURI
// SPDX-License-Identifier: MITpragma solidity ^0.8.20;import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";import "@openzeppelin/contracts/utils/Base64.sol";import "@openzeppelin/contracts/utils/Strings.sol";contract Domains is ERC721URIStorage { error Unauthorized(); error AlreadyRegistered(); error InvalidName(string name); using Strings for uint256; uint256 private _tokenIds; string public tld; mapping(string => address) public domains; mapping(string => string) public records; mapping(uint256 => string) public names; address payable public owner; string svgPartOne = '<svg xmlns="http://www.w3.org/2000/svg" width="270" height="270" fill="none"><path fill="url(#a)" d="M0 0h270v270H0z"/><defs><filter id="b" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse" height="270" width="270"><feDropShadow dx="0" dy="1" stdDeviation="2" flood-opacity=".225" width="200%" height="200%"/></filter></defs><text x="32.5" y="231" font-size="27" fill="#fff" filter="url(#b)" font-family="Plus Jakarta Sans,DejaVu Sans,Noto Color Emoji,Apple Color Emoji,sans-serif" font-weight="bold">'; string svgPartTwo = "</text></svg>"; constructor(string memory _tld) ERC721("Web3 username NFT on Shardeum", "SHMNAME") { owner = payable(msg.sender); tld = _tld; } function price(string calldata name) public pure returns (uint256) { uint256 len = _strlen(name); if (len < 3 || len > 10) revert InvalidName(name); // Example pricing logic (in wei): // 3 chars: 0.9 ETH equivalent (placeholder), 4: 0.5, 5: 0.3, 6+: 0.1 // Update these values to match your intended pricing model. if (len == 3) return 0.9 ether; if (len == 4) return 0.5 ether; if (len == 5) return 0.3 ether; return 0.1 ether; } function register(string calldata name) external payable { if (domains[name] != address(0)) revert AlreadyRegistered(); uint256 _price = price(name); require(msg.value >= _price, "Not enough SHM paid"); string memory fullName = string(abi.encodePacked(name, ".", tld)); string memory finalSvg = string(abi.encodePacked(svgPartOne, fullName, svgPartTwo)); uint256 newTokenId = _tokenIds; string memory json = Base64.encode( bytes( string( abi.encodePacked( '{"name":"', fullName, '","description":"Web3 username NFT on Shardeum","image":"data:image/svg+xml;base64,', Base64.encode(bytes(finalSvg)), '","length":"', _strlen(name).toString(), '"}' ) ) ) ); string memory finalTokenUri = string(abi.encodePacked("data:application/json;base64,", json)); _safeMint(msg.sender, newTokenId); _setTokenURI(newTokenId, finalTokenUri); domains[name] = msg.sender; names[newTokenId] = name; _tokenIds++; } function getAddress(string calldata name) external view returns (address) { return domains[name]; } function setRecord(string calldata name, string calldata record) external { if (msg.sender != domains[name]) revert Unauthorized(); records[name] = record; } function getRecord(string calldata name) external view returns (string memory) { return records[name]; } function withdraw() external { if (msg.sender != owner) revert Unauthorized(); uint256 amount = address(this).balance; (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Failed to withdraw SHM"); } function getAllNames() external view returns (string[] memory) { string[] memory allNames = new string[](_tokenIds); for (uint256 i = 0; i < _tokenIds; i++) { allNames[i] = names[i]; } return allNames; } // Minimal UTF-8 string length helper function _strlen(string memory s) internal pure returns (uint256) { uint256 len; uint256 i = 0; uint256 bytelength = bytes(s).length; while (i < bytelength) { bytes1 b = bytes(s)[i]; if (b < 0x80) i += 1; else if (b < 0xE0) i += 2; else if (b < 0xF0) i += 3; else if (b < 0xF8) i += 4; else if (b < 0xFC) i += 5; else i += 6; len++; } return len; }}
Now that we have created smart contract, we need to deploy this smart contract to the applicable Shardeum testnet
We've created a MetaMask wallet and written our smart contract, and now it's time to connect these two! Before that,
Every transaction sent from your virtual wallet requires a signature using your unique private key. To provide our program with this permission, we can safely store our private key in an environment file.
First, install the dotenv package in your project directory:
A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts, so Disperse here is a factory for instances of our Disperse contract. When using the hardhat-ethers plugin ContractFactory and Contract, instances are connected to the first signer (owner) by default.
await domainContract.deployed()
Calling deploy() on a ContractFactory will start the deployment, and return a Promise that resolves to a Contract object. This is the object that has a method for each of our smart contract functions.
You should then see something like this:
G:\shardeum-nft-dapp>node scripts/deploy-domains.jsshm name services deployedcontract depolyed to : 0x5FbDB2315678afecb367f032d93F642f64180aa3_name contract.shmRegistering contract on the contract with tokenId 0--------------------------------------------------------Final tokenURI data:application/json;base64,eyJuYW1lIjoiY29udHJhY3Quc2htIiwiZGVzY3JpcHRpb24iOiJXZWIzIHVzZXIgbmFtZSBORlQgb24gc2hhcmRldW0gfCBTSE0iLCJpbWFnZSI6ImRhdGE6aW1hZ2Uvc3ZnK3htbDtiYXNlNjQsUEhOMlp5QjRiV3h1Y3owaWFIUjBjRG92TDNkM2R5NTNNeTV2Y21jdk1qQXdNQzl6ZG1jaUlIZHBaSFJvUFNJeU56QWlJR2hsYVdkb2REMGlNamN3SWlCbWFXeHNQU0p1YjI1bElqNDhjR0YwYUNCbWFXeHNQU0oxY213b0kyRXBJaUJrUFNKTk1DQXdhREkzTUhZeU56QklNSG9pTHo0OFpHVm1jejQ4Wm1sc2RHVnlJR2xrUFNKaUlpQmpiMnh2Y2kxcGJuUmxjbkJ2YkdGMGFXOXVMV1pwYkhSbGNuTTlJbk5TUjBJaUlHWnBiSFJsY2xWdWFYUnpQU0oxYzJWeV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xqWXdNemNnTkRRdU9UazJOQ0EyTUNBME5DNDVPVFkwUXpVeUxqTTVOalFnTkRRdU9UazJOQ0EwTmk0eU16STBJRFV4TGpNMU1UVWdORFl1TWpNeU5DQTFPUzR4T1RBNVF6UTJMakl6TWpRZ05qY3VNRE13TXlBMU1pNHpPVFkwSURjekxqTTROVE1nTmpBZ056TXVNemcxTTFvaUlHWnBiR3c5SW5kb2FYUmxJaTgrUEhOMGIzQWdiMlptYzJWMFBTSXhJaUJ6ZEc5d0xXTnZiRzl5UFNJak1HTmtOMlUwSWlCemRHOXdMVzl3WVdOcGRIazlJaTQ1T1NJdlBqd3ZiR2x1WldGeVIzSmhaR2xsYm5RK1BDOWtaV1p6UGp4MFpYaDBJSGc5SWpNeUxqVWlJSGs5SWpJek1TSWdabTl1ZEMxemFYcGxQU0l5TnlJZ1ptbHNiRDBpSTJabVppSWdabWxzZEdWeVBTSjFjbXdvSTJJcElpQm1iMjUwTFdaaGJXbHNlVDBpVUd4MWN5QktZV3RoY25SaElGTmhibk1zUkdWcVlWWjFJRk5oYm5Nc1RtOTBieUJEYjJ4dmNpQkZiVzlxYVN4QmNIQnNaU0JEYjJ4dmNpQkZiVzlxYVN4ellXNXpMWE5sY21sbUlpQm1iMjUwTFhkbGFXZG9kRDBpWW05c1pDSStZMjl1ZEhKaFkzUXVjMmh0UEM5MFpYaDBQand2YzNablBnPT0iLCJsZW5ndGgiOiI4In0=--------------------------------------------------------0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 has registered a domainMinted domain contract.shmowner of contract domain : 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Copy the Final tokenURI and paste in browser new tab which will look like this:
Now copy the data as selection shown above from the json and paste in new browser tab. This is how it may appear: