Skip to content

Module 4 - Basics of CosmWasm

Basic Concepts

  • To Learn the Basics of CosmWasm, Click here. You can Refer CosmWasm Book here.

  • To Learn the Basics of Rust, Click here.

Creating a CosmWasm Project

To Create a CosmWasm Project, Run the following Command -

Terminal window
cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME

This will Create a new CosmWasm Project in the Current Directory.

Project Structure

After Running the above Command, you’ll get the Project Structure as follows -

  • Directory.cargo/
    • config.toml
  • Directory.circleci/
    • config.yml
  • Directory.github/
    • Directoryworkflows/
      • Basic.yml
      • Release.yml
  • Directorysrc/
    • Directorybin/
      • schema.rs
    • contract.rs
    • error.rs
    • helpers.rs
    • integration_tests.rs
    • lib.rs
    • msg.rs
    • state.rs
  • .editorconfig
  • .gitignore
  • Cargo.lock
  • Cargo.toml
  • LICENSE
  • NOTICE
  • ReadME.md

Now, Let’s Understand the Project Structure a bit -

  1. Cargo.toml - This is the Manifest file for Rust’s Package Manager, Cargo. It Defines the Project’s Dependencies, Metadata, and Build Settings.
  2. src/contract.rs - This is the Main file Containing your Smart Contract Logic. It typically includes the Contract’s Entry Points (instantiate, execute, query).
  3. src/msg.rs - Defines the Message Structs for your Contract’s Interface, including InstantiateMsg, ExecuteMsg, and QueryMsg.
  4. src/state.rs - Contains definitions for the Contract’s State, often using Storage Primitives from the cw_storage_plus crate.
  5. src/error.rs - Defines Custom Error Types for your Contract.
  6. src/lib.rs - The Library’s root file, which usually re-exports important items from other modules.
  7. src/bin/schema.rs - Generates JSON Schema files for your Contract’s messages, which can be used for front-end Integration.
  8. tests/ - Directory Containing Integration Tests for your Contract.
  9. examples/schema.rs - An Example file Demonstrating how to use the Contract’s Schema.
  10. .cargo/config - Contains Cargo Configuration, Often used to set the Target for WebAssembly Compilation.
  11. .gitignore - Specifies files that Git should ignore.

Entry Points

Unlike Native applications, which have only a Single Main Entry Point, Smart Contract have a Couple Corresponding to Different Message Types: instantiate, execute, query, sudo, migrate and more.

  • instantiate - function is called when the Contract is first created. It is Used to Initialize the Contract’s State.

  • execute - function is called when the Contract is called to Perform an Action. It is Used to Update the Contract’s State.

  • query - function is called when the Contract is Queried. It is Used to Read the Contract’s State.

Building & Deploying the Contract

After you’ve had the Project Structure Initialized with you, it is time to Compile & Build the Contract. Run the following Command in yout Directory to Build the Smart Contract -

Terminal window
cargo build --target wasm32-unknown-unknown --release

After Building the Smart Contract, you need to Deploy them and in CosmWasm that is being done using the .wasm file you get in the target folder.

Possible Errors you Might Get

For this try Running the Below Command -

Terminal window
rustup target add wasm32-unknown-unknown

But if it Still gives an Error then Run -

Terminal window
export PATH=~/.cargo/bin:$PATH

To Permanantly Solve this Issue, Go to your Root Terminal and try Running the Command -

Terminal window
ls -a

Look out for .zshrc, Open the file and add export PATH=~/.cargo/bin:$PATH Command to it.