
Third part of this series:
If you have an environment set up as per part 2 of this series, now it is time to start programming in Rust.
The project I chose requires the creation of affordable non-fungible tokens.
After a week long research for a blockchain where I can create smart contracts in Rust, I finally settle for NEAR Protocol, as other blockchains either don’t support Rust (Cardano), the NFT metadata is too complex to set up (Solana), you can do anything but I can’t figure out how to do one simple thing (Substrate), or the NFTs are too costly (Ethereum).
A surprise I had when chasing this affordable NFT solution is that you can’t just create NFTs, first you have to create your own “NFT minting machine”, a smart contract. So it is way more involved that I anticipated.
If you are up for learning to create a smart contract in Rust to mint non-fungible tokens, this is how to do it:
For testing purposes we need Node.js, we can install it using in the test environment with:
login YOURUSERACCOUNTIDsudo curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bashsudo apt-get install -y nodejs
We need to check that we have version 16 or above of Node.js
node -v
We also need yarn for building the smart contract
sudo apt-get update && sudo apt-get install yarncurl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/nullecho “deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main” | sudo tee /etc/apt/sources.list.d/yarn.list
The target is Webassembly so we need to install it for Rust
rustup target add wasm32-unknown-unknown
Next, create a test wallet in NEAR Protocol, choosing a name for your account; YOURNEARPROTOCOLACCOUNTID.testnet
Install NEAR-CLI in the test environment
sudo npm install -g npm@8.19.1sudo npm install -g near-clihttps://docs.near.org/tools/near-cli#setupwe can login to near
Log in to NEAR Protocol Testnet
Login to near testnet from the test environment using:
near login
Copy and paste the generated URL to a browser. You need to add the “s” and the IP of the computer where the browser is:
https://wallet.testnet.near.org/login/?referrer=NEAR+CLI&public_key=ed25519THISISSTRINGGENERATEDWHENYOURUNNEARLOGINsuccess_url=https%3A%2F%2FIPOFTHEBROWSERWHEREYOUVISITTHEURL%3A5000
Approve the log in from the browser.
Note: You will have to do this quite a few times. When running tests you will find that smart contracts keep their state in the blockchain. You could call the total opposite of idempotent, you can’t run for test purposes a smart contract more than once ignoring previous runs. So you may need a test account per set of tests.
Add to .bashrc the following:
export NEARID=YOURNEARPROTOCOLACCOUNTID.testnetexport NFT_CONTRACT_ID="YOURNEARPROTOCOLACCOUNTID.testnet"
logout and login (your computer session, not your NEAR session)
Code our smart contract
Next we need to download the NFT tutorial code with
git clone https://github.com/near-examples/nft-tutorial/
And switch to the branch used in the tutorial
cd ~/nft-tutorial
sudo git switch 1.skeleton
git checkout 1.skeleton
Make changes and updates according to the following tutorial, adding any custom changes for your own purposes:
Iron out any compile warnings that you probably will find.
Build the smart contract
When ready from the directory where cargo.toml is run:
yarn build
Deploy the smart contract
From the directory where you can see there is a “out” subdirectory running the following command:
near deploy --wasmFile out/main.wasm --accountId $NFT_CONTRACT_ID
Verify that the correct account ID is printed in the terminal
Initialize the contract
near call $NFT_CONTRACT_ID new_default_meta '{"owner_id": "'$NFT_CONTRACT_ID'"}' --accountId $NFT_CONTRACT_ID
Mint the non-fungible token
near call $NFT_CONTRACT_ID nft_mint '{"token_id": "token-1", "metadata": {"title": "My Non Fungible Team Token", "description": "The Team Most Certainly Goes :)", "media": "https://bafybeiftczwrtyr3k7a2k4vutd3amkwsmaqyhrdzlhvpt33dyjivufqusq.ipfs.dweb.link/goteam-gif.gif"}, "receiver_id": "'$NFT_CONTRACT_ID'"}' --accountId $NFT_CONTRACT_ID --amount 0.1
View the non-fungible token
near view $NFT_CONTRACT_ID nft_token '{"token_id": "token-1"}'near view $NFT_CONTRACT_ID nft_metadata
Success! A smart contract written in Rust that can be used to mint affordable non-fungible tokens.