Generate contract

Generate contract

With the environment setup in the getting-started section, we can dive into setting up a simple contract.

Run the cargo-generate command and give your contract a name. For simplicity in learning Sylvia framework we will name it counter. This way, we will avoid getting into possibly convoluted business logic and focus mainly on learning the tool.

$ cargo generate CosmWasm/sylvia-template
   Favorite `CosmWasm/sylvia-template` not found in config, using it as a git repository: https://github.com/CosmWasm/sylvia-template.git
   Project Name: counter
   Destination: /home/user/counter ...
   project-name: counter ...
   Generating template ...
   Moving generated files into: `/home/user/counter`...
Initializing a fresh Git repository
   Done! New project created /home/user/counter

Inspect Cargo.toml

First, we will investigate the Cargo.toml.

[package]
name = "counter"
version = "0.1.0"
edition = "2021"
 
[features]
library = []
 
[lib]
crate-type = ["cdylib", "rlib"]
 
[dependencies]
cosmwasm-std = { version = "2.0.2", features = ["staking"] }
sylvia = "1.0.2"
serde = "1.0.198"
schemars = "0.8"
cosmwasm-schema = "2.0.2"
cw-storage-plus = "2.0.0"
 
[dev-dependencies]
sylvia = { version = "1.0.2", features = ["mt"] }

The sylvia-template contains a library feature. Its goal is to hide the entry points of our contract for other developers who might want to use it in their contracts. Otherwise, there would be conflicting implementations.

This template also setup crate-type with two values:

  • cdylib - Required to compile the contract into wasm,
  • rlib - Required for the contract to be used as a dependency in another contract.

The sylvia-template also defines some dependencies.

  • cosmwasm-std (opens in a new tab) - 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.
  • sylvia (opens in a new tab) - Crate, this documentation describes. It provides us with three procedural macros: entry_points, contract, and interface. We will expand on them later in the book.
  • schemars (opens in a new tab) - 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 (opens in a new tab) - Similar to schemars. This crate expands on schemars and provides us with trait QueryResponses (opens in a new tab) which ties query variants to their responses. We will expand on that later.
  • serde (opens in a new tab) - Framework for serializing and deserializing Rust data structures efficiently and generically.

In the case of dev-dependencies, we will also enable the mt flag in Sylvia to generate Multitest helpers in the test environment.

Project structure

The structure present in the sylvia-template is an example and you can set up the project in any way you want.

  • src/lib.rs - exposes contract modules.
  • src/contract.rs - main module in which the contract is defined.
  • src/multitest/ - module in which we define our tests. It's a good practice to separate the tests between files semantically.
  • src/bin/schema.rs - builds into a binary which generates the contract schema.
  • .cargo/config - exposes some aliases to make working with the project simpler.
    • cargo wasm - run cargo build with additional options to compile a proper wasm binary.
    • cargo wasm-debug - same as cargo wasm, but without --release profile to provide more debugging symbols.
    • cargo schema - builds and runs the src/schema.rs for simpler schema generation.