Basic Anatomy of an Applet
Importing the SDK
All contracts will import the Weil SDK (weil_rs), enabling them to access the execution environment, call other contracts, transfer tokens, and much more. You can also use third-party libraries, though some might not work due to the limitations of the contract runtime.
Contract's Main Structure
The contract is described through a structure:
- The struct define which data the contract stores
- The methods on the struct defines the exposed interface of the contract.
Contract Struct Macro
#[smart_contract]
is used to annotate theimpl
block of generated trait implementation by the contract state. This tell the SDK to capturequery
andmutate
inside theimpl
block and convert them into WASM exported functions which can be invoked externally.#[constructor]
macro tell the SDK to call the method at the time of deployment. Usually this method is used to define default initial contract state.#[query]
and#[mutate]
macros are for annotating the methods which tell the SDK to insert appropriate runtime specific persistence wrappers around the exported methods for example: for mutate annotated methods, there are possible changes to the contract state which should be persisted back to the storage.
Storage: Contract State
We call the data stored in the contract the contract's state. In our counter example, the contract stores a single usize
value, and the state starts initialized with the default value 0.
Note: We will cover more about the contract's state in the state section.
Read Only Functions
Contract's functions can be read-only, meaning they don't modify the state. They are annotated with macro #[query]
.
Note: We will cover more about function types in the functions section.
State Mutating Functions
Functions that modify the state or call other contracts are considered state mutating functions. They are annotated with macro #[mutate]
. State mutating functions are different because they are submitted to the Blockchain platform and goes through the consensus protocol.