How to Create CW20 on an Ignite chain
This tutorial guides you through the process of creating, compiling, uploading, and transacting a CW20 token named "utitan" (ticker: TITAN) with 6 decimal places, using the Ignite CLI and the Wasm app.
Prerequisites
-
Ensure Ignite v28.3.0 or later is installed.
ignite version
-
Install Docker on your machine.
-
If not using an existing chain, scaffold a new chain:
ignite scaffold chain wasmapp --no-module
Your chain must be scaffolded with v28.2.1 or later. If not, follow the migration guide to upgrade your chain.
If you're creating a new chain with a name previously used in your Ignite environment, ensure to delete the old home directory at
~/.wasmapp
.
How to Install the Ignite Wasm App
The Ignite Wasm App is an application developed to extend the Ignite CLI, enabling developers to integrate CosmWasm smart contracts into their blockchain projects with ease. The Wasm App supports commands for adding and configuring CosmWasm support within your chain.
Installation
Before starting, ensure Ignite v28.2.1 or later is installed.
- Install the Wasm app:
ignite app install -g github.com/ignite/apps/wasm
-
Scaffold or migrate your chain: Your chain must be scaffolded with v28.2.1 or later. If not, follow the migration guide to upgrade your chain.
-
Add Wasm support: Navigate to your chain's directory and execute the following command to add Wasm support:
ignite wasm add
This command integrates Wasm into your chain's code and configuration. If your chain configuration does not exist yet (for non-initiated chains), you'll need to add the Wasm configuration manually:
ignite wasm config
Remember, all commands should be executed within your chain directory.
- Develop and test: Make changes to the app code as needed. Then, run
ignite chain serve
to recompile the app and test your changes.
At this point, your chain is now ready to develop and deploy Wasm smart contracts, enabling you to create and manage a CW20 token.
CW20 Token Development
We will use the CosmWasm/cw-plus
repository for production-ready Wasm contracts. These contracts work similarly to what OpenZeppelin offers for Ethereum.
Step 1: Build the CW20 Contract
Start by cloning the repository and navigate to its root:
git clone https://github.com/CosmWasm/cw-plus.git
cd cw-plus
The
cw-plus
repository is organized into various packages, with template contracts located under thecontracts/
directory. For this tutorial, we're interested in thecw20-base
contract, which provides a basic implementation of a CW20 token.
Step 2: Compile the CW20 Base Contract
Ensure Docker is running. Then execute the following command from the root of the cw-plus
repository:
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer:0.13.0
This command compiles all contracts in the repository. The output, including the
cw20-base
contract, will be in theartifacts/
directory.
Step 3: Deploy the Contract
With the contract compiled, the next steps involve deploying it to your chain.
-
First, upload the compiled
cw20-base
contract by executing the following transaction:wasmappd tx wasm store artifacts/cw20_base.wasm --from alice --chain-id wasmapp -y
Next, list all existing contracts and take note of the code ID:
wasmappd query wasm list-code
-
Use the code ID to instantiate the contract with its initial configuration:
Replace the placeholders with appropriate values:
<code-id>
,--from alice
,--chain-id wasmapp
, and<your-address>
.wasmappd tx wasm instantiate <code-id> '{"name":"utitan", "symbol":"TITAN", "decimals":6, "initial_balances":[{"address":"<your-address>", "amount":"1000000000000"}]}' --label "utitan" --from alice --chain-id wasmapp --no-admin -y
Step 4: Construct Transactions
To execute the contract, you'll need the contract address. Retrieve it using:
wasmappd query wasm list-contract-by-code <code-id>
With the contract instantiated, you can now construct and execute transactions. Here is an example command to transfer tokens between addresses:
wasmappd tx wasm execute <contract-address> '{"transfer":{"recipient":"<recipient-address>","amount":"10000"}}' --from alice --chain-id wasmapp -y
Step 5: Check Balances
Verify the success of your transfer and ensure the tokens have been successfully transferred:
wasmappd q wasm contract-state smart <contract-address> '{ "balance": { "address": "<recipient-address>" } }'
Conclusion
Congratulations! You've successfully compiled, uploaded, and made a transaction with the CW20 token "utitan" using Ignite CLI and the Wasm app. Explore further by adding more features to your token contract or integrating it with other contracts on your chain.