# CosmWasm
Discover how multi-chain smart contracts become possible with CosmWasm. The following sections are recommended as a preparation:
CosmWasm (opens new window) offers multi-chain solutions for smart contracts through an actor-model design focused on providing a library.
The actor model is a design pattern for reliable, distributed systems. It is the pattern underlying CosmWasm smart contracts.
Each actor has access to its own internal state and can only message other actors through a so-called dispatcher, which maintains the state and maps addresses to code and storage.
Want to read more on the actor model? See the CosmWasm documentation on the Actor Model for Contract Calls (opens new window).
CosmWasm's design makes the code agnostic to the details of underlying chains. It only requires a Cosmos SDK application to embed the Wasm
module.
CosmWasm is adaptable to different development environments by design and makes it possible to connect chains. It is a solid platform to develop on because:
- If you want to change chains, you can easily transfer smart contracts and decentralized applications (dApps).
- If your application grows, you can launch your chain for the next version of your smart contract. You do not need to compile and deploy the binaries again.
# Install
Go must be installed to use the Cosmos SDK. You also need Rust to write smart contracts.
Go to rustup.rs (opens new window) to install Rust, or update your version with rustup update
. Then, have it download and install the wasm32
compilation target:
wasmd
is the easiest way to begin. It is forked from gaiad (the Gaia Daemon) (opens new window), which is a binary build with the Cosmos Hub, and includes the Wasm (opens new window) module.
Create a folder and clone the wasmd
(opens new window) repository into it:
Verify your installation:
This returns:
If you cannot call wasmd
, make sure your $GOPATH
and $PATH
are set correctly.
# Connect to a testnet
First test the wasmd
client with the Cliffnet (opens new window) testnet. wasmd
is configured via environment variables. Export the most recent environment from here (opens new window):
Confirm you got it correctly:
This returns:
If you open another terminal window, do not forget to repeat this source
command, as this is local to the session.
# Your accounts
Now add some keys:
What was created?
This returns:
That is your address. Query your token balance:
This returns:
You have none. Time to ask the faucet (opens new window) to remedy this. To facilitate command-line actions, install jq (opens new window), which is a lightweight and flexible command-line JSON processor. Then prepare the request for alice
:
This returns:
upebble
is the denomination of the testnet token. With the content of the request ready, call the faucet:
This returns:
Query your balance again:
This returns:
Repeat this process for bob
.
# Compile a smart contract
Now that you have enough tokens to deploy a smart contract on Cliffnet, clone the contract samples away from your wasmd
folder:
This returns:
In this last command, wasm
is an alias (opens new window) for wasm build --release --target wasm32-unknown-unknown
.
You now have a compiled smart contract on file. You want to maintain your smart contract binary as small as possible, and have Rust compiled with default settings. Check the size of your build with:
This returns:
You can optimize the code with a Docker (opens new window) container based on an image provided by CosmWasm (opens new window) for production purposes:
Apple M1
If you work with a machine using M1 architecture, you need to add the --platform linux/amd64
flag:
Compare the result:
This returns:
# Upload a smart contract binary
Time to store the smart contract binaries on the blockchain:
The response returns a code_id
value (for instance 1391
), which uniquely identifies your newly uploaded binary in the blockchain. Record this in order to instantiate a name service with this binary in the next steps.
# Instantiate your smart contract
You have uploaded some code, but do not yet have any smart contract instance. Now to instantiate a new smart contract that uses this code. Look at the aptly-named instantiate
function in the name server contract:
Among the parameters the function expects are msg.purchase_price
and msg.transfer_price
(opens new window). Both have the type cosmwasm_std::Coin (opens new window), which looks very similar to Cosmos SDK's Coin
(opens new window). This is no coincidence. With this knowledge, instantiate a new name service with a purchase_price
and transfer_price
:
Note the code_id
that refers to which binary to use for the instantiation. Check that the name service instance was successfully created with:
You can find the contract address in the response. Make it a variable too:
Use this to fetch more information with the following command:
# Call your smart contract
With your instance now running, you can call other functions on it.
# Register a name
Looking into the contract code, you can find the execute
function:
There are two execute message types. These are used to register or transfer a name within the name service. Start by registering (opens new window) a new name with your instance:
# Verify the name registration
With the transaction posted, it is time to verify that the name was registered. In the contract you can find the query
function:
There are two query message types. Note that you now have deps: Deps
instead of deps: DepsMut
. This indicates that the execution of the function does not mutate the state. This is what to use with functions that implement a query type of service.
Verify the registration with ResolveRecord
(opens new window):
The response gives you the wallet address owning the registered name, which should be alice
.
# Transfer a name
Now create another transaction to transfer the name to the second wallet bob
. First prepare the query with the address of your other wallet:
Then send the transaction:
Under the hood, the execution used transfer_price
, which you set at the instantiation.
Check again with a resolve_record
query to confirm that the transfer was successful. Experiment with another transfer from bob
to alice
, and pay attention to which wallet can perform which transaction.
CosmWasm offers good documentation (opens new window). This section is a summary of the Getting Started section (opens new window). Store the env
script from here (opens new window) in case you wish to test it on your local node. Also look at the contract semantics (opens new window).
You can find more information in the CosmWasm Developer Academy (opens new window) and modular tutorials in the Wasm tutorials (opens new window). You can also find various hands-on videos on the workshops (opens new window) page.
# Next up
At this point, you have:
- Understood how Cosmos and the Cosmos SDK fit in the overall development of blockchain technology.
- A better sense of what comprises the Cosmos Ecosystem.
- Set up a wallet, got some ATOM tokens, and staked them.
- Learned more about the elements of application architecture.
- Understood and applied main concepts of the Cosmos SDK.
- Run a node, API, and CLI for a Cosmos chain.
- Used Ignite CLI to develop your chain.
- [Discovered how CosmWasm assists with developing multi-chain smart contracts in Rust.
Head to the next chapter to discover the Inter-Blockchain Communication Protocol.