In the previous sections, we have covered setting up the development environment, configuring your wallet, and obtaining testnet OM tokens for development purposes. We have also walked through the steps for writing and deploying CW20 contracts on MANTRA Chain. Now, we will shift our focus to writing NFT CosmWasm contracts.
Overview
Before diving into the steps of creating an NFT on MANTRA Chain, let's understand the contract structure and flow:
CW-721 Base Contract: All NFT data is stored and instantiated here. This contract handles the creation and management of individual NFTs.
CW-721 Factory Contract: Facilitates the creation of multiple NFT instances. Deployed once, it handles the heavy lifting for NFT creation.
Prerequisites
If you encounter any issues with your development environment setup or need to refresh your setup, follow these steps:
Install Rust
First, you need to install Rust, a programming language used for developing smart contracts. You can do this by running the following command in your terminal:
Download and extract the pre-built mantrachaind binary:
On Linux:
# Download the CLIcurl-LO<https://github.com/MANTRA-Finance/public/raw/main/mantrachain-testnet/mantrachaind-linux-amd64.zip># Unzip the CLIunzipmantrachaind-linux-amd64.zip
On MacOS:
# Download the CLI for Intel chipscurl -LO <https://github.com/MANTRA-Finance/public/raw/main/mantrachain-hongbai/mantrachaind-static-darwin-amd64.tar.gz>
# Download the CLI for Silicon chips (M1, M2...)curl -LO <https://github.com/MANTRA-Finance/public/raw/main/mantrachain-hongbai/mantrachaind-static-darwin-arm64.tar.gz>
# Extract the CLItar-xzvfmantrachaind-static-darwin-*.tar.gz
If you encounter a missing libwasmvm.x86_64.so error, download the library:
For more details on setting up the development environment, check the section Install Prerequisites.
Getting Started with NFT CosmWasm Contract
Now that your development environment is set up, let's begin with a boilerplate codebase for NFT CosmWasm contracts. It contains a starting template for CW721 NFT contracts tailored for deployment on MANTRA Chain.
Use any your preferred code editor, such as Visual Studio Code. Open its terminal,then clone the following repository:
When deploying contracts on the chain, they must be instantiated. This step involves initializing the contract's state and linking it with a CW721 contract to handle NFTs. You will create a Config struct to store the necessary information and define the logic for submessages and handling replies. This ensures that our contract can interact seamlessly with the CW721 contract.
The following code should be added to src/contract.rs to handle the instantiation and reply logic. The instantiate function sets up the initial configuration and sends a submessage to instantiate the CW721 contract. The reply function processes the response from this submessage, linking the CW721 contract to our main contract.
Next, integrate the execution logic into src/contract.rs, below the handling replies contract code. This includes the execute function, which handles incoming messages, and the execute_receive function, which processes transactions and mints NFTs.
Here, the execute function dispatches incoming messages to the appropriate handler. The execute_mint function accepts a native coin, checks it against the configuration set on instantiation, and if the conditions for minting a NFT are right, sends a mint message to the linked CW721 contract. The callback used here is a CosmosMsg message to trigger the mint function on the CW721 contract linked to our contract after instantiation.
Error Handling Logic
In this step, you will delete the src/helpers.rs file from our project setup. You will also create an errors.rs file in the src folder if it doesn't already exist. This file will handle the error logic for running our contracts.
Create the src/errors.rs file and add the following code:
Querying is an essential part of contract interactions, allowing you to retrieve data stored on the blockchain. In this step, we'll add the necessary query logic to our contract.
In this code, the query function handles incoming query messages and dispatches them to the appropriate handler. The query_config function retrieves the contract's configuration data from storage and returns it as a ConfigResponse.
You have successfully written a complete NFT CosmWasm contract . This contract can be deployed on MANTRA Chain or any other Cosmos-based chain. It includes functionalities for initialization, execution, and querying, enabling the minting and management of NFTs.
Make sure to thoroughly test your contract on a testnet before deploying it on the Hongbai Testnet. Customize the contract further to meet your specific requirements and ensure it fits your project's needs.
Step 4: Build Artifacts and Wasm file
To build the artifact, we need to run the rust-optimizer with docker:
Once we get the code_id after storing the contract on chain, we can create an instance of the contract like this:
MSG='{ "owner": "", // add your wallet "max_tokens": 10000, "unit_price": { "denom": "uom", "amount": "1000" }, "name": "Mantra NFT", "symbol": "MANTRANFT", "token_code_id": 507, // the code id of a cw721_base contract "token_uri": "<https://ipfs.io/ipfs/QmZ>"}'mantrachaind tx wasm instantiate <code_id> "$MSG" --from <wallet> --node <https://rpc.hongbai.mantrachain.io:443> --chain-id mantra-hongbai-1 --label "MANTRAcw20" --no-admin --gas-prices 0.35uom --gas auto --gas-adjustment 1.4 -y --output json
Note that the token_code_id value on the InstantiateMsg needs to be a valid cw721_base contract code_id. Feel free to compile and deploy your own from the cw-nfts repo, but you can reuse the code_id 507 which is one we have deployed on Hongbai Testnet.
Once deployed, you will receive the wallet address and confirmation of contract instantiation. Verify your deployment on the MANTRA Chain Explorer athttp://explorer.hongbai.mantrachain.io.