Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe building contract and query creation #182

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs-test-gen/templates/empty.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(unexpected_cfgs, dead_code, unused_variables)]

{{code}}

#[test]
fn doctest() {
{{code}}
}
4 changes: 3 additions & 1 deletion src/pages/tutorial/cw-contract/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"contract-creation": "Contract creation",
"entry-points": "Entry points"
"entry-points": "Entry points",
"building-contract": "Building the contract",
"query": "Creating a query"
}
59 changes: 59 additions & 0 deletions src/pages/tutorial/cw-contract/building-contract.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Building the contract

Now it is time to build our contract. We can use a traditional cargo build pipeline for local
testing purposes: `cargo build` for compiling it and `cargo test` for running all tests (which we
don't have yet, but we will work on that soon).

However, we need to create a wasm binary to upload the contract to blockchain. We can do it by
passing an additional argument to the build command:

```shell copy filename="TERMINAL"
cargo build --target wasm32-unknown-unknown --release
```

The `--target` argument tells cargo to perform cross-compilation for a given target instead of
building a native binary for an OS it is running on - in this case, `wasm32-unknown-unknown`, which
is a fancy name for the Wasm target.

Additionally, I passed the `--release` argument to the command - it is not required, but in most
cases, debug information is not very useful while running on-chain. It is crucial to reduce the
uploaded binary size for gas cost minimization. It is worth knowing that there is a
[CosmWasm Rust Optimizer](https://github.com/CosmWasm/rust-optimizer) tool that takes care of
building even smaller binaries. For production, all the contracts should be compiled using this
tool, but for learning purposes, it is not an essential thing to do.

## Aliasing the build command

Now I can imagine you are disappointed in building your contracts with some overcomplicated command
instead of simple `cargo build`. Fortunately, it is not the case. The common practice is to alias
the building command to make it as simple as building a native app.

Let's create the `.cargo/config.toml` file in your contract project directory with the following
content:

```toml copy filename=".cargo/config.toml"
[alias]
wasm = "build --target wasm32-unknown-unknown --release"
wasm-debug = "build --target wasm32-unknown-unknown"
```

Now, building your Wasm binary is as easy as executing `cargo wasm`. We also added the additional
`wasm-debug` command for rare cases when we want to build the wasm binary, including debug
information.

## Checking contract validity

Once the contract is built, the last step is to ensure it is a valid CosmWasm contract by calling
`cosmwasm-check` on it:

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

```shell filename="TERMINAL"
Available capabilities: {"cosmwasm_1_2", "cosmwasm_1_4", "stargate", "cosmwasm_1_1", "cosmwasm_2_1", "cosmwasm_1_3", "cosmwasm_2_0", "staking", "iterator"}

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

All contracts (1) passed checks!
```
Loading