Skip to main content

CosmWasm rollup



CosmWasm is a smart contracting platform built for the Cosmos ecosystem by making use of WebAssembly (Wasm) to build smart contracts for Cosmos-SDK. In this tutorial, we will be exploring how to integrate CosmWasm with Celestia's data availability layer using Rollkit.


This tutorial will explore developing with Rollkit, which is still in Alpha stage. If you run into bugs, please write a Github Issue ticket or let us know in our Telegram.


The script for this tutorial is built for Celestia's Mocha testnet. If you choose to use Arabica devnet, you will need to modify the script manually.

You can learn more about CosmWasm here.

In this tutorial, we will going over the following:

  1. Setting up your dependencies for your CosmWasm smart contracts
  2. Setting up Rollkit on CosmWasm
  3. Instantiate a local network for your CosmWasm chain connected to Celestia
  4. Deploying a Rust smart contract to CosmWasm chain
  5. Interacting with the smart contract

The smart contract we will use for this tutorial is one provided by the CosmWasm team for Nameservice purchasing.

You can check out the contract here.

How to write the Rust smart contract for Nameservice is outside the scope of this tutorial. In the future we will add more tutorials for writing CosmWasm smart contracts for Celestia.

CosmWasm dependency installations

Environment setup

For this tutorial, we will be using curl and jq as helpful tools. You can follow the guide on installing them here.

Golang dependency

The Golang version used for this tutorial is v1.18+

You can install Golang by following our tutorial here.

Rust installation


First, before installing Rust, you would need to install rustup.

On Mac and Linux systems, here are the commands for installing it:

curl --proto '=https' --tlsv1.2 -sSf | sh

You will see a note similar to below after installing Rust:

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source "$HOME/.cargo/env"

If you don't follow the guidance, you won't be able to continue with the tutorial!

After installation, follow the commands here to setup Rust.

rustup default stable
cargo version

rustup target list --installed
rustup target add wasm32-unknown-unknown

Your output should look similar to below:

info: using existing install for 'stable-aarch64-apple-darwin'
info: default toolchain set to 'stable-aarch64-apple-darwin'

stable-aarch64-apple-darwin unchanged - rustc 1.65.0 (897e37553 2022-11-02)

cargo 1.65.0 (4bc8f24d3 2022-10-20)
info: downloading component 'rust-std' for 'wasm32-unknown-unknown'
info: installing component 'rust-std' for 'wasm32-unknown-unknown'

Docker installation

We will be using Docker later in this tutorial for compiling a smart contract to use a small footprint. We recommend installing Docker on your machine.

Examples on how to install it on Linux are found here. Find the right instructions specific for your OS here.

Wasmd installation

Here, we are going to pull down the wasmd repository and replace Tendermint with Rollkit. Rollkit is a drop-in replacement for Tendermint that allows Cosmos-SDK applications to connect to Celestia's data availability network.

git clone
cd wasmd
git fetch --tags
git checkout v0.27.0
go mod edit -replace[email protected]
go mod edit -replace[email protected]
go mod tidy -compat=1.17
go mod download
make install

Celestia node

You will need a light node running with test tokens on Mocha testnet in order to complete this tutorial. Please complete the tutorial here, or start up yournode.

Setting up your environment for CosmWasm on Celestia

Now the wasmd binary is built, we need to setup a local network that communicates between wasmd and Rollkit.

Initializing CosmWasm rollup with a bash script

We have a handy found in this repo here.

We can copy it over to our directory with the following commands:

# From inside the `wasmd` directory

This copies over our script to initialize our CosmWasm rollup.

You can view the contents of the script to see how we initialize the CosmWasm Rollup.


If you are on macOS, you will need to install md5sha1sum before starting your rollup:

brew install md5sha1sum

You can initialize the script with the following command:


With that, we have kickstarted our wasmd network!

Optional: see what's inside the script

You can skip this section, but it is important to know how Rollkit is initializing the cosmwasm rollup.

Here are the contents of the script:


CHAINFLAG="--chain-id ${CHAIN_ID}"
TXFLAG="--chain-id ${CHAIN_ID} --gas-prices 0uwasm --gas auto --gas-adjustment 1.3"

NAMESPACE_ID=$(echo $RANDOM | md5sum | head -c 16; echo;)
DA_BLOCK_HEIGHT=$(curl | jq -r '.result.block.header.height')

rm -rf "$HOME"/.wasmd
wasmd tendermint unsafe-reset-all
wasmd init $VALIDATOR_NAME --chain-id $CHAIN_ID

sed -i'' -e 's/^minimum-gas-prices *= .*/minimum-gas-prices = "0uwasm"/' "$HOME"/.wasmd/config/app.toml
sed -i'' -e '/\[api\]/,+3 s/enable *= .*/enable = true/' "$HOME"/.wasmd/config/app.toml
sed -i'' -e "s/^chain-id *= .*/chain-id = \"$CHAIN_ID\"/" "$HOME"/.wasmd/config/client.toml
sed -i'' -e '/\[rpc\]/,+3 s/laddr *= .*/laddr = "tcp:\/\/"/' "$HOME"/.wasmd/config/config.toml
sed -i'' -e 's/"time_iota_ms": "1000"/"time_iota_ms": "10"/' "$HOME"/.wasmd/config/genesis.json
sed -i'' -e 's/bond_denom": ".*"/bond_denom": "uwasm"/' "$HOME"/.wasmd/config/genesis.json
sed -i'' -e 's/mint_denom": ".*"/mint_denom": "uwasm"/' "$HOME"/.wasmd/config/genesis.json

