CosmWasm 101 - The Entry Point

Photo by Esther Jiao on Unsplash

CosmWasm 101 - The Entry Point

ยท

3 min read

CosmWasm is a powerful smart contract platform designed for the Cosmos SDK that allows developers to build efficient, secure, and scalable blockchain applications. With its focus on simplicity, ease of use, and flexibility, CosmWasm has quickly become one of the most popular choices for smart contract development on the Cosmos Network.

In this blog post, we'll closely examine the basics of building CosmWasm contracts.


I assume the readers are familiar with the rust language, If you are unfamiliar with the rust language, please refer to my series on it.

Prerequisite

  • Install rust

      curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  • Install wasmd binary

      $ git clone git@github.com:CosmWasm/wasmd.git
      $ cd ./wasmd
      $ make install
    
  • Add wasm target

      rustup target add wasm32-unknown-unknow
    

Getting Started

Create a new rust library project. As smart contracts are rust libraries.

cargo new cw-simple-counter --lib

Update Cargo.toml to add dependencies

[package]
name = "cw-simple-counter"
version = "0.1.0"
edition = "2021"

[dependencies]
cosmwasm-std = {version = "1.2.2",features=["iterator"]}

The CosmWasm standard library will include all the structs, enums, functions, and macros that can be used in CosmWasm contracts. See the crate documentation for more information about this library.

Defining Entry Points

Rust binaries will have a single entry point, fn main(). In contrast to native binaries, smart contracts will have multiple entry points.

Based on the message passed to them, smart contracts will have multiple entry points. There are several entry points in cosmwasm-based contracts, including instantiate, execute, query and reply.

  • instantiate will perform the contract initialization and used only once

  • execute will handle all actions that need to be performed on the contract which changes the state of the contracts.

  • query used to get information from the contract but it does not do any state change.

  • reply handles the response generated from the sub-message execution.

Let's add these entry points to our lib.rs file.

use cosmwasm_std::{
    entry_point, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdResult,
};

#[entry_point]
pub fn instantiate(
    _deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    _msg: Empty,
) -> StdResult<Response> {
    unimplemented!()
}

#[entry_point]
pub fn execute(_deps: DepsMut, _env: Env, _info: MessageInfo, _msg: Empty) -> StdResult<Response> {
    unimplemented!()
}

#[entry_point]
pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult<Binary> {
    unimplemented!()
}

#[entry_point]
pub fn reply(_deps: DepsMut, _env: Env, _msg: Reply) -> StdResult<Response> {
    unimplemented!()
}

Argument Definitions

  • DepsMut and Deps - is a utility type for communicating with the outer world - it allows querying and updating the contract state, querying other contracts state, and gives access to an Api object with a couple of helper functions for dealing with CW addresses.

  • Env - is an object representing the blockchains state when executing the message - the chain height and id, current timestamp, and the called contract address.

  • MessageInfo - contains metainformation about the message which triggered an execution - an address that sends the message, and chain native tokens sent with the message.

  • Empty - the type that represents {} JSON, but the type of this argument can be anything that can be deserialized

  • Reply - Which defines the Id and SubMessage execution result

๐Ÿ’ก Summary

In this post, we have covered the prerequisite and setting up the cosmwasm contracts. Query & Execution can be done on smart contracts using the Entry points. The source code for this post can be found on GitHub. The following part of this series will focus on creating messages for query, execution, and initialization.

Did you find this article valuable?

Support The Missing Semicolon by becoming a sponsor. Any amount is appreciated!

ย