1
+ ---
2
+ tags : ["tutorial, sylvia"]
3
+ ---
4
+
5
+ import { Callout } from " nextra/components" ;
6
+
1
7
# Entry points
2
8
3
9
Typical Rust application starts with the ` fn main() ` function called by the operating system. Smart
4
10
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 .
8
14
9
15
To start, we will go with three basic entry points:
10
16
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.
18
24
19
25
## Generate entry points
20
26
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.
25
30
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:
27
32
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 } ;
31
36
use sylvia :: {contract, entry_points};
32
37
33
38
pub struct CounterContract ;
@@ -46,28 +51,31 @@ impl CounterContract {
46
51
}
47
52
```
48
53
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 >
51
61
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
56
63
macro. Its purpose is to wrap the whole entry point to the form the Wasm runtime understands. The
57
64
proper Wasm entry points can use only basic types supported natively by Wasm specification, and Rust
58
65
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.
62
70
63
71
Now, when our contract has a proper entry point, let's build it and check if it's correctly defined:
64
72
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
+ ```
68
76
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 " }
71
79
72
80
target/wasm32-unknown-unknown/release/contract.wasm: pass
73
81
@@ -76,5 +84,5 @@ All contracts (1) passed checks!
76
84
77
85
## Next step
78
86
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
80
88
be able to do something.
0 commit comments