DEV Community

Vu
Vu

Posted on

Writing Smart contract with CosmWasm (Part 1)

Introduction

Hi, I am Vu, and I share a new series: Writing a smart contract with CosmWasm.

What are smart contracts?

It is a simple program stored on a blockchain. It can execute when predetermined conditions.
These are some of the benefits and props of the smart contract:

  • Security
  • Automation
  • Speed and efficiency
  • Trust and transparency

Of cause, it is a program by humans and they can make mistakes and this makes the contracts vulnerable to attacks.
Maybe, a smart contract cannot exceed 24kb or else it will run out of gas.

Environment

In part 1, I introduce the environment to develop and how to set up this.

Setup rust

Rust is the main programming language used for CosmWasm smart contracts. You can follow this to set up Rust.
Then run the following commands:

# 1. Set stable as the default release channel

rustup default stable 
cargo version

# update if it is lower than 1.50.0+

rustup update stable

# 2. Add WASM as the compilation target:

rustup target list --installed
rustup target add wasm32-unknow-unknown

# 3. Install the following packages to generate the contract:

cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
Enter fullscreen mode Exit fullscreen mode

Setup VS Code

VS Code is a powerful editor. You can download and install it on all platforms by link.
And you can add extensions: rust-analyzer to the diagnostic rust code.

Image description

And CosmWasm IDE to develop and deploy the smart contract.

Image description

After all, we finished to setup the environment to develop smart contract programming.

Time to start.

This is the time to write a smart contract. We will write a smart contract which has a logic: when we execute, the smart contract will increment the counter variable to one.

First of all, we are starting with the CW-Template to sets up a boilerplate project.
I use a version of minimal boilerplate:

cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 1.0-minimal --name counter-contract
Enter fullscreen mode Exit fullscreen mode

Once complete, we look output:

🔧   Destination: /Users/oraichain/Desktop/person/rust-lab/cw-starter ...
🔧   Generating template ...
[ 1/30]   Done: .cargo/config
[ 2/30]   Done: .cargo
[ 3/30]   Skipped: .circleci/config.yml
[ 4/30]   Done: .circleci
[ 5/30]   Done: .editorconfig
[ 6/30]   Done: .github/workflows/Basic.yml
[ 7/30]   Done: .github/workflows
[ 8/30]   Done: .github
[ 9/30]   Done: .gitignore
[10/30]   Done: Cargo.lock
[11/30]   Done: Cargo.toml
[12/30]   Done: LICENSE
[13/30]   Done: NOTICE
[14/30]   Done: README.md
[15/30]   Done: examples/schema.rs
[16/30]   Done: examples
[17/30]   Done: rustfmt.toml
[18/30]   Done: schema/count_response.json
[19/30]   Done: schema/execute_msg.json
[20/30]   Done: schema/instantiate_msg.json
[21/30]   Done: schema/query_msg.json
[22/30]   Done: schema/state.json
[23/30]   Done: schema
[24/30]   Done: src/contract.rs
[25/30]   Done: src/error.rs
[26/30]   Done: src/helpers.rs
[27/30]   Done: src/lib.rs
[28/30]   Done: src/msg.rs
[29/30]   Done: src/state.rs
[30/30]   Done: src
🔧   Moving generated files into: `/Users/oraichain/Desktop/person/rust-lab/cw-starter`...
💡   Initializing a fresh Git repository
✨   Done! New project created /Users/oraichain/Desktop/person/rust-lab/cw-starter
Enter fullscreen mode Exit fullscreen mode

Let's open project with the commands:

cd cw-starter # change dir to project.
code . # open project with vscode.
Enter fullscreen mode Exit fullscreen mode

Tada, you had a smart contract project.

Thank you read the post. I will update part 2 soon. Have a good day!

Top comments (1)

Collapse
 
tuanorai profile image
tuancaurao

Great post

from: dev buoito