Part 1: Introduction to CosmWasm
Introduction
CosmWasm is a smart contract platform built for the Cosmos ecosystem, allowing developers to write secure and efficient smart contracts in Rust. Please check the article for basic understanding of CosmWasm. In this article, we will go through the process of setting up a development environment for CosmWasm, including the installation of essential tools and dependencies, more specifically, for CosmWasm testnet malaga-420.
Prerequisites
- A Linux-based operating system (Ubuntu is used in this article)
- Basic understanding of Bash scripting and the command line
Step 1: Update and Install Basic Dependencies
First, update the package list and install basic dependencies, including Git, wget, curl, nano, and jq:
apt-get -y update
apt-get -y install git wget curl nano jq
Step 2: Install Make
Make is a build automation tool, and it’s required for compiling some of the tools used in this tutorial. Install Make by installing the build-essential package:
apt-get -y install build-essential
Step 3: Install Go
The Go programming language is required for some of the tools used in the CosmWasm ecosystem. Download and install Go 1.18.2 (as per requirements of Malaga-420) with the following commands:
wget https://dl.google.com/go/go1.18.2.linux-amd64.tar.gz
tar -xvf go1.18.2.linux-amd64.tar.gz
mv go /usr/local
Next, set the Go environment variables and update the PATH:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Save these environment variables to your .bash_profile
for future terminal sessions:
echo 'export GOROOT=/usr/local/go' >>~/.bash_profile
echo 'export GOPATH=$HOME/go' >>~/.bash_profile
echo 'export PATH=$GOPATH/bin:$GOROOT/bin:$PATH' >>~/.bash_profile
source ~/.bash_profile
echo 'source ~/.bash_profile' >> /etc/bash.bashrc
Clean up the Go tar.gz:
rm go1.18.2.linux-amd64.tar.gz
Step 4: Install Rust
Rust is the primary programming language for writing CosmWasm smart contracts. Install Rust with the following commands:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
echo "source $HOME/.cargo/env" >> ~/.bash_profile
Set the Rust default toolchain to stable and update it:
rustup default stable
rustup update stable
Add the wasm32-unknown-unknown
target, which is required for compiling Rust code to WebAssembly:
rustup target list --installed
rustup target add wasm32-unknown-unknown
Step 5: Install wasmd
wasmd
is a blockchain daemon built with CosmWasm support. Clone the wasmd repository and checkout to version 0.27.0 (as per requirements of Malaga-420):
git clone https://github.com/CosmWasm/wasmd.git
cd wasmd
git checkout v0.21.0
Build and install wasmd
:
make install
Verify the installation by checking the version:
wasmd version
Step 6: Configure Testnet Environment
Source and save the environment variables for the CosmWasm testnet, malaga-420:
source <(curl -sSL https://raw.githubusercontent.com/CosmWasm/testnets/master/sandynet-1/defaults.env)
echo "source <(curl -sSL https://raw.githubusercontent.com/CosmWasm/testnets/master/sandynet-1/defaults.env)" >> ~/.bash_profile
Next, set the NODE
environment variable with the Remote Procedure Call (RPC) endpoint and save it to .bash_profile
:
echo 'export NODE="--node $RPC"' >> ~/.bash_profile
Finally, set the TXFLAG
environment variable with transaction-related flags (such as chain ID, gas prices, and gas adjustment) and save it to .bash_profile
:
echo 'export TXFLAG="${NODE} --chain-id ${CHAIN_ID} --gas-prices 0.015umlg --gas auto --gas-adjustment 1.5"' >> ~/.bash_profile
The TXFLAG
variable includes the following flags:
${NODE}
: The RPC endpoint (previously set in theNODE
variable).--chain-id ${CHAIN_ID}
: The chain ID of the testnet.--gas-prices 0.015umlg
: The gas prices for transactions (in this case, 0.015umlg).--gas auto
: Automatically estimate the gas needed for a transaction.--gas-adjustment 1.5
: Apply a 1.5x multiplier to the estimated gas for a transaction to account for variability.
Conclusions
In this article, we walked through the process of setting up CosmWasm development environment. With the tools and dependencies in place, we are now well-equipped to dive into the world of smart contracts and the Cosmos ecosystem. Happy coding!