CosmWasm to ink! — Beyond the Cosmos 🌌

Roloi | Dynamic payments
5 min readJul 18, 2022

--

At Roloi, we love Rust. That’s why we created our main smart contract for the Cosmos ecosystem at first.

Later on, we decided to pivot and migrate our smart contract to Polkadot, and that’s how this journey began. 🚀

🤓 ink! & CosmWasm

For Polkadot, we decided to build our smart contract using ink!, the Rust eDSL to build smart contracts for blockchains built on the Substrate framework.

The goal of this article is to explain the main differences we noticed between CosmWasm and ink!

🏁 Setting up the environment

The steps to set up the environment are pretty much the same. You can take a look at both processes here:

The process looks like this:

  • 📦 Install Rust & Cargo
  • 🎯 Configure compilation target
  • 👨🏻‍💻 Setup tooling (CLI, local node)

Some relevant points:

  • ink! uses nightly versions of Rust
  • Local node setup is different

For Cosmos, generally, you will need to run the specific local node of the chain you are developing for (Secret, Juno, etc). Those nodes are usually containerized with Docker and the setup varies from chain to chain.

Setting up a local node for ink! is super straightforward. substrate-contracts-node is a simple blockchain that supports smart contracts. It provides a quick setup to start building smart contracts fast in a local environment.

🧐 Architecture and philosophy

Each technology uses a different design pattern:

These design patterns shape how the contracts will look like.

🧾 CosmWasm contracts

The CosmWasm contracts consist of 3 main functions that act as entry points. instantiate(), execute(), query():

instantiate() | Assuming the contract’s already been deployed, this bootstraps the initial contract state
execute() | executed on all chain nodes, permission to update storage
query() | read-only permissions over storage

🧾 ink! contracts

ink! leverages the power of Rust macros to implement a module-based contract (list of examples here)

This code explains by itself 👏🏻

🔧 Tools

🤓 Some tools that improve the development experience:

  • cargo-generate is a smart contract template generator for CosmWasm.
  • cargo-contract is the main CLI for ink! development that includes template generator and a lot of smart contract subcommands such as build, upload, instantiate, call, check and test.
  • OpenBrush is a smart contracts library on ink!. It provides standard contracts and development helpers such as macros, extensions, and interfaces.
  • Swanky CLI is based on cargo-contract but has extended features. One of them is the possibility of starting a development chain node with just one command. It’s developed by the Astar Network team.

💻 Development

In terms of development, we would like to mention some relevant topics.

💾 Storage

  • CosmWasm storage structure allows division and on-demand loading.
  • ink! storage is represented as a unique struct that is loaded as a whole.

In ink! key lookups in mappings are done directly from contract storage. That’s why using mappings over arrays is recommended.

🔢 Integer types

  • CosmWasm uses JSON encoded messages, so the uint wrapper is needed.
  • ink! doesn’t rely on JSON schemas for defining how the messages are structured, so native uint Rust types can be used.

🔗 Chain modules

  • CosmWasm: the main crate (cosmwasm_std) contains modules like Bank or Staking that have specific logic that can be imported.
  • ink!: the smart contract can call existing public functions in parachains through chain extensions. For example, dApp Staking functions from Astar parachain.

📑 Indexed mappings

  • CosmWasm: supports mappings through storage plus. Stored as B-tree structure and supports iteration.
  • ink!: supports mappings by default, but doesn’t support iteration yet. Extra behaviors should be developed separately.

💰 Funds

  • CosmWasm handles multi-token transfers by using an array as part of the MessageInfo structure.
  • ink! by default, only supports the native token of the chain. Messages should implement the payable flag. Value can be accessed through the environment: “transferred_value()”.

🚀 Build & Deploy

📦 Compile a contract

  • CosmWasm: build done with cargo-wasm. Generates a .wasm file. Optimization is manually done through a Cargo.toml script (this requires for cargo-run-script to be installed).
  • ink!: build done with cargo-contract. Generates a .contract and a .wasm.
    The .contract file contains both the wasm blob and the metadata.

🚀 Deploy a contract

  • CosmWasm: deployment is done through specific chain CLI (secretd, junod) or through the chain UI. The .wasm file is uploaded.
  • ink!: the process is standard through the cargo-contract CLI. The .contract file is uploaded.

➕ Instantiate a contract

  • CosmWasm: instantiation is done through the chain CLI or chain UI.
  • ink!: cargo-contract CLI supports instantiation. This can also be done through the Polkadot or Substrate portal. The use of parameters instead of JSON messages is a huge win and simplifies both instantiation and interaction.

💡 Conclusion

We are builders. We are all trying to find the future of Web3. We think both Cosmos and Polkadot are on their way there by introducing the concept of “internet of blockchains”.

Both technologies are in different stages. We think that is earlier for Polkadot and the roadmap is looking great. The experience with ink! has been amazing from day 1.

The Polkadot builders community is strong and constantly experimenting with technologies and innovative ideas. That’s one of the reasons we are choosing Polkadot to be the first home for Roloi.

For our team, it feels like Polkadot is learning from other blockchains and always trying to improve how things are done, that’s exciting.

Below, you will find some useful resources to get your journey started. 🚀

📢 Contact us

--

--