Skip to main content

CosmWasm Example

Building smart contracts

For working with CosmWasm we recommend using development frameworks Sylvia or beginning with the CosmWasm template.

Example

  1. Set Up Your dev environment and install the CosmWasm workspace, which includes templates and tools for building CosmWasm smart contracts.
cargo install cargo-generate --features vendored-openssl
cargo generate --git https://github.com/CosmWasm/cw-template.git --name my-contract
cd my-contract
  1. Write Your Smart Contract

Edit the src/contract.rs and src/msg.rs files to define your contract's logic and messages it handles.

  1. Test Your Contract

Write unit tests in the tests directory.

cargo test
  1. Compile the Contract

Compile your CosmWasm contract into a WebAssembly (WASM) binary:

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

Optimize the WASM binary to reduce its size and make it blockchain-ready:

docker run --rm -v "$(pwd)":/contract \
--mount type=volume,source="$(basename "$(pwd)")\_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer:0.12.6
  1. Upload the Contract

Connect to RollApp X:

rollappd tx wasm store contract.wasm --from <your-wallet> --chain-id <chain-id> --gas auto --gas-adjustment 1.3 --gas-prices 0.025roly -y
  1. Instantiate the Contract After uploading, you need to instantiate the contract with its initial configuration.
rollappd tx wasm instantiate <code_id> '{"count": 100}' --label "my counter" --from <your-wallet> --chain-id <chain-id> --amount 5000roly --gas auto