|
| 1 | +--- |
| 2 | +tags: ["tutorial", "sylvia"] |
| 3 | +--- |
| 4 | + |
| 5 | +import { Callout } from "nextra/components"; |
| 6 | + |
| 7 | +# Create a Rust project |
| 8 | + |
| 9 | +The easiest and recommended way to start working on a new [Sylvia](https://crates.io/crates/sylvia) |
| 10 | +contract is to generate it from the |
| 11 | +[`sylvia-template`](https://github.com/CosmWasm/sylvia-template). |
| 12 | + |
| 13 | +```shell copy filename="TERMINAL" |
| 14 | +cargo generate CosmWasm/sylvia-template |
| 15 | +``` |
| 16 | + |
| 17 | +The [`sylvia-template`](https://github.com/CosmWasm/sylvia-template) will generate a lot of code for |
| 18 | +you. Because this tutorial aims to guide you step-by-step through the process of creating your first |
| 19 | +contract we will omit the use of the template and show you how to create it from the start. |
| 20 | + |
| 21 | +## New project |
| 22 | + |
| 23 | +Smart contracts are Rust library crates. We will start with creating one: |
| 24 | + |
| 25 | +```shell copy filename="TERMINAL" |
| 26 | +cargo new --lib ./contract |
| 27 | +``` |
| 28 | + |
| 29 | +You created a simple Rust library, but it is not yet ready to be a smart contract. The first thing |
| 30 | +to do is to update the `Cargo.toml` file: |
| 31 | + |
| 32 | +```toml copy filename="Cargo.toml" |
| 33 | +[package] |
| 34 | +name = "contract" |
| 35 | +version = "0.1.0" |
| 36 | +edition = "2021" |
| 37 | + |
| 38 | +[lib] |
| 39 | +crate-type = ["cdylib"] |
| 40 | + |
| 41 | +[dependencies] |
| 42 | +sylvia = "1.3.1" |
| 43 | +``` |
| 44 | + |
| 45 | +<Callout> |
| 46 | + Notice lack of `cosmwasm-std` dependency. Sylvia reexports it as well as other necessary crates |
| 47 | + for CosmWasm smart contracts. You can find list of reexported crates in the |
| 48 | + [documentation](https://docs.rs/sylvia/latest/sylvia/#reexports). |
| 49 | +</Callout> |
| 50 | + |
| 51 | +As you can see, I added a `crate-type` field for the library section. Generating the |
| 52 | +[`cdylib`](https://doc.rust-lang.org/reference/linkage.html) is required to create a proper web |
| 53 | +assembly binary. The downside of this is that such a library cannot be used as a dependency for |
| 54 | +other Rust crates - for now, it is not needed, but later we will show how to approach reusing |
| 55 | +contracts as dependencies. |
| 56 | + |
| 57 | +Additionally, we added some core dependencies for smart contracts: |
| 58 | + |
| 59 | +- [cosmwasm-std](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/) - Crate that is a standard |
| 60 | + library for smart contracts. It provides essential utilities for communication with the outside |
| 61 | + world, helper functions, and types. Every smart contract we will build will use this dependency. |
| 62 | +- [sylvia](https://docs.rs/sylvia/latest/sylvia/) - Crate, we will learn in this book. It provides |
| 63 | + us with three procedural macros: `entry_points`, `contract` and `interface`. I will expand on them |
| 64 | + later in the book. |
| 65 | +- [schemars](https://docs.rs/schemars/latest/schemars/index.html) - Crate used to create JSON schema |
| 66 | + documents for our contracts. It is automatically derived on types generated by ^sylvia and will be |
| 67 | + later used to provide concise API for blockchain users, who might not be Rust developers. |
| 68 | +- [cosmwasm-schema](https://docs.rs/cosmwasm-schema/latest/cosmwasm_schema/) - Similar to |
| 69 | + `schemars`. This crate expands on `schemars` and provides us with trait |
| 70 | + [`QueryResponses`](https://docs.rs/cosmwasm-schema/latest/cosmwasm_schema/trait.QueryResponses.html) |
| 71 | + which ties query variants to their responses. I will expand on that later in the book. |
| 72 | +- [serde](https://docs.rs/serde/latest/serde/) - Framework for serializing and deserializing Rust |
| 73 | + data structures efficiently and generically. |
0 commit comments