Try   HackMD

Introduction to Smart Contracts on Cosmos SDK

Cosmos SDK and Starport: Development tools for the Cosmos Network

Cosmos SDK is an open-sourced framework for building Blockchains. Cosmos SDK and Starport are tools to help developers scaffold and launch Blockchains easily. A popular analogy made is that Cosmos SDK is the Ruby-on-Rails of Blockchains. It ships with the core that allows basic functionalities of every Blockchain: ABCI, Consensus, State persistence etc. Cosmos also allows developers to edit the codebase to introduce the business logic of their applications.

Smart Contracts: Programmable Logic on Blockchain

Smart Contracts are code that reside on the network and run as required. For Ethereum, you might have already used Solidity to write code for the Ethereum Network. For example. here is the Solidity code for voting on a decentralized election application.

    function vote(uint proposal) public {
        Voter storage sender = voters[msg.sender];
        require(sender.weight != 0, "Has no right to vote");
        require(!sender.voted, "Already voted.");
        sender.voted = true;
        sender.vote = proposal;

        // If `proposal` is out of the range of the array,
        // this will throw automatically and revert all
        // changes.
        proposals[proposal].voteCount += sender.weight;
    }

The logic is pretty straightforward: If a voter has right to vote, and if the voter has not already voted, add their vote to the digital ballot and increase the vote count by the weight of the voter's power.

Code such as this allows developers to write custom logic on the Blockchain. NFTs and UniSwap are all examples of Smart Contracts. It is important to learn how to implement Smart Contracts on Cosmos SDK.

CosmWasm: The Smart Contract Platform for Cosmos

Before CosmWasm, if you had to run logic on Cosmos SDK platform, you'd have to depend upon using GoLang to make changes to the source code. CosmWasm is a module you can now plug onto your present ecosystem, and write code in a bunch of languages and deploy it to your network. This is done via Web Assembly. You can read more about Web Assembly here. We will be using Rust in our series.

CosmWasm allows multi-chain contracts to be built. It is pretty easy to integrate since it has little requirements on host.

This allows CosmWasm to run agnostic of the chain it is running on. Multi-chain contracts also means we would have interblockchain contracts. This is an IBC-first design, allowing the developer to write code for one chain, but running it on another chain for certain processes.

Contracts are different from Modules in Cosmos. You can upload and edit contracts to the chain.

Wasmd: A Cosmos Zone with WASM smart contract enabled

Wasmd is a fork of Gaia with WASM support available. We will install the daemon wasmd and set it up later in this blog. Before you push your contracts to your mainnet, you can publish it here for testing purposes. Please note that wasmd is available on unix-like systems (Linux and MacOS), if you run Windows, you could give WSL2 a try.

With all that said, let's move on to installing and setting up Wasmd, Starport and CosmWasm!