NFTs are individual tokens with valuable information stored in them. Because they hold a value primarily set by the market and demand, they can be bought and sold just like other physical types of art. NFTs’ immutable data stored on technologies like blockchain makes it easy to assign/validate its ownership and transfer tokens between two parties transparently without any intermediary.
NFT is a virtual digital asset that is typically unique and scarce similar to a Mona Lisa painting. An NFT is unique evidence of ownership of a virtual digital asset. You can read more about the use cases of NFTs like ‘NFT for Causes’ and ‘Private Club Memberships’ here. And smart contracts are one of the best use cases/innovations of blockchain technology. It is a virtual settlement between two parties enforced through a computer program (code) instead of a third party. You can find more on smart contracts and their deployment on Shardeum using Hardhat here, which would have similar coding as below since we will be using Hardhat for this documentation of deploying an NFT smart contract.
First, we’ll need to create a folder for our project. Navigate to your command line and type the following commands.
mkdir shardeum-nft-dappcd shardeum-nft-dapp
Now that we’re inside our project folder, we’ll use ‘npm init’ to initialize the project. If you don’t have npm installed already, download from here Node.
npm init
This command will create a package.json file. It doesn’t really matter how you answer the installation questions, here is how we did it for reference.
package name: (shardeum-nft-dapp)version: (1.0.0)description:entry point: (index.js)test command:git repository:keywords:author:license: (ISC)About to write to G:shardeum-nft-dapppackage.json:{ "name": "shardeum-nft-dapp", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC"}Is this OK? (yes) yes
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers when building smart contracts and dApps locally before deploying to the live chain.
Open up the shardeum-nft-dapp project in your favorite 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 the nft.shardeum.us that we will be using for this tutorial. Copy and paste in the contents below into your Domains.sol file.
// SPDX-License-Identifier: MITpragma solidity 0.8.17;import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";import "@openzeppelin/contracts/utils/Counters.sol";import "@openzeppelin/contracts/utils/Base64.sol";// import "hardhat/console.sol";contract Domains is ERC721URIStorage { error Unauthorized(); error AlreadyRegistered(); error InvalidName(string name); using Counters for Counters.Counter; Counters.Counter private _tokenIds; string public tld; mapping(string => address) public domains; mapping(string => string) public records; mapping(uint => 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><svg x="15" y="15" width="120" height="108" viewBox="0 0 120 108" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M29.4358 77.2888L16.7213 100H103.279L90.5643 77.2888H29.4358Z" fill="white"/><path d="M60 22.7112L47.2856 0L4 77.2889H29.4358L60 22.7112Z" fill="white"/><path d="M90.5642 77.2889H116L72.7145 -3.05176e-05L60 22.7111L90.5642 77.2889Z" fill="white"/><path d="M60 73.3853C67.6037 73.3853 73.7677 67.0303 73.7677 59.1909C73.7677 51.3515 67.6037 44.9964 60 44.9964C52.3964 44.9964 46.2324 51.3515 46.2324 59.1909C46.2324 67.0303 52.3964 73.3853 60 73.3853Z" fill="white"/></svg><defs><linearGradient id="a" x1="0" y1="0" x2="270" y2="270" gradientUnits="userSpaceOnUse"><stop stop-color="#cb5eee"/><stop offset="1" stop-color="#0cd7e4" stop-opacity=".99"/></linearGradient></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 user name NFT on shardeum | SHM", "Web3 User Name") payable { owner = payable(msg.sender); tld = _tld; // console.log("%s name services deployed", _tld); } function price(string calldata name) public pure returns(uint){ uint len = StringUtils.strlen(name); require(len > 2); if(len == 3){ return 90 * 10**17; }else if(len == 4){ return 50 * 10**17; }else if(len == 5){ return 30 * 10**17; }else{ return 10 * 10**17; } } function registers(string calldata name) public payable { // require(domains[name] == address(0)); if(domains[name] != address(0)) revert AlreadyRegistered(); if(!valid(name)) revert InvalidName(name); uint _price = price(name); require(msg.value >= _price, "not enough SHM paid"); string memory _name = string(abi.encodePacked(name, ".", tld)); // console.log("_name", _name); string memory finalSvg = string(abi.encodePacked(svgPartOne, _name, svgPartTwo)); uint256 newRecordId = _tokenIds.current(); uint256 length = StringUtils.strlen(name); string memory strLen = Strings.toString(length); // console.log("Registering %s on the contract with tokenId %d", name, newRecordId); string memory json = Base64.encode( bytes( string( abi.encodePacked( '{"name":"', _name, '","description":"Web3 user name NFT on shardeum | SHM","image":"data:image/svg+xml;base64,', Base64.encode(bytes(finalSvg)), '","length":"', strLen, '"}' ) ) ) ); string memory finalTokenUri = string(abi.encodePacked("data:application/json;base64,", json)); // console.log("\n--------------------------------------------------------"); // console.log("Final tokenURI", finalTokenUri); // console.log("--------------------------------------------------------\n"); _safeMint(msg.sender, newRecordId); _setTokenURI(newRecordId, finalTokenUri); domains[name] = msg.sender; // console.log("%s has registered a domain", msg.sender); names[newRecordId] = name; _tokenIds.increment(); } function getAddress(string calldata name) public view returns (address){ return domains[name]; } function setRecord(string calldata name ,string calldata record) public { // require(domains[name] == msg.sender); if(msg.sender != domains[name]) revert Unauthorized(); records[name] = record; } function getRecord(string calldata name) public view returns(string memory){ return records[name]; } modifier onlyOwner(){ require(isOwner()); _; } function isOwner() public view returns (bool){ return msg.sender == owner; } function withdraw() public onlyOwner{ uint amount = address(this).balance; (bool success, ) = msg.sender.call{value: amount}(""); require(success, "failed to withdraw SHM"); } function getAllNames() public view returns(string[] memory){ string[] memory allNames = new string[](_tokenIds.current()); for(uint i = 0; i < _tokenIds.current(); i++){ // console.log("iteration i names[i] ", names[i]); // console.log("iteration i allNames[i] ", allNames[i]); allNames[i] = names[i]; } return allNames; } function valid(string calldata name) public pure returns(bool){ return StringUtils.strlen(name) >= 3 && StringUtils.strlen(name) <= 10; }}library StringUtils { // Source: ENS contracts: //https://github.com/ensdomains/ens-contracts/blob/master/contracts/ethregistrar/StringUtils.sol function strlen(string memory s) internal pure returns (uint256) { uint256 len; uint256 i = 0; uint256 bytelength = bytes(s).length; for (len = 0; i < bytelength; len++) { 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; } } return len; }}
This is the smart contract that creates Shardeum .shm Domains by calling registers function with your desired name.
We used 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.
npm install @openzeppelin/contracts
Now that we have created smart contract, we need to deploy this smart contract to the Shardeum Sphinx
MetaMask allows users to store and manage account keys, broadcast transactions, send and receive Ethereum-based cryptocurrencies and tokens, and securely connect to decentralized applications through a compatible web browser or the mobile app’s built-in browser. Click here to install the MetaMask extension on your browser.
And follow these instructions to add Shardeum to MetaMask wallet and claim test 100 $SHM tokens from Sphinx faucet.
We’ve created a MetaMask wallet and written our smart contract, and now it’s time to connect these two!
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:
npm install dotenv --save
Note:
.env- Your environment file must be named .env or it won’t be recognized as an environment file. Do not name it process.env or .env-custom or anything else.
Ethers.js is a library that makes it easier to interact and make requests to Ethereum by wrapping standard JSON-RPC methods with more user friendly methods.
Hardhat makes it super easy to integrate Plugins for additional tooling and extended functionality. We’ll be taking advantage of the Ethers plugin for contract deployment (Ethers.js has some super clean contract deployment methods).
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.
Before we deploy our smart contract to Shardeum network, lets test this once in local hardhat environment to check what a generated Shardeum NFT looks like:
create a test-deploy.js file in scripts/ and copy paste the below code:
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,eyJuYW1lIjoiY29udHJhY3Quc2htIiwiZGVzY3JpcHRpb24iOiJXZWIzIHVzZXIgbmFtZSBORlQgb24gc2hhcmRldW0gfCBTSE0iLCJpbWFnZSI6ImRhdGE6aW1hZ2Uvc3ZnK3htbDtiYXNlNjQsUEhOMlp5QjRiV3h1Y3owaWFIUjBjRG92TDNkM2R5NTNNeTV2Y21jdk1qQXdNQzl6ZG1jaUlIZHBaSFJvUFNJeU56QWlJR2hsYVdkb2REMGlNamN3SWlCbWFXeHNQU0p1YjI1bElqNDhjR0YwYUNCbWFXeHNQU0oxY213b0kyRXBJaUJrUFNKTk1DQXdhREkzTUhZeU56QklNSG9pTHo0OFpHVm1jejQ4Wm1sc2RHVnlJR2xrUFNKaUlpQmpiMnh2Y2kxcGJuUmxjbkJ2YkdGMGFXOXVMV1pwYkhSbGNuTTlJbk5TUjBJaUlHWnBiSFJsY2xWdWFYUnpQU0oxYzJWeVUzQmhZMlZQYmxWelpTSWdhR1ZwWjJoMFBTSXlOekFpSUhkcFpIUm9QU0l5TnpBaVBqeG1aVVJ5YjNCVGFHRmtiM2NnWkhnOUlqQWlJR1I1UFNJeElpQnpkR1JFWlhacFlYUnBiMjQ5SWpJaUlHWnNiMjlrTFc5d1lXTnBkSGs5SWk0eU1qVWlJSGRwWkhSb1BTSXlNREFsSWlCb1pXbG5hSFE5SWpJd01DVWlMejQ4TDJacGJIUmxjajQ4TDJSbFpuTStQSE4yWnlCNFBTSXhOU0lnZVQwaU1UVWlJSGRwWkhSb1BTSXhNakFpSUdobGFXZG9kRDBpTVRBNElpQjJhV1YzUW05NFBTSXdJREFnTVRJd0lER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E05SW5WelpYSlRjR0ZqWlU5dVZYTmxJajQ4YzNSdmNDQnpkRzl3TFdOdmJHOXlQU0lqWTJJMVpXVmxJaTgrUEhOMGIzQWdiMlptYzJWMFBTSXhJaUJ6ZEc5d0xXTnZiRzl5UFNJak1HTmtOMlUwSWlCemRHOXdMVzl3WVdOcGRIazlJaTQ1T1NJdlBqd3ZiR2x1WldGeVIzSmhaR2xsYm5RK1BDOWtaV1p6UGp4MFpYaDBJSGc5SWpNeUxqVWlJSGs5SWpJek1TSWdabTl1ZEMxemFYcGxQU0l5TnlJZ1ptbHNiRDBpSTJabVppSWdabWxzZEdWeVBTSjFjbXdvSTJJcElpQm1iMjUwTFdaaGJXbHNlVDBpVUd4MWN5QktZV3RoY25SaElGTmhibk1zUkdWcVlWWjFJRk5oYm5Nc1RtOTBieUJEYjJ4dmNpQkZiVzlxYVN4QmNIQnNaU0JEYjJ4dmNpQkZiVzlxYVN4ellXNXpMWE5sY21sbUlpQm1iMjUwTFhkbGFXZG9kRDBpWW05c1pDSStZMjl1ZEhKaFkzUXVjMmh0UEM5MFpYaDBQand2YzNablBnPT0iLCJsZW5ndGgiOiI4In0=--------------------------------------------------------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: