Skip to content

Commit 6bed71b

Browse files
committed
Added Building contract chapter.
1 parent f07cc64 commit 6bed71b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Building the contract
6+
7+
Now it is time to build our contract. We can use a traditional cargo build pipeline for local
8+
testing purposes: `cargo build` for compiling it and `cargo test` for running all tests (which we
9+
don't have yet, but we will work on that soon).
10+
11+
However, we need to create a Wasm binary to upload the contract to blockchain. We can do it by
12+
passing an additional argument to the build command:
13+
14+
```shell title="terminal"
15+
cargo build --target wasm32-unknown-unknown --release
16+
```
17+
18+
The `--target` argument tells cargo to perform cross-compilation for a given target instead of
19+
building a native binary for an operating system it is running on, in this case, `wasm32-unknown-unknown`,
20+
which is a fancy name for the Wasm target.
21+
22+
Additionally, we have passed the `--release` argument to the command. It is not required, but in most cases,
23+
debug information is not very useful while running on-chain. It is crucial to reduce the uploaded binary size
24+
for gas cost minimization. It is worth knowing that there is a [CosmWasm Rust Optimizer] tool that takes care
25+
of building even smaller binaries. For production, all the contracts should be compiled using this tool,
26+
but now, for learning purposes, it is not an essential thing to do.
27+
28+
## Aliasing the build command
29+
30+
Now I can imagine you are disappointed in building your contracts with some overcomplicated command
31+
instead of simple `cargo build`. Fortunately, it is not the case. The common practice is to alias the building
32+
command to make it as simple as building a native app.
33+
34+
Let's create the `.cargo/config.toml` file in your contract project directory with the following content:
35+
36+
```toml title=".cargo/config.toml"
37+
[alias]
38+
wasm = "build --target wasm32-unknown-unknown --release"
39+
wasm-debug = "build --target wasm32-unknown-unknown"
40+
```
41+
42+
Now, building your Wasm binary is as easy as executing `cargo wasm`.
43+
We also added the additional `wasm-debug` command for very rare cases when you might want
44+
to build the Wasm binary with debug information.
45+
46+
## Checking contract validity
47+
48+
Once the contract is built, the last step to ensure it is a valid CosmWasm contract is by calling `cosmwasm-check` on it:
49+
50+
```shell title="terminal"
51+
cargo wasm && cosmwasm-check ./target/wasm32-unknown-unknown/release/contract.wasm
52+
```
53+
54+
```text title="terminal"
55+
Available capabilities: {"cosmwasm_1_2", "cosmwasm_1_4", "stargate", "cosmwasm_1_1", "cosmwasm_2_1", "cosmwasm_1_3", "cosmwasm_2_0", "staking", "iterator"}
56+
57+
./target/wasm32-unknown-unknown/release/contract.wasm: pass
58+
59+
All contracts (1) passed checks!
60+
```
61+
62+
[CosmWasm Rust Optimizer]: https://github.com/CosmWasm/rust-optimizer

0 commit comments

Comments
 (0)