|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Environment setup |
| 6 | + |
| 7 | +## Rust installation |
| 8 | + |
| 9 | +To work with CosmWasm smart contract, you will need Rust installed on your machine. If you don't |
| 10 | +have it, you can find installation instructions on |
| 11 | +[the Rust website](https://www.rust-lang.org/tools/install). |
| 12 | + |
| 13 | +We assume you are working with the stable Rust channel in this book. |
| 14 | + |
| 15 | +Additionally, you will need the Rust Wasm compiler backend installed to build Wasm binaries. |
| 16 | +To install it, run: |
| 17 | + |
| 18 | +```shell title="terminal" |
| 19 | +rustup target add wasm32-unknown-unknown |
| 20 | +``` |
| 21 | + |
| 22 | +## The cosmwasm-check utility |
| 23 | + |
| 24 | +An additional helpful tool for building smart contracts is the **[cosmwasm-check]** utility. |
| 25 | +It allows you to check if the wasm binary is a proper smart contract ready to upload into the blockchain. |
| 26 | +You can install it using Cargo: |
| 27 | + |
| 28 | +```shell title="terminal" |
| 29 | +cargo install cosmwasm-check |
| 30 | +``` |
| 31 | + |
| 32 | +If the installation succeeds, you should be able to execute the utility from your command line. |
| 33 | + |
| 34 | +```shell title="terminal" |
| 35 | +cosmwasm-check --version |
| 36 | +``` |
| 37 | + |
| 38 | +The output should look like this: |
| 39 | + |
| 40 | +```text |
| 41 | +Contract checking 3.0.2 |
| 42 | +``` |
| 43 | + |
| 44 | +## Verifying the installation |
| 45 | + |
| 46 | +To guarantee you are ready to build your smart contracts, you need to make sure you can build examples. |
| 47 | +Check out the **[cw-plus]** repository and run the testing command in its folder: |
| 48 | + |
| 49 | +```shell title="terminal" |
| 50 | +git clone [email protected]:CosmWasm/cw-plus.git && cd ./cw-plus && cargo test |
| 51 | +``` |
| 52 | + |
| 53 | +You should see that everything in the repository gets compiled and all tests pass. |
| 54 | + |
| 55 | +The **[cw-plus]** is a great place to find example contracts - look for them in the `contracts` directory. |
| 56 | +The repository is maintained by CosmWasm creators, so contracts in there should follow good practices. |
| 57 | + |
| 58 | +To use the **[cosmwasm-check]** utility, first, you need to build a smart contract. |
| 59 | +Go to some contract directory, for example, `contracts/cw1-whitelist`, and call `cargo wasm`: |
| 60 | + |
| 61 | +```shell title="terminal" |
| 62 | +cd contracts/cw1-whitelist && cargo wasm |
| 63 | +``` |
| 64 | + |
| 65 | +:::tip |
| 66 | + |
| 67 | +Due to reference types feature enabled by default in the Rust compiler since version `1.82` it is |
| 68 | +required to use the previous Rust compiler releases until the CosmWasm `2.2` version is released. |
| 69 | +The CosmWasm `2.2` will enable support for the reference types. |
| 70 | + |
| 71 | +::: |
| 72 | + |
| 73 | +You should be able to find your output binary in the `target/wasm32-unknown-unknown/release/` of the |
| 74 | +**root directory** of the repository (not in the contract directory itself). |
| 75 | +Now you can check if contract validation passes: |
| 76 | + |
| 77 | +```shell title="terminal" |
| 78 | +cw-plus/contracts/cw1-whitelist $ cosmwasm-check ../../target/wasm32-unknown-unknown/release/ |
| 79 | +../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm Available capabilities: {"iterator", |
| 80 | +"cosmwasm_1_1", "cosmwasm_1_2", "stargate", "staking"} |
| 81 | + |
| 82 | +../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm: pass |
| 83 | + |
| 84 | +All contracts (1) passed checks! |
| 85 | +``` |
| 86 | + |
| 87 | +## Macro expansion |
| 88 | + |
| 89 | +In VSCode you can hover over a macro like `#[cw_serde]`, do `shift+p` and then type: |
| 90 | +`rust analyzer: Expand macro recursively`. This will open a window with a fully expanded macro, |
| 91 | +which you can browse. In Vim, you can consider installing the [rustaceanvim] plugin. |
| 92 | +You can also use `cargo expand` tool from CLI, like this: |
| 93 | + |
| 94 | +```shell title="terminal" |
| 95 | +cargo expand --lib |
| 96 | +``` |
| 97 | + |
| 98 | +[cosmwasm-check]: https://github.com/CosmWasm/cosmwasm/tree/main/packages/check |
| 99 | +[cw-plus]: https://github.com/CosmWasm/cw-plus |
| 100 | +[rustaceanvim]: https://github.com/mrcjkb/rustaceanvim |
0 commit comments