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
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.
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:
Create a folder and clone the
wasmd (opens new window) repository into it:
Verify your installation:
If you cannot call
wasmd, make sure your
$PATH are set correctly.
# Connect to a testnet
Confirm you got it correctly:
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?
That is your address. Query your token balance:
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 your
upebble is the denomination of the testnet token. With the content of the request ready, call the faucet:
Query your balance again:
Repeat this process for
# Compile a smart contract
Now that you have enough tokens to deploy a smart contract on Pebblenet, clone the contract samples away from your
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:
Compare the result:
# Upload a smart contract binary
Time to store the smart contract binaries on the blockchain:
The response returns a
code_id value, 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.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
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
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
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
# Transfer a name
Now create another transaction to transfer the name to the second wallet
wallet2. 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
wallet, and pay attention to which wallet can perform which transaction.
# 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.