wasmd keys add $KEY_NAME --keyring-backend test
wasmd add-genesis-account $KEY_NAME $TOKEN_AMOUNT --keyring-backend test
wasmd gentx $KEY_NAME $STAKING_AMOUNT --chain-id $CHAIN_ID --keyring-backend test
wasmd start --rollkit.aggregator true --rollkit.da_layer celestia --rollkit.da_config='{"base_url":"http://localhost:26659","timeout":60000000000,"fee":6000,"gas_limit":6000000}' --rollkit.namespace_id $NAMESPACE_ID --rollkit.da_start_height $DA_BLOCK_HEIGHT

Contract deployment on CosmWasm with Rollkit

Compile the smart contract

In a new terminal instance, we will run the following commands to pull down the Nameservice smart contract and compile it:

git clone
cd cw-contracts
cd contracts/nameservice
cargo wasm

The compiled contract is outputted to: target/wasm32-unknown-unknown/release/cw_nameservice.wasm.

Unit tests

If we want to run tests, we can do so with the following command in the ~/cw-contracts/contracts/nameservice directory:

cargo unit-test

Optimized smart contract

Because we are deploying the compiled smart contract to wasmd, we want it to be as small as possible.

The CosmWasm team provides a tool called rust-optimizer which we need Docker for in order to compile.

AMD Machines

Run the following command in the ~/cw-contracts/contracts/nameservice directory:

sudo 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 \

This will place the optimized Wasm bytecode at artifacts/cw_nameservice.wasm.

Contract deployment

Let's now deploy our smart contract!

AMD Machines

Run the following in the ~/cw-contracts/contracts/nameservice directory:

TX_HASH=$(wasmd tx wasm store artifacts/cw_nameservice.wasm --from celeswasm-key --keyring-backend test --chain-id celeswasm --gas-prices 0uwasm --gas auto --gas-adjustment 1.3 --node --output json -y | jq -r '.txhash') && echo $TX_HASH

This will get you the transaction hash for the smart contract deployment. Given we are using Rollkit, there will be a delay on the transaction being included due to Rollkit waiting on Celestia's data availability layer to confirm the block has been included before submitting a new block.


If you run into errors with variables on the previous command, or commands in the remainder of the tutorial, cross-reference the variables in the command with the variables in the script.

Contract interaction on CosmWasm with Celestia

In the previous steps, we have stored out contract's tx hash in an environment variable for later use.

Because of the longer time periods of submitting transactions via Rollkit due to waiting on Celestia's data availability layer to confirm block inclusion, we will need to query our tx hash directly to get information about it.

Contract querying

Let's start by querying our transaction hash for its code ID:

CODE_ID=$(wasmd query tx --type=hash $TX_HASH --chain-id celeswasm --node --output json | jq -r '.logs[0].events[-1].attributes[0].value')
echo $CODE_ID

This will give us back the Code ID of the deployed contract.

In our case, since it's the first contract deployed on our local network, the value is 1.

Now, we can take a look at the contracts instantiated by this Code ID:

wasmd query wasm list-contract-by-code $CODE_ID --chain-id celeswasm --node --output json

We get the following output:


Contract instantiation

We start instantiating the contract by writing up the following INIT message for nameservice contract. Here, we are specifying that purchase_price of a name is 100uwasm and transfer_price is 999uwasm.

wasmd tx wasm instantiate $CODE_ID "$INIT" --from celeswasm-key --keyring-backend test --label "name service" --chain-id celeswasm --gas-prices 0uwasm --gas auto --gas-adjustment 1.3 -y --no-admin --node

Contract interaction

Now that we instantiated it, we can interact further with the contract:

wasmd query wasm list-contract-by-code $CODE_ID --chain-id celeswasm --output json --node
CONTRACT=$(wasmd query wasm list-contract-by-code $CODE_ID --chain-id celeswasm --output json --node | jq -r '.contracts[-1]')

wasmd query wasm contract --node $CONTRACT --chain-id celeswasm
wasmd query bank balances --node $CONTRACT --chain-id celeswasm

This allows us to see the contract address, contract details, and bank balances.

Your output will look similar to below:

address: wasm14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s0phg4d
admin: ""
code_id: "1"
created: null
creator: wasm1y9ceqvnsnm9xtcdmhrjvv4rslgwfzmrzky2c5z
extension: null
ibc_port_id: ""
label: name service
balances: []
next_key: null
total: "0"

Now, let's register a name to the contract for our wallet address:

wasmd tx wasm execute $CONTRACT "$REGISTER" --amount 100uwasm --from celeswasm-key --chain-id celeswasm --gas-prices 0uwasm --gas auto --gas-adjustment 1.3 --node --keyring-backend test -y

Your output will look similar to below:

DEIP --keyring-backend test -y
gas estimate: 167533
code: 0
codespace: ""
data: ""
events: []
gas_used: "0"
gas_wanted: "0"
height: "0"
info: ""
logs: []
raw_log: '[]'
timestamp: ""
tx: null
txhash: C147257485B72E7FFA5FDB943C94CE951A37817554339586FFD645AD2AA397C3

If you try to register the same name again, you'll see an expected error:

Error: rpc error: code = Unknown desc = rpc error: code = Unknown desc = failed to execute message; message index: 0: Name has been taken (name fred): execute wasm contract failed [CosmWasm/wasmd/x/wasm/keeper/keeper.go:364] With gas wanted: '0' and gas used: '123809' : unknown request

Next, query the owner of the name record:

NAME_QUERY='{"resolve_record": {"name": "fred"}}'
wasmd query wasm contract-state smart $CONTRACT "$NAME_QUERY" --chain-id celeswasm --node --output json

You'll see the owner's address in a JSON response:


With that, we have instantiated and interacted with the CosmWasm nameservice smart contract using Celestia!