|
| 1 | +[rustup installer]: https://rustup.rs |
| 2 | +[Docker]: https://www.docker.com/ |
| 3 | +[CosmWasm Optimizing Compiler]: https://github.com/CosmWasm/optimizer |
| 4 | +[binaryen]: https://github.com/WebAssembly/binaryen |
| 5 | + |
| 6 | +# Installation |
| 7 | + |
| 8 | +## Setting up the environment |
| 9 | + |
| 10 | +Before diving right into writing code, you need to install some tooling in order to compile your contract. |
| 11 | +CosmWasm is luckily rather self-contained and therefore needs little external tooling to compile. |
| 12 | +Our only external dependency is Rust, which you need to install for your platform. |
| 13 | + |
| 14 | +::: tip :bulb: Tip |
| 15 | + We recommend installing Rust using the official [rustup installer]. This makes it easy to stay on |
| 16 | + the most recent Rust version and to install compiler targets. |
| 17 | +::: |
| 18 | + |
| 19 | +::: tip :bulb: Tip |
| 20 | + For production builds you probably also want to install [Docker], too. |
| 21 | + <br /> This is because we offer the [CosmWasm Optimizing Compiler], which uses |
| 22 | + a Docker image to build the smallest contract possible in a deterministic fashion. |
| 23 | + |
| 24 | + ::: details Additional information about the Optimizing Compiler |
| 25 | + |
| 26 | + Please note that this image is intended for reproducible production builds. |
| 27 | + It is _not_ optimized for development or in general environments where you |
| 28 | + want to iterate quickly. The builder is optimizing for size, not compilation speed. |
| 29 | + If you want to slim down your contract for development, you can do so by |
| 30 | + tweaking your Cargo profile. |
| 31 | + |
| 32 | + ```toml |
| 33 | + [profile.dev] |
| 34 | + lto = "thin" |
| 35 | + strip = true |
| 36 | + ``` |
| 37 | + |
| 38 | + If you want to build with native tools, you might miss out on determinism, but you can still build your |
| 39 | + contract into a small size like so: |
| 40 | + |
| 41 | + ```shell |
| 42 | + RUSTFLAGS="-C link-arg=-s" cargo build --release --lib --target=wasm32-unknown-unknown |
| 43 | + wasm-opt -Os --signext-lowering "target/wasm32-unknown-unknown/release/my-contract.wasm" -o "artifacts/my-contract.wasm" |
| 44 | + ``` |
| 45 | + |
| 46 | + (Note: Replace `my-contract` with the name of your contract. You also need `wasm-opt` installed, |
| 47 | + which is part of the [binaryen] project.) |
| 48 | +::: |
| 49 | + |
| 50 | +After installing Rust, you need to add the WebAssembly target. This is needed so Rust knows how to |
| 51 | +build your code to WebAssembly. |
| 52 | + |
| 53 | +To install the target using `rustup`, run the following command: |
| 54 | + |
| 55 | +```shell |
| 56 | +$ rustup target add wasm32-unknown-unknown |
| 57 | +``` |
| 58 | + |
| 59 | +Perfect! |
| 60 | + |
| 61 | +Now that we set up the foundation we just need two more tools: |
| 62 | + |
| 63 | +1. `cargo-generate` |
| 64 | +2. `cargo-run-script` (this is required to later optimize your contract for production) |
| 65 | + |
| 66 | +To install those, run the following commands: |
| 67 | + |
| 68 | +```shell |
| 69 | +$ cargo install cargo-generate --features vendored-openssl |
| 70 | +$ cargo install cargo-run-script |
| 71 | +``` |
| 72 | + |
| 73 | +## Setting up the contract |
| 74 | + |
| 75 | +Now that the environment is all done, let's create the project! |
| 76 | + |
| 77 | +Luckily you don't need to start from scratch, we already took care of the most tedious parts of |
| 78 | +setting up a new project in form of a template! |
| 79 | + |
| 80 | +In order to generate a fresh project, run this command and off we go: |
| 81 | + |
| 82 | +<Callout>Make sure to change `PROJECT_NAME` to the name of your contract!</Callout> |
| 83 | + |
| 84 | +```shell |
| 85 | +$ cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME |
| 86 | +``` |
| 87 | + |
| 88 | +Now you should have a ready contract project in a new folder called `PROJECT_NAME` (or whatever you changed it to). |
0 commit comments