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 -
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 -
Cargo.toml
- This is the Manifest file for Rust’s Package Manager, Cargo. It Defines the Project’s Dependencies, Metadata, and Build Settings.src/contract.rs
- This is the Main file Containing your Smart Contract Logic. It typically includes the Contract’s Entry Points (instantiate, execute, query).src/msg.rs
- Defines the Message Structs for your Contract’s Interface, including InstantiateMsg, ExecuteMsg, and QueryMsg.src/state.rs
- Contains definitions for the Contract’s State, often using Storage Primitives from thecw_storage_plus
crate.src/error.rs
- Defines Custom Error Types for your Contract.src/lib.rs
- The Library’s root file, which usually re-exports important items from other modules.src/bin/schema.rs
- Generates JSON Schema files for your Contract’s messages, which can be used for front-end Integration.tests/
- Directory Containing Integration Tests for your Contract.examples/schema.rs
- An Example file Demonstrating how to use the Contract’s Schema..cargo/config
- Contains Cargo Configuration, Often used to set the Target for WebAssembly Compilation..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 -
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 -
rustup target add wasm32-unknown-unknown
But if it Still gives an Error then Run -
export PATH=~/.cargo/bin:$PATH
To Permanantly Solve this Issue, Go to your Root Terminal and try Running the Command -
ls -a
Look out for .zshrc
, Open the file and add export PATH=~/.cargo/bin:$PATH
Command to it.