Docs
CosmWasm Contract

CosmWasm Contract

CosmWasm on Dora Vota

CosmWasm (opens in a new tab) is a library for writing Rust smart contracts. Dora Vota supports CosmWasm 1.0.0+ smart contracts.

Install dependencies

Run the following command to download rustup and install Rust, then proceed by following the prompts:

Terminal
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Set up environment

Set stable as the default toolchain channel

Terminal
$ rustup default stable

Add WASM as the compilation target

Terminal
$ rustup target add wasm32-unknown-unknown

Install cargo-generate and cargo-run-script

Terminal
$ cargo install cargo-generate --features vendored-openssl
$ cargo install cargo-run-script

For more instructions, check out the CosmWasm Starter Pack (opens in a new tab).

Create a boilerplate smart contract

Create a template repository

Navigate to the folder where you want to create a boilerplate smart contract, then run:

Terminal
$ cargo generate --git https://github.com/DoraFactory/cw-template.git --name counter -d minimal=false
  • Cargo.toml
  • Developing.md
  • Importing.md
  • LICENSE
  • NOTICE
  • Publishing.md
  • README.md
      • schema.rs
    • contract.rs
    • error.rs
    • helpers.rs
    • integration_tests.rs
    • lib.rs
    • msg.rs
    • state.rs
  • A repository will be created with the structure above. It defines a simple counter app, which supports 4 basic functions:

    1. Instantiate the counter with a starting count.
    2. Increment the counter by 1.
    3. Reset the counter.
    4. Read the current count.

    Run unit tests

    Terminal
    $ cargo unit-test
    running 4 tests
    test contract::tests::increment ... ok
    test contract::tests::reset ... ok
    test contract::tests::proper_initialization ... ok
    test integration_tests::tests::count::count ... ok
     
    test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

    Deploy the boilerplate smart contract

    Compile the contract

    Before compile the contract, make sure you have met the prerequisites listed in the CosmWasm Developing documentation (opens in a new tab)

    Run the following command to compile:

    Terminal
    $ cargo wasm

    Before uploading, use the rust-optimizer (opens in a new tab) to minimize the size of the binary. Also, pay attention (opens in a new tab) to your processor architecture:

    $ 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
  • Cargo.lock
  • Cargo.toml
  • Developing.md
  • Importing.md
  • LICENSE
  • NOTICE
  • Publishing.md
  • README.md
    • checksums.txt
    • checksums_intermediate.txt
    • counter.wasm
  • After compiling the project, the relevant .wasm and checksums files will be created in the artifacts/ folder.

    Create an account

    Make sure that you have installed dorad.

    You can create a Dora Vota account via dorad keys add and display the account details via dorad keys list. For example:

    Terminal
    $ dorad keys add test1
    - address: dora1wxxtzdhydas27rtt99qtkzu5hqunxhlceltc7m
      name: test1
      pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A42Z75qDlS4x/exB/dYc5AcVTUPzvoKYFsMc0lT7AFa5"}'
      type: local
     
    **Important** write this mnemonic phrase in a safe place.
    It is the only way to recover your account if you ever forget your password.
     
    <mnemonic phrase>
    Terminal
    $ dorad keys list
    - address: dora1wxxtzdhydas27rtt99qtkzu5hqunxhlceltc7m
      name: test1
      pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A42Z75qDlS4x/exB/dYc5AcVTUPzvoKYFsMc0lT7AFa5"}'
      type: local

    Store on Mainnet

    In this example, we will demonstrate how to store on the Mainnet:

    Terminal
    $ cd artifacts
    $ dorad tx wasm store counter.wasm --from <unsafe-test-key-name> --chain-id=<chain-id> --gas-prices 100000000000peaka --gas auto --gas-adjustment 1.5 -b block -y
    • <unsafe-test-key-name>: Replace it with the key name from your local keys.
    • <chain-id>: Replace it with vote-ash.

    You will find a CODE_ID from the output of the command above. Save it as a local variable like CODE_ID=XX.

    Instead, you can also run the following command to set the CODE_ID as a variable:

    Terminal
    $ TX=$(dorad tx wasm store counter.wasm --from <unsafe-test-key-name> --chain-id=<chain-id> --gas-prices 100000000000peaka --gas auto --gas-adjustment 1.5 -b block --output json -y | jq -r '.txhash')
    $ CODE_ID=$(dorad query tx $TX --output json | jq -r '.logs[0].events[-1].attributes[0].value')
    $ echo "Your contract code_id is $CODE_ID"

    Or, you can also look up the code_id on Dora Vota Explorer (opens in a new tab) by the store’s transaction.

    Instantiate the contract

    Terminal
    $ INITIAL_STATE='{"count":100}'
    $ dorad tx wasm instantiate $CODE_ID $INITIAL_STATE --amount 50000peaka --label "Counter Contract" --from test1 --no-admin --gas-prices 100000000000peaka --gas auto --gas-adjustment 1.5 --chain-id "vota-ash" --node https://vota-rpc.dorafactory.org:443

    Get the contract address

    Terminal
    $ CONTRACT_ADDR=$(dorad query wasm list-contract-by-code ${CODE_ID} --node https://vota-rpc.dorafactory.org:443 --output json | jq -r '.contracts[0]')

    Execute the contract

    Increment the count in contract

    Terminal
    $ INCREMENT_MSG='{"increment":{}}'
    $ dorad tx wasm execute $CONTRACT_ADDR "$INCREMENT_MSG" --from test1 --gas-prices 100000000000peaka --gas auto --gas-adjustment 1.5 --chain-id "vota-ash" --node https://vota-rpc.dorafactory.org:443 -y

    Reset the count in contract

    Terminal
    $ RESET_MSG='{"reset":{"count":0}}'
    $ dorad tx wasm execute $CONTRACT_ADDR "$RESET_MSG" --from test1 --gas-prices 100000000000peaka --gas auto --gas-adjustment 1.5 --chain-id "vota-ash" --node https://vota-rpc.dorafactory.org:443 -y

    Query the contract

    Get the contract state

    Terminal
    $ GET_STATE_MSG='{"get_count":{}}'
    $ dorad query wasm contract-state smart $CONTRACT_ADDR "$GET_STATE_MSG" --node https://vota-rpc.dorafactory.org:443

    Query the contract information

    Terminal
    $ dorad query wasm contract $CONTRACT_ADDR --node https://vota-rpc.dorafactory.org:443

    List all contracts on the blockchain

    Terminal
    $ dorad query wasm list-code --node https://vota-rpc.dorafactory.org:443

    Good job! Now it’s time to learn how to actually develop contracts. You can now visit the CosmWasm documentation to gain a better understanding of how to interact with the Dora Vota blockchain.