-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
# Create a Rust project | ||
# Creating a Rust Project for CosmWasm Smart Contracts | ||
|
||
As smart contracts are Rust library crates, we will start with creating one: | ||
Developing smart contracts for the CosmWasm platform requires a structured approach, starting from setting up a Rust project. This dedicated guide will walk you through creating a Rust library crate designed to work as a CosmWasm smart contract, including configuring the cargo.toml file to meet the requirements of compiling to WebAssembly (Wasm), the format needed for deploying on the CosmWasm platform. | ||
|
||
### Step 1: Initialize Your Rust Library | ||
|
||
You'll need to create a new Rust library to kick off your smart contract development. Open your terminal and execute the following command, which creates a new directory named empty-contract and initializes a Rust project configured as a library: | ||
```rust | ||
cargo new --lib ./empty-contract | ||
``` | ||
$ cargo new --lib ./empty-contract | ||
``` | ||
This command sets up a basic Rust project structure, including a Cargo. The toml file will be used to manage your project's settings and dependencies, and a src directory will be used where your Contract's Rust source code will reside. | ||
|
||
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: | ||
### Step 2: Configuring the Cargo.toml file | ||
|
||
After setting up your project, the next crucial step is configuring the cargo.toml file. This file resides at the root of your project and dictates how your project is built. Navigate to the empty-contract directory and open the cargo.toml file in your preferred editor to make the following adjustments: | ||
|
||
```toml | ||
[package] | ||
name = "contract" | ||
[package] name = "contract" | ||
version = "0.1.0" | ||
edition = "2021" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
[dependencies] | ||
cosmwasm-std = { version = "1.0.0-beta8", features = ["staking"] } | ||
|
||
``` | ||
### Explanation of Key Configurations: | ||
|
||
- **[package]**: This section defines basic metadata about your project. The edition field specifies which edition of Rust you are targeting, with "2021" being the most recent as of this writing. | ||
- **[lib]**: By specifying crate-type as ["cdylib"], you are instructing Rust to compile your library into a dynamic library, specifically a WebAssembly (Wasm) binary. This is required for the Contract to run in the CosmWasm environment. Note that this configuration means the compiled library cannot be included as a dependency in other Rust crates. | ||
- **[dependencies]**: The cosmwasm-std dependency is essential for smart contract development on the CosmWasm platform. It acts as the standard library, providing the types, functions, and utilities necessary to interact with the blockchain. The version should be the latest stable version compatible with your project's requirements, and the features field enables specific functionalities, such as staking in this case, which may be necessary depending on your Contract's use case. | ||
|
||
As you can see, I added a `crate-type` field for the library section. Generating the `cdylib` 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. | ||
### Final Steps: | ||
|
||
Additionally, there is one core dependency for smart contracts: the `cosmwasm-std`. This crate is a | ||
standard library for smart contracts. It provides essential utilities for communication with the | ||
outside world and a couple of helper functions and types. Every smart contract we will build will | ||
use this dependency. | ||
With your Rust project correctly configured, you can start writing your smart contract code within the src/lib.rs file. This involves implementing the Contract's logic, including handling initialization, execute, and query operations according to the CosmWasm standard. | ||
|
||
As you develop your smart Contract, regularly compile and test your code to ensure that it meets the expected functionalities and security standards required for deployment on the blockchain. Utilizing the comprehensive tooling and resources available in the Rust and CosmWasm communities will aid in this process, helping you to develop robust, efficient, and secure smart contracts. |