Cosmos applications are often written in Go using Cosmos SDK. The code gets compiled before the chain is launched and effectively the app becomes an intergral part of the chain. If you ever need to update the code of the application, the chain has to go though a process of upgrading.

Another way of building applications is by writing smart contracts. A smart contract is a piece of code that gets written (uploaded) to the chain like any other transaction and then can be executed. The main difference (compared to an SDK module) is that you can create new contracts after the chain has launched, thus adding new functionality while the chain is running.

In this example we’ll be using CosmWasm to build, upload, instantiate and execute a smart contract. CosmWasm is implemented as a Cosmos SDK module that includes a WebAssembly virtual machine. Contract code is written using Rust language, compiled locally and uploaded to a chain using the same appcli binary used to interact with the chain (where app is the name of your application).


Creating a chain

Let’s start by creating a new Cosmos application:

starport app

Inside the ./contracts directory run the following command to enable CosmWasm:

starport add wasm

You can also enable it manually by editing several files in the project directory: see “Adding CosmWasm support manually”.

Star your application to see that everything builds and launches correctly:

starport serve

While the command above is running, execute a command in a different terminal to check that wasm subcommand has been added:

contractscli tx wasm

You should see a list of available subcommands: store, instantiate, execute, etc.

Building and uploading a contract