DEV Community

Vu
Vu

Posted on • Updated on

Smartcontract with CosmWasm (Part 5)

Introduction

I come back after work personal. Thanks to everyone following and supporting me and that's the end of the series.

Build the contract

We need to define the schema of the contract in the file examples/schema.rs. Import all schemas we have from previous part as: State, InstantiateMsg, ExecuteMsg, QueryMsg, ExecuteResponse, QueryResponse:

use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use cw_starter::msg::{ExecuteMsg, ExecuteResponse, InstantiateMsg, QueryMsg, QueryResponse};
use cw_starter::state::State;

fn main() {
    let mut out_dir = current_dir().unwrap();
    out_dir.push("schema");
    create_dir_all(&out_dir).unwrap();
    remove_schemas(&out_dir).unwrap();

    export_schema(&schema_for!(State), &out_dir);
    export_schema(&schema_for!(InstantiateMsg), &out_dir);
    export_schema(&schema_for!(ExecuteMsg), &out_dir);
    export_schema(&schema_for!(ExecuteResponse), &out_dir);
    export_schema(&schema_for!(QueryMsg), &out_dir);
    export_schema(&schema_for!(QueryResponse), &out_dir);
}
Enter fullscreen mode Exit fullscreen mode

We push all json schema to the schema directory. Now, we generate our schemas
Run command in the terminal in the root our folder:

cargo schema
Enter fullscreen mode Exit fullscreen mode

After the command runs successfully, we will see the json schemas in the schema folder.

Image description

The preparation step is alright. Next, build the smart contract with CosmWasm IDE.

Open the file src/contract.rs and click Build CosmWasm in the status bar and wait for the extension to build our contract. After the building has been done, we find a new file: artifacts/cw-starter.wasm and the UI of the extension will appear. This is our contract:

Note: We are using these dependencies in Cargo.toml, so we need to choose the chain support our contract as: Malaga, Osmosis Testnet, Osmosis Mainnet and CosmosHub Testnet and add your mnemonic to the file .env in the root project for extension to read mnemonic and interact with your wallet.

[dependencies]
cosmwasm-std = "1.0.0"
cosmwasm-storage = "1.0.0"
cw-storage-plus = "0.13.2"
cw2 = "0.13.2"
schemars = "0.8.8"
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.31" }
Enter fullscreen mode Exit fullscreen mode

Alright, we move to the next step: Deploy the contract on the chain.

Deploy the contract

First of all, select the chain, I will choose the Osmosis Testnet.
We need complete all fields when deploying the contract. In our case, the InstantiateMsg doesn't need any field, so we only need to input the label of the contract or you can ignore it.

Let's deploy with the Deploy CosmWasm and wait for our contract StoreCode and Instantiate to the chain. After deployment success, we have a contract address.

Execute the contract

Open the select box and choose Execute, input the contract address we get from the above step and the following message in the Execute Message input.
Need input your mnemonic in the wallet mnemonic(optional) (I know that is confusing, but it is required).

{"update":{}}
Enter fullscreen mode Exit fullscreen mode

Press the Execute button. The extension will execute our contract and return the transaction containing msg execute the contract.

Image description

Query the contract

Same execute the contract, I will move fast:
Select the Query, input our contract address and input the Query Message:

{"counter":{}}
Enter fullscreen mode Exit fullscreen mode

Press the Query button and tada! And the current state of the contract will appear.

Image description

Summary

That's the end of the series. I hope after this series, we have a simple term, development code and interact with CosmWasm Smart Contract.
Thanks for reading.

Reference

Top comments (0)