Skip to content

Commit 5d2c647

Browse files
committed
Sylvia tutorial entry points
1 parent 7ff5a96 commit 5d2c647

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed
Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1+
---
2+
tags: ["tutorial, sylvia"]
3+
---
4+
5+
import { Callout } from "nextra/components";
6+
17
# Entry points
28

39
Typical Rust application starts with the `fn main()` function called by the operating system. Smart
410
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.
11+
called [entry point](../../core/entrypoints) is executed. Unlike native applications, which have
12+
only a single `main` entry point, smart contracts have a couple of them, each corresponding to
13+
different message type.
814

915
To start, we will go with three basic entry points:
1016

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.
17+
- [instantiate](../../core/entrypoints/instantiate) is called once per smart contract lifetime; you
18+
can think about it as a constructor or initializer of a contract.
19+
- [execute](../../core/entrypoints/execute) for handling messages which can modify contract state;
20+
they are used to perform some actual actions.
21+
- [query](../../core/entrypoints/query) for handling messages requesting some information from a
22+
contract; unlike [execute](../../core/entrypoints/execute), they can never alter any contract
23+
state, and are used in a similar manner to database queries.
1824

1925
## Generate entry points
2026

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.
27+
Sylvia provides an attribute macro named [`entry_points`](../../sylvia/macros/entry-points). In most
28+
cases, your entry point will just dispatch received messages to the handler, so it's not necessary
29+
to manually create them, and we can rely on a macro to do that for us.
2530

26-
Let's add the **`entry_points`** attribute macro to our contract:
31+
Let's add the [`entry_points`](../../sylvia/macros/entry-points) attribute macro to our contract:
2732

28-
```rust,noplayground
29-
use cosmwasm_std::{Response, StdResult};
30-
use sylvia::types::InstantiateCtx;
33+
```rust {3, 7} copy filename="src/contract.rs" template="sylvia/empty"
34+
use sylvia::ctx::InstantiateCtx;
35+
use sylvia::cw_std::{Response, StdResult};
3136
use sylvia::{contract, entry_points};
3237

3338
pub struct CounterContract;
@@ -46,28 +51,31 @@ impl CounterContract {
4651
}
4752
```
4853

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.
54+
<Callout>
55+
Note that [`entry_points`](../../sylvia/macros/entry-points) is added above the
56+
[`contract`](../../sylvia/macros/contract). It is because
57+
[`contract`](../../sylvia/macros/contract) removes attributes like
58+
[`sv::msg(...)`](../../sylvia/macros/attributes/msg) on which both these macros rely. Always
59+
remember to place [`entry_points`](../../sylvia/macros/entry-points) first.
60+
</Callout>
5161

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
62+
Sylvia generates entry points with the [`entry_points`](../../sylvia/macros/entry-points) attribute
5663
macro. Its purpose is to wrap the whole entry point to the form the Wasm runtime understands. The
5764
proper Wasm entry points can use only basic types supported natively by Wasm specification, and Rust
5865
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.
66+
so CosmWasm creators delivered the
67+
[`entry_point`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/attr.entry_point.html) macro. It
68+
creates the raw Wasm entry point, calling the decorated function internally and doing all the magic
69+
required to build our high-level Rust arguments from arguments passed by Wasm runtime.
6270

6371
Now, when our contract has a proper entry point, let's build it and check if it's correctly defined:
6472

65-
```shell
66-
contract $ cargo build --release --target wasm32-unknown-unknown --lib
67-
Finished release [optimized] target(s) in 0.03s
73+
```shell copy filename="TERMINAL"
74+
cargo build --release --target wasm32-unknown-unknown --lib
75+
```
6876

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"}
77+
```shell filename="TERMINAL"
78+
Available capabilities: {"cosmwasm_1_3", "cosmwasm_2_0", "cosmwasm_1_2", "stargate", "iterator", "cosmwasm_1_1", "cosmwasm_1_4", "staking", "cosmwasm_2_1"}
7179

7280
target/wasm32-unknown-unknown/release/contract.wasm: pass
7381

@@ -76,5 +84,5 @@ All contracts (1) passed checks!
7684

7785
## Next step
7886

79-
Well done! We have now a proper `CosmWasm` contract. Let's add some state to it, so it will actually
87+
Well done! We have now a proper CosmWasm contract. Let's add some state to it, so it will actually
8088
be able to do something.

src/pages/tutorial/sylvia-contract/first-messages.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ cosmwasm-check target/wasm32-unknown-unknown/release/contract.wasm
8080
The output should look like this:
8181

8282
```shell filename="TERMINAL"
83-
Available capabilities: {"stargate", "staking", "cosmwasm_1_3", "cosmwasm_2_0", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_4", "iterator"}
83+
Available capabilities: {"cosmwasm_1_3", "cosmwasm_2_0", "cosmwasm_1_2", "stargate", "iterator", "cosmwasm_1_1", "cosmwasm_1_4", "staking", "cosmwasm_2_1"}
8484

8585
target/wasm32-unknown-unknown/release/contract.wasm: pass
8686

0 commit comments

Comments
 (0)