|
| 1 | +# Entry points |
| 2 | + |
| 3 | +Typical Rust application starts with the `fn main()` function called by the operating system. Smart |
| 4 | +contracts are not significantly different. When the message is sent to the contract, a function |
| 5 | +called "entry point" is executed. Unlike native applications, which have only a single `main` entry |
| 6 | +point, smart contracts have a couple of them, each corresponding to different message type: |
| 7 | +`instantiate`, `execute`, `query`, `sudo`, `migrate` and more. |
| 8 | + |
| 9 | +To start, we will go with three basic entry points: |
| 10 | + |
| 11 | +- **`instantiate`** is called once per smart contract lifetime; you can think about it as a |
| 12 | + constructor or initializer of a contract. |
| 13 | +- **`execute`** for handling messages which can modify contract state; they are used to perform some |
| 14 | + actual actions. |
| 15 | +- **`query`** for handling messages requesting some information from a contract; unlike |
| 16 | + **`execute`**, they can never alter any contract state, and are used in a similar manner to |
| 17 | + database queries. |
| 18 | + |
| 19 | +## Generate entry points |
| 20 | + |
| 21 | +^Sylvia provides an attribute macro named |
| 22 | +[`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html). In most cases, your |
| 23 | +entry point will just dispatch received messages to the handler, so it's not necessary to manually |
| 24 | +create them, and we can rely on a macro to do that for us. |
| 25 | + |
| 26 | +Let's add the **`entry_points`** attribute macro to our contract: |
| 27 | + |
| 28 | +```rust,noplayground |
| 29 | +use cosmwasm_std::{Response, StdResult}; |
| 30 | +use sylvia::types::InstantiateCtx; |
| 31 | +use sylvia::{contract, entry_points}; |
| 32 | +
|
| 33 | +pub struct CounterContract; |
| 34 | +
|
| 35 | +#[entry_points] |
| 36 | +#[contract] |
| 37 | +impl CounterContract { |
| 38 | + pub const fn new() -> Self { |
| 39 | + Self |
| 40 | + } |
| 41 | +
|
| 42 | + #[sv::msg(instantiate)] |
| 43 | + pub fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult<Response> { |
| 44 | + Ok(Response::default()) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Note that **`#[entry_points]`** is added above the **`#[contract]`**. It is because |
| 50 | +**`#[contract]`** removes attributes like **`#[sv::msg(...)]`** on which both these macros rely. |
| 51 | + |
| 52 | +Always remember to place **`#[entry_points]`** first. |
| 53 | + |
| 54 | +^Sylvia generates entry points with |
| 55 | +[`#[entry_point]`](https://docs.rs/cosmwasm-std/1.3.1/cosmwasm_std/attr.entry_point.html) attribute |
| 56 | +macro. Its purpose is to wrap the whole entry point to the form the Wasm runtime understands. The |
| 57 | +proper Wasm entry points can use only basic types supported natively by Wasm specification, and Rust |
| 58 | +structures and enums are not in this set. Working with such entry points would be overcomplicated, |
| 59 | +so CosmWasm creators delivered the `entry_point` macro. It creates the raw Wasm entry point, calling |
| 60 | +the decorated function internally and doing all the magic required to build our high-level Rust |
| 61 | +arguments from arguments passed by Wasm runtime. |
| 62 | + |
| 63 | +Now, when our contract has a proper entry point, let's build it and check if it's correctly defined: |
| 64 | + |
| 65 | +```shell |
| 66 | +contract $ cargo build --release --target wasm32-unknown-unknown --lib |
| 67 | + Finished release [optimized] target(s) in 0.03s |
| 68 | + |
| 69 | +contract $ cosmwasm-check target/wasm32-unknown-unknown/release/contract.wasm |
| 70 | +Available capabilities: {"stargate", "cosmwasm_1_3", "cosmwasm_1_1", "cosmwasm_1_2", "staking", "iterator"} |
| 71 | + |
| 72 | +target/wasm32-unknown-unknown/release/contract.wasm: pass |
| 73 | + |
| 74 | +All contracts (1) passed checks! |
| 75 | +``` |
| 76 | + |
| 77 | +## Next step |
| 78 | + |
| 79 | +Well done! We have now a proper `CosmWasm` contract. Let's add some state to it, so it will actually |
| 80 | +be able to do something. |
0 commit comments