In this tutorial, we will be following the official documentation to build a simple Options contract since it is an excellent place to start - but we will go the full way from absolute scratch. I will explaining all my steps. Here is the original documentation.
Taken from Investopedia, here is what options mean
Options are financial instruments that are derivatives based on the value of underlying securities such as stocks.
Cargo is a Rust package manager. We will need cargo-generate
, which allows us to set up Rust code using defined templates.
We will be using this repo to scaffold our project. For the
This will generate a folder for us with the general cosmwasm template.
To go forward, it will be important to understand how the files work and interact with each other.
If you go to src
folder, you'll find lib.rs
: This is where your wasm bindings are contained. Think of it as a wrapper for the smart contract functionalities around Rust functions. We won't be needing to touch this during this tutorial.
To start, let's have a look at msg.rs
. This file is the entry point of our development.
It might be a little intimidating reading Rust for the first time, so we will break it down step-by-step using comments.
#[derive()]
is an attribute in Rust that allows new items to be automatically created for data strctures. InitMsg structure has initial value that initializes the smart contract using the code and feeds in the data. Using the derive attribute, we implement specified traits (think of them as abstract methods equivalent from C++) for the structure.
The HumanAddr
would be the prefixed public address of the recipient.
QueryMsg
is a way for client to query the Smart Contract state. We will setup Config{}
. ConfigResponse = State
might seem a little confusing, but we will go through it step by step.
State
handles the state of the database where smart contract data is stored and accessed.
This means that our database state is handled by the State
variable pub static CONFIG_KEY: &[u8] = b"config";
configures the instance key.
The State
structure allows for a creator, an owner, a collateral (coins sent with contract message), counter offer (strike price) and TTL.
Here we define a function config
for which the parameter is storage
. Storage can be of two types: Singleton and Structured. In Singleton, the storage saves one instance of the structure and has one unique key. Structured storage is complex and structured with different types of data relations. While this tutorial uses only a singleton storage, more complicated dApps will require more complicated storage formats - thus structured. For now, let us create a function that can access the state as read-only.
Now over to understanding contract.rs
in part 2 Let's get the party started!