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