Skip to content

Commit

Permalink
Create tutorial section with introduction (#176) (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia authored Nov 14, 2024
2 parents d01d52e + bb80b29 commit 0c697b6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"how-to-doc": "How to doc",
"tags": {
"display": "hidden"
}
},
"tutorial": "Tutorial"
}
8 changes: 8 additions & 0 deletions src/pages/tutorial.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Tutorial

This module is a collection of guides for creating CosmWasm smart contracts. It will lead you step
by step, and explain relevant topics from the easiest to the trickier ones.

The point of these tutorials is not only to tell you about smart contracts API but also to show you
how to write contracts in a clean and maintainable way. We will show you patterns that CosmWasm
creators established and encouraged you to use.
4 changes: 4 additions & 0 deletions src/pages/tutorial/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"setup-environment": "Environment setup",
"cw-contract": "CosmWasm Contract"
}
3 changes: 3 additions & 0 deletions src/pages/tutorial/cw-contract.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CosmWasm contract writting tutorial

This section is a step-by-step guide on how to write a CosmWasm contract.
1 change: 1 addition & 0 deletions src/pages/tutorial/cw-contract/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
93 changes: 93 additions & 0 deletions src/pages/tutorial/setup-environment.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
tags: ["tutorial"]
---

import { Callout } from "nextra/components";

# Environment setup

## Rust installation

To work with CosmWasm smart contract, you will need Rust installed on your machine. If you don't
have it, you can find installation instructions on
[the Rust website](https://www.rust-lang.org/tools/install).

I assume you are working with the stable Rust channel in this book.

Additionally, you will need the Wasm rust compiler backend installed to build Wasm binaries. To
install it, run:

```shell copy filename="TERMINAL"
rustup target add wasm32-unknown-unknown
```

## The cosmwasm-check utility

An additional helpful tool for building smart contracts is the
[`cosmwasm-check`](https://github.com/CosmWasm/cosmwasm/tree/main/packages/check) utility. It allows
you to check if the wasm binary is a proper smart contract ready to upload into the blockchain. You
can install it using cargo:

```shell copy filename="TERMINAL"
cargo install cosmwasm-check
```

If the installation succeeds, you should be able to execute the utility from your command line.

```shell copy filename="TERMINAL"
cosmwasm-check --version
```

The output should look like this:

```shell
Contract checking 1.2.3
```

## Verifying the installation

To guarantee you are ready to build your smart contracts, you need to make sure you can build
examples. Check out the [`cw-plus`](https://github.com/CosmWasm/cw-plus) repository and run the
testing command in its folder:

```shell copy filename="TERMINAL"
git clone git@github.com:CosmWasm/cw-plus.git && cd ./cw-plus && cargo test
```

You should see that everything in the repository gets compiled and all tests pass.

The [`cw-plus`](https://github.com/CosmWasm/cw-plus) is a great place to find example contracts -
look for them in the `contracts` directory. The repository is maintained by CosmWasm creators, so
contracts in there should follow good practices.

To verify the [`cosmwasm-check`](https://github.com/CosmWasm/cosmwasm/tree/main/packages/check)
utility, first, you need to build a smart contract. Go to some contract directory, for example,
`contracts/cw1-whitelist`, and call `cargo wasm`:

```shell copy filename="TERMINAL"
cd contracts/cw1-whitelist && cargo wasm
```

<Callout>
Due to reference types feature enabled by default in the Rust compiler since version `1.82` it is
required to use the previous Rust compiler releases until the CosmWasm `2.2` version is released.
The CosmWasm `2.2` will enable support for the reference types.
</Callout>

You should be able to find your output binary in the `target/wasm32-unknown-unknown/release/` of the
root repo directory - not in the contract directory itself! Now you can check if contract validation
passes:

```shell copy filename="TERMINAL"
cosmwasm-check ../../target/wasm32-unknown-unknown/release/
```

```shell
cw-plus/contracts/cw1-whitelist $ cosmwasm-check
../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm Available capabilities: {"iterator",
"cosmwasm_1_1", "cosmwasm_1_2", "stargate", "staking"}

../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm: pass

All contracts (1) passed checks!
```

0 comments on commit 0c697b6

Please sign in to comment.