Since Facebook announced it’s rebranding into Meta, Metaverse projects became super hot. In this article I will show you how to create a Metaverse NFT in Solidity on Ethereum .
What is a Metaverse NFT?
Decentraland is one of the most famous metaverse games. In Decentraland, There are 2 kind of tokens:
- The LAND token, used for governance, and to buy virtual lands
- And another token used to represent virtual lands
The LAND token is an ERC20 token, a token standard for non-unique assets And each virtual land is represented as ERC721 token, a token standard for unique assets, also called NFTs.
Coding a Metaverse NFT
We are going to focus on the ERC721 token. We will use small programs called smart contracts to represent a virtual land. And we will use the Solidity programming language.
The easiest way to get started is to use Remix, and online IDE for Solidity.
We first select the correct version of Solidity.
I will walk you through the code of our NFT:
pragma solidity ^0.8.10;
//We important OpenZeppelin, a library too easily create ERC721 tokens.
// The big advantage of OpenZeppelin is that it provides safe code that has been reviewed by the community.
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol';
//
We declare our smart contract, and we make it inherit from ERC721
contract MetaverseNFT is ERC721 {
uint public nextTokenId;
//We call the constructor of ERC721, with the name and symbol of our token.
constructor() ERC721('Land', 'LAND') {}
//Then we override the inherited _baseURI() function to return the base URL of the metadata server.
That’s an off-chain server that returns a JSON object with the attribute of each virtual land, like its position on the map, and the URL to an image representing the land.
And you can find the schema of this json object in the ERC721 standard.
function _baseURI() internal pure override returns(string memory) {
return 'https://https/path/to/metadata.server';
}
//
Then there is the mint function to create new virtual lands
function mint() external payable {
//
We first check that enough ether was sent.
require(msg.value == 1 ether);
//Then we check that we haven’t reached the maximum number of lands yet.
require(nextTokenId < 10000);
//We use the inherited _safeMint() function of ERC721 to create the new land.
_safeMint(msg.sender, nextTokenId);
//And finally we increment the tokenId, for the next time the mint() function is called
nextTokenId++;
}
}
Conclusion
And with this, you have created a metaverse NFT, representing a virtual land on the Blockchain.
Feel free to adapt this NFT for your metaverse, which will have different kinds of virtual assets.
Beside ERC721, there is another token standard called ERC1155, which is a container for multiple ERC721 tokens. It allows to manage all your virtual assets in a single smart contract, which can be very useful for metaverses. And if you want to learn more about it, checkout this video.
Leave a Reply