Deploy first WASM contract

The tutorial provides example on how to use the CosmJS package to instantiate, execute and query CosmWASM Smart Contracts deployed on the Coreum blockchain.

What Is CosmWasm?

CosmWasm is a smart contract platform that allows for the development and execution of smart contracts on blockchains that are built using the Cosmos SDK. It is designed to enhance the Cosmos ecosystem by providing a powerful and flexible way to create decentralized applications (dApps) that can interact across different blockchains within the Cosmos network. Here's an in-depth look at its features, architecture, and how it works:

  1. Multi-chain Compatibility: CosmWasm contracts can run on any blockchain that integrates the CosmWasm module, enabling developers to write a contract once and deploy it across multiple chains in the Cosmos ecosystem.

  2. Language Support: It allows developers to write smart contracts in Rust, a memory-safe, and high-performance programming language. This choice provides developers with the tools to write secure and efficient code.

  3. IBC Integration: CosmWasm supports the Inter-Blockchain Communication (IBC) protocol, enabling smart contracts to send and receive messages across different blockchains in the Cosmos network, facilitating true interoperability.

  4. Customizable: Blockchains can customize the CosmWasm module to fit their specific needs, making it a flexible solution for a wide range of applications.

Prerequisites

To complete this tutorial, you need to:

  • Install rust and cargo.
  • Be familiar with the Rust programming language.
  • Have a general understanding of how the Coreum blockchain works.
  • Follow the instruction to install cored binary.
  • Install the required util: jq.
  • Set the network variables for the development (testnet is preferable).

Source Code

The complete source code is located here.

Getting Started

  • Generate a new wallet for testing.
cored keys add wallet $COREUM_CHAIN_ID_ARGS
  • Use the faucet to fund your account

  • Clone the smart contract template

git clone https://github.com/CoreumFoundation/cw-contracts.git
  • Go to the template directory.
cd cw-contracts/contracts/nameservice

Build contract

  • Build optimized WASM smart contract:
docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/optimizer:0.15.0

This operation might take a significant amount of time.

    • If you get a following error:
error: could not find `Cargo.toml` in `/code` or any parent directory

Check if you are in a right directory (should be cw-contracts/contracts/nameservice)

Deploy contract

  • List the already deployed contract codes.
cored q wasm list-code $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS
  • Deploy the built artifact.
RES=$(cored tx wasm store artifacts/cw_nameservice.wasm \
    --from wallet --gas auto --gas-adjustment 1.3 -y -b block --output json $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS)
echo $RES
CODE_ID=$(echo $RES | jq -r '.logs[0].events[-1].attributes[-1].value')
echo $CODE_ID
  • Check the deployed code.
cored q wasm code-info $CODE_ID $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS

Instantiate contract

  • Instantiating the contract.
INIT="{\"purchase_price\":{\"amount\":\"100\",\"denom\":\"$COREUM_DENOM\"},\"transfer_price\":{\"amount\":\"999\",\"denom\":\"$COREUM_DENOM\"}}"
cored tx wasm instantiate $CODE_ID "$INIT" --from wallet --label "name service" -b block -y --no-admin $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS
  • Check the contract details and account balance.
cored q wasm list-contract-by-code $CODE_ID --output json $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS
CONTRACT_ADDRESS=$(cored q wasm list-contract-by-code $CODE_ID --output json $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS | jq -r '.contracts[-1]')
echo $CONTRACT_ADDRESS

Interact with the contract

  • Register a name for the wallet address on the contract.
REGISTER='{"register":{"name":"fred"}}'
cored tx wasm execute $CONTRACT_ADDRESS "$REGISTER" --amount 100$COREUM_DENOM --from wallet -b block -y $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS
  • Query the owner of the name record.
NAME_QUERY='{"resolve_record": {"name": "fred"}}'
cored q wasm contract-state smart $CONTRACT_ADDRESS "$NAME_QUERY" --output json $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS

The owner is the "wallet" now.

  • Transfer the ownership of the name record to "new-owner" wallet.
cored keys add new-owner $COREUM_CHAIN_ID_ARGS
RECIPIENT_ADDRESS=$(cored keys show --address new-owner $COREUM_CHAIN_ID_ARGS)
TRANSFER="{\"transfer\":{\"name\":\"fred\",\"to\":\"$RECIPIENT_ADDRESS\"}}"
cored tx wasm execute $CONTRACT_ADDRESS "$TRANSFER" --amount 999$COREUM_DENOM --from wallet -b block -y $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS
  • Query the record owner again to see the new owner address.
echo "Recipient address: $RECIPIENT_ADDRESS"
NAME_QUERY='{"resolve_record": {"name": "fred"}}'
cored q wasm contract-state smart $CONTRACT_ADDRESS "$NAME_QUERY" --output json $COREUM_NODE_ARGS $COREUM_CHAIN_ID_ARGS

Next steps

  • Read Coreum modules specification, to be familiar with the custom Coreum functionality you can use for your application.
  • Read WASM docs to understand all supported WASM features.
  • Check other tutorials to find something you might be interested in additionally.