This Blog is a tutorial on "How to Create your First token on Stacks" . We will learn more about tokens on Stacks , Setting up a Dev Environment for creating the Smart Contract , Create a token , deploy them on Blockchain and even test them.
So let's get right inn -->
PreRequisites
- Introduction about Stacks Chain
- HIRO Wallet Setup
- STX faucets
- Knowledge about Clarity
I would highly recommend to go through this Blog by Sahil Aujla first , before starting this tutorial for a introduction to Stacks Ecosystem.
What is Stack Blockchain
Stacks is a Layer-1 Blockchain that brings the power of Smart Contracts and dApps to Bitcoin , they are integrated to the security of Bitcoin. STX is the native token for Stacks blockchain and is used to pay for the gas fees. Clarity is the language for Smart Contracts on Stack . We use HIRO as the wallet for interacting with Stacks Chain .
For more Info on Stacks , check here
Tokens
A fundamental use of blockchain technology is the representation, store, and transfer of value between users of a blockchain. Cryptocurrency is a very common use of blockchain technology .
A blockchain token is a digital asset that can be verifiably owned by a user of a blockchain. Blockchain tokens are governed by a set of rules that are defined by either the blockchain itself (in the case of native tokens) or by a smart contract on a blockchain.
Traits
Before , we start diving deep into each type of tokens , we need to know about Traits in clarity .
Traits are a bit like templates for smart contracts. They are a collection of public function definitions that describe names, input types, and output types .The purpose of a trait is to define a public interface to which a contract can conform either implicitly or explicitly. It works similar to an interface in Solidity .
Basically, if there is a trait my-trait
defined in a contract my-contract
that is deployed on mainnet, another contract my-implementation
can point to my-contract.my-trait
. Traits are integral for dynamic inter-contract calls.
Defining a Trait
Traits are defined using the define-trait function. It takes a trait name as a parameter followed by a series of function signatures in short form , we just include the function name, input types and output types .
(define-trait my-trait
(
(function-name-1 (param-types-1) response-type-1)
(function-name-2 (param-types-2) response-type-2)
;; And so on...
)
)
Implementing and Asserting a Trait
Trait conformance is just a fancy way of saying that a specific smart contract implements the functions defined in the trait. For example ,
;; Trait defined
(define-trait multiplier
(
(multiply (uint uint) (response uint uint))
)
)
;; Trait Implemented
(define-read-only (multiply (a uint) (b uint))
(ok (* a b))
)
To assert that the contract implements the trait, the impl-trait
function is used.
impl-trait .contract.trait-name
;; for example
impl-trait .multiplier-trait.multiplier
Types of Tokens
Non - Fungible Tokens
Non-fungible tokens (NFTs) are a type of token that are not interchangeable. NFTs have unique traits (usually in the form of attached metadata) that restrict the abillity to replace them with identical tokens. An NFT is a token that is unique, such as a piece of art, or ownership rights to a real-world asset such as a house.
Creating a SIP009-compliant NFT comes down implementing a trait .
(define-trait sip009-nft-trait
(
;; Last token ID, limited to uint range
(get-last-token-id () (response uint uint))
;; URI for metadata associated with the token
(get-token-uri (uint) (response (optional (string-ascii 256)) uint))
;; Owner of a given token identifier
(get-owner (uint) (response (optional principal) uint))
;; Transfer from the sender to a new principal
(transfer (uint principal principal) (response bool uint))
)
)
Fungible Tokens
A fungible token is a token that's mutually interchangable or capable of mutual substitution. In other words, one quantity or part of a fungible token can be replaced by an equal quantity or part of the same fungible token. Fungible tokens are often used to represent real-world fungible assets like currency.
Creating a SIP010-compliant fungible token also comes down implementing a trait.
(define-trait sip010-ft-trait
(
;; Transfer from the caller to a new principal
(transfer (uint principal principal (optional (buff 34))) (response bool uint))
;; the human readable name of the token
(get-name () (response (string-ascii 32) uint))
;; the ticker symbol, or empty if none
(get-symbol () (response (string-ascii 32) uint))
;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token
(get-decimals () (response uint uint))
;; the balance of the passed principal
(get-balance (principal) (response uint uint))
;; the current total supply (which does not need to be a constant)
(get-total-supply () (response uint uint))
;; an optional URI that represents metadata of this token
(get-token-uri () (response (optional (string-utf8 256)) uint))
)
)
Setting up the Dev Environment
Clarity is the language which is used to write Smart Contracts on Stacks Chain . For an intro to Clarity check this . I am assuming , you already have basic knowledge of Clarity and that is what we need for this tutorial .
Please ensure , you have Hiro Wallet Setup , it will be used to interact with STAX blockchain and you have some funds . If not follow the tutorial here
Luckily Clarity has inbuilt functions for both the types of token , so we are gonna use that.
- Head over to clarity.tools , this is where we will write Smart Contract in Clarity and Then deploy it to the testnet.
- Connect Hiro Wallet by clicking on the gate icon in the top , A popup will appear , Select the account .
Once Connected , the window will appear as :
- Clear up all the default Code , The window is divided into 2 parts , Code editor and Terminal that returns the output. Check Reference Image below
Ok So Now , we are good to go to create both types of token.
NOTE
Before we move ahead , I have added both the codes in the github Repo here , do check them out .
Fungible Tokens Standard
The SIP-010 standard is the token Standard on Stacks for fungible types of tokens . These are the default function for SIP-010 types of tokens :
mint
- mint tokens to an Addressburn
- burn tokens from an Addresstransfer
- transfer a token amount from address1 to address2get-name
- Name of the token createdget-symbol
- Symbol of the Tokenget-decimals
- No. of decimals for the tokenget-total-supply
- Total Supply of the tokenget-balance-of
- Balance of token for an Addressget-token-uri
- Token URI associated with the token
We can implement the SIP-010 Trait with impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait
, the Trait contract is already deployed on Stacks.
We create a token using
define-fungible-token token-name
and Boom !!, you just created a token , it's that easy.Now to test it , Functions for fungible token standard are already predefined in clarity . Here is the code example with comments containing all the functions which are predefined :
To deploy the token contract , we will just keep
define-fungible-token token-name
to create the token.Head over to the tool box icon on the top and Select Deploy to tesnet.
A Hiro Wallet Popup will be there to deploy the contract by confirming the tx .
You will get a Tx link of the explorer for the contract. It will be something like this : explorer.stacks.co/txid/0x8165aaf1cf4767ead..
The contract is deployed to ST3MNTK3HEQ8GTSB6SNWQVN84VPKG4D5Q8TS5JD45.contract-68408193608 .
Woohoo !! We just deployed a token contract to Stacks . Let's head-over to NFT
Non Fungible Token Standard
The SIP-009 standard is the token standard for Non fungible type of tokens. Default functions for this type
mint
- Mint a paritcular token to an Addressburn
- Burn a particular token from an Addresstransfer
- Transfer the token from an Addressget-owner
- Get the owner of a particular token
Here too , we can implement the SIP-009 trait ,with impl-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait
, even this trait contract is deployed on Stacks chain.
To Create the token use
define-non-fungible-token tokenName
, and this will create nftTokens for you .Similar to Fungible token , Non-fungible token also have these functions predefined
To deploy this contract , follow similar procedure from step 3 in Fungible token Tutorial . You will get the txId after confirming the tx . Mine is deployed on : explorer.stacks.co/txid/0x8b0561a54ce1c69e7..
For Testing , we will call similar functions and we can see the output in the sidebar
So , We successfully completed NFT too . Great Job !!
Bonus
For all who have come to end of this tutorial , there is a bonus , which is transacting with STX token on Stacks blockchain with Contracts.
STX is the native token of STACKS chain. We have inbuilt clarity Commands as follows:
stx-transfer
- Transfer STX from account 1 to account 2stx-get-balance
- Balance of STX of the accountstx-burn
- Burn the amount STX from an account
We can use this inbuilt functions in other functions directly.
Conclusion
We learnt about what are tokens and It's types : Fungible and Non Fungible . How to write a Smart contract for creating tokens using Clarity , then to test them and eventually deploying them on the blockchain .
Finally , We have created our first Tokens ( Fungible and Non-Fungible ) on Stacks Blockchain . Kudos to you all who completed the tutorial.
If you like the blog , consider giving me on Twitter @0xdhruva . In case of further queries , connect on Twitter .
Links and References
To learn more, refer the links below :