WASM - Create an NFT
This guide is a step-by-step guide to deploy a WebAssembly (WASM) smart contract. As a result, the output is highly optimized, which saves you gas costs.
In this tutorial, we will create a non-fungible token (NFT) and deploy it to our public testnet Dusty.
ERC721 is a standard for representing ownership of non-fungible tokens, that is, where each token is unique such as in real estate or collectibles.

Setup

Substrate Prerequisites

During this part, we will set up a Rust environment with the latest nightlyrelease
1
rustup component add rust-src --toolchain nightly
2
rustup target add wasm32-unknown-unknown --toolchain stable
Copied!

ink! CLI

As a pre-requisite for the tool, you need to install the binaryen package, which is used to optimize the WebAssembly bytecode of the contract. Many package managers have it available nowadays ‒ e.g., it's a package for Debian/Ubuntu, Homebrew, and Arch Linux.
After you've installed the package, execute:
1
cargo install cargo-contract --vers ^0.11 --force --lockedCopy to clipboardErrorCopied
Copied!
You can then use cargo contract --help to start exploring the commands made available to you.
Installation of cargo-contract

Create an ink! contract

We are going to use ink! CLI to generate the files we need for a WASM smart contract project. Make sure you are in your working directory and then run:
re in your working directory and then run:
1
cargo contract new erc721
Copied!
This command will create a new project folder named erc20 which we will explore:
1
cd erc721
Copied!
ink! Contract Project
1
NFToken
2
|
3
+-- lib.rs <-- Contract Source Code
4
|
5
+-- Cargo.toml <-- Rust Dependencies and ink! Configuration
Copied!

Contract Source Code

The ink CLI automatically generates the source code for a standard ink! contract. You can take a sneak peek to have a look at some code. Now, let's add some code. In this guide, we will use Visual Studio Code.
1
code .
Copied!
Template ERC721 contract
You can find our example contract here:
plasmdocs/example/wasm/erc721 at master · PlasmNetwork/plasmdocs
GitHub
Just replace the code in your lib.rs file with the complete code from our template.

Testing the contract

You will see at the bottom of the source code there is a simple test that verifies the contract functionality. We can test this code is functioning as expected using the off-chain test environment that ink! provides.
In your project folder, run:
1
cargo +nightly test
Copied!
To which you should see a successful test completion:
Now that we are feeling confident things are working, we can actually compile this contract to WASM.

Build your contract

Run the following command to compile your smart contract:
1
cargo +nightly contract build
Copied!
Building contract

Deploy your contract

Let's deploy the contract now on Plasm/Shiden or Dusty, our testnet. In this guide, I will use our testnet, 'Dusty'. To deploy your contract go to 'Developer - Contracts'. Click on 'Upload and deploy code'.
Load your contractfile and give your bundle a name. I used 'My NFT'
Upload .contract file
Add PLD to keep your contract alive.
After being deployed you can start mint your NFT. Those are the availble command:
Contract calls.
That's it! Great work. Now you can send mint and send your NFT around.
Last modified 5mo ago