CW20 smart contract in action

Everything is ready. Gearwheels(rust, wasm) are greased, testnets are up. Now it’s time to see some action.

You must be asking what is this gif?

It takes only 24 seconds.

This is how long it takes to create your own CW20(ERC20 on CosmWasm brand steroids) token from zero. It only requires a node REPL import and some keyboard typing. Really that’s all.

Let’s walk through you through at a slower pace.

This tiny, cute command up there does a lot more compared to its size. This is the reason the GIF up there takes 24 seconds not hours.

npx @cosmjs/cli command downloads the @cosmjs/cli package from the register, downloads dependencies, and launches a Node REPL that imports this package.

--init downloads and imports the helper that contains some code that makesCW20 contracts and some testnet configuration bindings for fast bootstrapping. Go ahead and see it yourself.

After running the command, you will see a REPL console that contains everything you need to use the smart contract.

After developing a smart contract, you can provide a helper.ts file like above to make your and users’ lives easier.

First of all, we need a client to deploy, initialize, and execute contracts. Here is the code:

It simply decrypts the private key if you created one before if not creates one, initializes a SigningCosmWasmClient with network fee info for various operations, and private key, then points it to network endpoints.

Deploy the Contract Code

Here is a demonstration of the deployment of the CW20 smart contract code.

Note: You don’t have to run the command below since the smart contract code is already deployed.

Smart contract code is a bytecode that contains processing logic instructions. Smart Contract is a working instance of the code on the chain.

You can initiate an infinite amount of contract from code if chain authorization permissions allow it.

The command above uploads precompiled cw20 to the chain.

Instantiate a Smart Contract

Step by step:

  1. Get address of the wallet

2. Wrap wallet with CW20 helper

3. Form an init message containing token information: denom, decimals, initial balances, and set minter address as wallet address to authorize the wallet to mint afterward

4. Derive and init a smart contract labeled “DIZZ” from a code that lives on the chain under the ID “4”, feed it with the init message

The last line will give you the recently created smart contract’s address.

Query the Smart Contract

You can query the contract using commands below:

Execute the Smart Contract

Let’s see how to execute operations on the smart

In this article, we summarized basic CW20 execution. As you can see and I mentioned several times, dev UX is very straight forward and intuitive.

Of course, this is not the upper limit of CosmWasm’s capabilities. For more advanced usage details, you can go through Using Contracts section which this article is based on.

If all these pearls interests you, the next step is checking out documentation: https://docs.cosmwasm.com

If you want to ask questions and discuss, join us on discord.

Happy coding and contracting!

Software crafter. Bedroom electronic music producer.