Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sylvia contract creation #208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/pages/tutorial/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"setup-environment": "Environment setup",
"cw-contract": "CosmWasm Contract"
"cw-contract": "CosmWasm Contract",
"sylvia-contract": "Sylvia Contract"
}
14 changes: 13 additions & 1 deletion src/pages/tutorial/setup-environment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ passes:
cosmwasm-check ../../target/wasm32-unknown-unknown/release/
```

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

All contracts (1) passed checks!
```

## Macro expansion

In VSCode you can hover over a macro like [`#[contract]`](../sylvia/macros/contract), do `shift+p`
and then type: `rust analyzer: Expand macro recursively`. This will open a window with a fully
expanded macro, which you can browse. In Vim you can consider installing the
[rustaceanvim](https://github.com/mrcjkb/rustaceanvim) plugin. You can also use `cargo expand` tool
from CLI, like this:

```shell copy filename="TERMINAL"
cargo expand --lib
```
3 changes: 3 additions & 0 deletions src/pages/tutorial/sylvia-contract.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sylvia contract writing tutorial

This section is a step-by-step guide on how to write a Sylvia contract.
3 changes: 3 additions & 0 deletions src/pages/tutorial/sylvia-contract/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contract-creation": "Contract creation"
}
73 changes: 73 additions & 0 deletions src/pages/tutorial/sylvia-contract/contract-creation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
tags: ["tutorial", "sylvia"]
---

import { Callout } from "nextra/components";

# Create a Rust project

The easiest and recommended way to start working on a new [Sylvia](https://crates.io/crates/sylvia)
contract is to generate it from the
[`sylvia-template`](https://github.com/CosmWasm/sylvia-template).

```shell copy filename="TERMINAL"
cargo generate CosmWasm/sylvia-template
```

The [`sylvia-template`](https://github.com/CosmWasm/sylvia-template) will generate a lot of code for
you. Because this tutorial aims to guide you step-by-step through the process of creating your first
contract we will omit the use of the template and show you how to create it from the start.

## New project

Smart contracts are Rust library crates. We will start with creating one:

```shell copy filename="TERMINAL"
cargo new --lib ./contract
```

You created a simple Rust library, but it is not yet ready to be a smart contract. The first thing
to do is to update the `Cargo.toml` file:

```toml copy filename="Cargo.toml"
[package]
name = "contract"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
sylvia = "1.3.1"
```

<Callout>
Notice lack of `cosmwasm-std` dependency. Sylvia reexports it as well as other necessary crates
for CosmWasm smart contracts. You can find list of reexported crates in the
[documentation](https://docs.rs/sylvia/latest/sylvia/#reexports).
</Callout>

As you can see, I added a `crate-type` field for the library section. Generating the
[`cdylib`](https://doc.rust-lang.org/reference/linkage.html) is required to create a proper web
assembly binary. The downside of this is that such a library cannot be used as a dependency for
other Rust crates - for now, it is not needed, but later we will show how to approach reusing
contracts as dependencies.

Additionally, we added a single dependency - [sylvia](https://docs.rs/sylvia/latest/sylvia/). This
is the only dependency you need to start since [sylvia](https://docs.rs/sylvia/latest/sylvia/)
reexports all the necessary crates for CosmWasm smart contracts. The most important reexported
crates are:

- [cosmwasm-std](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/) - Crate that is a standard
library for smart contracts. It provides essential utilities for communication with the outside
world, helper functions, and types. Every smart contract we will build will use this dependency.
- [schemars](https://docs.rs/schemars/latest/schemars/index.html) - Crate used to create JSON schema
documents for our contracts. It is automatically derived on types generated by ^sylvia and will be
later used to provide concise API for blockchain users, who might not be Rust developers.
- [cosmwasm-schema](https://docs.rs/cosmwasm-schema/latest/cosmwasm_schema/) - Similar to
`schemars`. This crate expands on `schemars` and provides us with trait
[`QueryResponses`](https://docs.rs/cosmwasm-schema/latest/cosmwasm_schema/trait.QueryResponses.html)
which ties query variants to their responses. I will expand on that later in the book.
- [serde](https://docs.rs/serde/latest/serde/) - Framework for serializing and deserializing Rust
data structures efficiently and generically.
Loading