Deploying CosmWasm Contract

Overview

This guide provides a comprehensive overview of creating, deploying, and interacting with CosmWasm contracts on a Wasm Minitia. It is essential to note that CosmWasm contracts are exclusively supported on Minitias utilizing the WasmVM.

Tutorial

Step 1: Clone the cw-contracts Repository

To begin, we will use the nameservice module from the cw-contracts repository as our example project. Execute the following commands to clone the repository and navigate to the relevant contract directory:

git clone https://github.com/deus-labs/cw-contracts.git
cd cw-contracts
git checkout main
cd contracts/nameservice

Step 2: Compile a Contract

Use the command below to compile a Wasm binary, preparing it for deployment on a Wasm Minitia:

RUSTFLAGS='-C link-arg=-s' cargo wasm

Upon successful compilation, the binary target/wasm32-unknown-unknown/release/cw_nameservice.wasm will be generated.

Advanced Compilation with rust-optimizer

For optimal efficiency, particularly in minimizing gas costs associated with deployment and interaction, the binary file size should be minimized. This necessitates the installation of Docker and the use of rust-optimizer, a tool designed to reduce the binary size without compromising contract functionality.

Execute the following command to utilize rust-optimizer:

docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer:0.12.11

This process will generate a binary file located at artifacts/cw_nameservice.wasm.

Step 3: Store a Contract

The next phase involves storing the compiled cw_nameservice.wasm contract on the Minitia.

> minitiad tx wasm store [binary-path] \
  --from test-account \
  --gas auto --gas-adjustment 1.5 \
  --gas-prices [gas-price] \
  --node [rpc-url]:[rpc-port] \
  --chain-id [chain-id]

Step 4: Instantiate a Contract

The final step involves instantiating the newly stored contract. To achieve this, the codeId of the stored cw_nameservice.wasm contract is necessary. Fetch the codeId with the following command line:

> RES=$(minitiad q tx --type=hash [txhash] --node [rpc-url]:[rpc-port] --output json)
CODE_ID=$(echo $RES | jq -r '.events[-1].attributes[1].value') 
echo $CODE_ID

Now you can instantiate a new Wasm contract using this code:

> INIT='{"purchase_price":{"amount":"100","denom":"l2/2588fd87a8e081f6a557f43ff14f05dddf5e34cb27afcefd6eaf81f1daea30d0"},"transfer_price":{"amount":"100","denom":"l2/2588fd87a8e081f6a557f43ff14f05dddf5e34cb27afcefd6eaf81f1daea30d0"}}'

minitiad tx wasm instantiate 1 "$INIT" --label=nameservice --no-admin \
  --from test-account \
  --gas auto --gas-adjustment 1.5 \
  --gas-prices [gas-price] \
  --node [rpc-url]:[rpc-port] \
  --chain-id [chain-id]

Step 5: Interact with a Contract

Having successfully instantiated the CosmWasm contract, the next step involves interacting with it. This phase requires the contract's address, which can be retrieved using the command outlined below:

> RES=$(minitiad q tx --type=hash [txhash] --node [rpc-url]:[rpc-port] --output json)
CONTRACT_ADDRESS=$(echo $RES | jq -r '.events[-1].attributes[0].value') 
echo $CONTRACT_ADDRESS

Execute a contract function

With the contract address at hand, we can now execute functions within the contract. As an example, we will execute the register function to register "test" as a name service domain. The execution command is as follows:

> REGISTER='{"register":{"name":"test"}}'
minitiad tx wasm execute [contract_address] "$REGISTER" --amount 100[denom]  \
  --from test-account \
  --gas auto --gas-adjustment 1.5 \
  --gas-prices [gas-price] \
  --node [rpc-url]:[rpc-port] \
  --chain-id [chain-id]

Query Results

After the successful execution of the register function, verification of the registered name is achievable through a contract query. Utilize the following command to perform the query:

> NAME_QUERY='{"resolve_record": {"name": "test"}}'
minitiad query wasm contract-state smart [contract_address] "$NAME_QUERY" --node [rpc-url]:[rpc-port]

This query will yield results in the format below, confirming the successful registration:

{
  "address": "init150748hf2e3mjed5w0xqkp9wn8xs55nnzneg452"
}

This step completes the guide on developing, deploying, and interacting with CosmWasm contracts. It illustrates the stages from contract compilation, storage, instantiation, to the final interaction phase. Following this guide ensures a structured approach to CosmWasm contract development, enabling developers to efficiently build and manage contracts within the Wasm Minitia.

Last updated

Logo

© 2024 Initia Foundation, All rights reserved.