Skip to content

Commit ef074b7

Browse files
committed
Sylvia contract creation
1 parent 1929600 commit ef074b7

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

src/pages/tutorial/_meta.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"setup-environment": "Environment setup",
3-
"cw-contract": "CosmWasm Contract"
3+
"cw-contract": "CosmWasm Contract",
4+
"sylvia-contract": "Sylvia Contract"
45
}

src/pages/tutorial/setup-environment.mdx

+13-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ passes:
8282
cosmwasm-check ../../target/wasm32-unknown-unknown/release/
8383
```
8484

85-
```shell
85+
```shell filename="TERMINAL"
8686
cw-plus/contracts/cw1-whitelist $ cosmwasm-check
8787
../../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm Available capabilities: {"iterator",
8888
"cosmwasm_1_1", "cosmwasm_1_2", "stargate", "staking"}
@@ -91,3 +91,15 @@ cw-plus/contracts/cw1-whitelist $ cosmwasm-check
9191

9292
All contracts (1) passed checks!
9393
```
94+
95+
## Macro expansion
96+
97+
In VSCode you can hower over a macro like [`#[contract]`](../sylvia/macros/contract), do `shift+p`
98+
and then type: `rust analyzer: Expand macro recursively`. This will open a window with a fully
99+
expanded macro, which you can browse. In Vim you can consider installing the
100+
[rustaceanvim](https://github.com/mrcjkb/rustaceanvim) plugin. You can also use `cargo expand` tool
101+
from CLI, like this:
102+
103+
```shell copy filename="TERMINAL"
104+
cargo expand --lib
105+
```
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sylvia contract writting tutorial
2+
3+
This section is a step-by-step guide on how to write a Sylvia contract.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contract-creation": "Contract creation"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)