Secret Tokens (SNIP-20)
Learn how to create a SNIP-20 token on Secret Network
Introduction
In this tutorial, we are going to create our own SNIP-20 token on Secret Network using Secret Labs' SNIP-20 reference implementation contract, and we will learn how to upload, instantiate, execute, and query our SNIP-20 contract using Secret.js. Let's dive in!
Source Code
You can clone the source code here, which we will reference throughout the course of this documentation.
Prerequisites
Use the following guide to set up your developer environment.
Build and Deploy Contract
Now that you've cloned the SNIP-20 reference implementation repo above, let's compile the contract. In your terminal run make compile-optimized
.
In Rust, a Makefile can be used to automate tasks such as building the project, running tests, or even generating documentation. Make compile-optimized
is running the following optimizer command, which you can view in the Makefile:
Optimizer command
Configuring Secret.js
In your root project folder, create a new folder called
node.
In your
node
folder, create a new javascript file calledindex.js
.Run
npm init -y
to create a package.json file.Add
"type" : "module"
to your package.json file.Install secret.js:
npm i secretjs
Uploading the SNIP-20 Contract
In your index.js file, paste the following (be sure to replace the wallet seed phrase with your wallet seed phrase):
Run node index.js
in your terminal to execute the upload_contract()
function. Upon successful execution, a codeId and contract hash will be returned:
Instantiating the SNIP-20 Contract
In your index.js file, paste the following:
The initMsg
object in our index.js
file is referencing the instantiation message defined in msg.rs at line 20. Notice that we chose to omit the optional config
variable. If we include config
, there is a variety of additional contract functionality that we could program, such as burn, mint, admin privileges, etc as seen here.
Now we are going to instantiate some ZBRA coin. If you want to create your own coin name, update the name, symbol,
and amount
fields respectively. Be sure to comment out upload_contract()
and now run node index.js
to call instantiate_contract()
. Upon successful execution, a contract address will be returned:
Query the Token Info
To check that the instantiation of our SNIP-20 ZEBRA token was successful, let's query the smart contract's token info:
The following is returned upon successful query:
The reason total supply
is null
is because we chose to make total supply
hidden in our instantiation message. If you want it to be public, then in the InitConfig
variable set public_total_supply
to true.
SNIP-20 Contract Messages
Now that we have successfully instantiated our SNIP-20 contract, let's send an execution message to better understand the contract's functionality.
Start by adding the token to your Keplr wallet. Click on Keplr, select the hamburger icon, select "Add Token", and then paste in your token's contract address. If you need to fund your wallet to execute the transaction, you can do so using the pulsar-3 faucet here. You should now see your token in your Keplr wallet!
Let's transfer some tokens to another wallet address. The transfer message is defined in msg.rs as follows:
Now let's execute the transfer message with secret.js. Be sure to update the recipient
wallet address with your own wallet before executing the code below. For testing purposes, I am using two Keplr wallet connected to the Secret Network testnet in order to move funds back and forth:
Congrats! You just successfully transferred your own SNIP-20 token on Secret Network! 🎉
Last updated