Skip to content

Commit 94a08a7

Browse files
committed
Sylvia: Describe forwarding attributes to message fields
1 parent 3696850 commit 94a08a7

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

src/pages/sylvia/basics/contract-structure.mdx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ In the first two lines, we see the usage of two macros:
6565
point collision.
6666

6767
- [`contract`](../macros/contract) - Parses every method inside the `impl` block marked with the
68-
`[sv::msg(...)]` attribute and create proper messages and utilities like helpers for
69-
[`MultiTest`](../../cw-multi-test).
68+
[`[sv::msg(...)]`](../attributes/msg) attribute and create proper messages and utilities like
69+
helpers for [`MultiTest`](../../cw-multi-test).
7070

71-
This simple example also has the `sv::msg(...)` attributes. Sylvia macros distinguish the if message
72-
should be generated from the marked method and of what type.
71+
This simple example also has the [`sv::msg(...)`](../attributes/msg) attributes. Sylvia macros
72+
distinguish the if message should be generated from the marked method and of what type.
7373

7474
CosmWasm contract requires the `instantiate` message, and it is mandatory to specify it for the
75-
`contract` macro. We have to provide it with the proper context type:
75+
[`contract`](../macros/contract) macro. We have to provide it with the proper context type:
7676
[`InstantiateCtx`](https://docs.rs/sylvia/latest/sylvia/types/struct.InstantiateCtx.html). Another
77-
mandatory method is the `new`, as contract fields are out of scope for the `contract` macro, and
78-
otherwise we wouldn't be able to create the contract object in message dispatching.
77+
mandatory method is the `new`, as contract fields are out of scope for the
78+
[`contract`](../macros/contract) macro, and otherwise we wouldn't be able to create the contract
79+
object in message dispatching.
7980

8081
Context gives us access to the blockchain state, information about our contract, and the sender of
8182
the message. We return the
@@ -85,16 +86,16 @@ standard CosmWasm error
8586
[`Response`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Response.html).
8687

8788
The template contract also contains a query and an exec messages. Each type of message in CosmWasm
88-
supports different contexts. F.e. the
89-
[`QueryCtx`](https://docs.rs/sylvia/latest/sylvia/types/struct.QueryCtx.html) exposes to the user an
89+
supports different contexts. F.e. the [`QueryCtx`](../types/context) exposes to the user an
9090
immutable [`Deps`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Deps.html) as by design,
91-
queries should never mutate the state. This is not the case for the
92-
[`ExecCtx`](https://docs.rs/sylvia/latest/sylvia/types/struct.ExecCtx.html) and `InstantiateCtx`
93-
which exposes the [`DepsMut`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.DepsMut.html).
91+
queries should never mutate the state. This is not the case for the [`ExecCtx`](../types/context)
92+
and `InstantiateCtx` which exposes the
93+
[`DepsMut`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.DepsMut.html).
9494

9595
Fell free expanding the macro now and seeing what Sylvia generates. It might be overwhelming, as
9696
there will be a lot of things generated that seem not relevant to our code, so for the bare minimum,
97-
check the `InstantiateMsg` and its `impl` block.
97+
check the [`InstantiateMsg`](../macros/generated-types/message-types#contract-messages) and its
98+
`impl` block.
9899

99100
Sylvia doesn't generate anything magical, but regular CosmWasm contract types customized based on
100101
the provided methods and attributes. This means that the Sylvia contract is fully interoperational

src/pages/sylvia/macros/contract.mdx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import { Callout } from "nextra/components";
66

77
# Contract
88

9-
Use the `contract` macro to generate contract messages
9+
Use the [`contract`](https://docs.rs/sylvia/latest/sylvia/attr.contract.html) macro to generate
10+
contract messages
1011

11-
<Callout>Use `contract` macro only on top of struct impl blocks</Callout>
12+
<Callout>
13+
Use [`contract`](https://docs.rs/sylvia/latest/sylvia/attr.contract.html) macro only on top of
14+
struct impl blocks
15+
</Callout>
1216

1317
## Attributes
1418

15-
List of attributes supported by `contract` macro:
19+
List of attributes supported by
20+
[`contract`](https://docs.rs/sylvia/latest/sylvia/attr.contract.html) macro:
1621

1722
- [`custom`](../attributes/custom)
1823
- [`error`](../attributes/error)
@@ -142,6 +147,31 @@ This is a standard way to create generic structs in Rust. Two important things t
142147
fulfill their trait bounds. In most cases it's enough to add the
143148
`sylvia::types::CustomMsg + \'static` bounds.
144149

150+
## Forwarding attributes to fields
151+
152+
The [`contract`](https://docs.rs/sylvia/latest/sylvia/attr.contract.html) macro can forward
153+
attributes to the fields of the messages.
154+
155+
```rust {5}
156+
#[sv::msg(instantiate)]
157+
fn instantiate(
158+
&self,
159+
ctx: InstantiateCtx,
160+
#[serde(default)] value: String,
161+
) -> StdResult<Response> {
162+
Ok(Response::new())
163+
}
164+
```
165+
166+
The output of the above code will be:
167+
168+
```rust {2}
169+
pub struct InstantiateMsg {
170+
#[serde(default)]
171+
pub value: String,
172+
}
173+
```
174+
145175
## Good practices
146176

147177
### Prefer generic custom types

src/pages/sylvia/macros/interface.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,28 @@ pub trait Interface {
103103
fn interface_exec(&self, ctx: ExecCtx, param: Self::MyType) -> StdResult<Response>;
104104
}
105105
```
106+
107+
## Forwarding attributes to fields
108+
109+
The [`interface`](https://docs.rs/sylvia/latest/sylvia/attr.interface.html) macro can forward
110+
attributes to the fields of the messages.
111+
112+
```rust {5}
113+
#[sv::msg(exec)]
114+
fn exec(
115+
&self,
116+
ctx: ExecCtx,
117+
#[serde(default)] value: String,
118+
) -> Result<Response, Self::Error>;
119+
```
120+
121+
The output of the above code will be:
122+
123+
```rust {2}
124+
pub enum MyInterfaceExecMsg {
125+
Exec {
126+
#[serde(default)]
127+
value: String,
128+
},
129+
}
130+
```

0 commit comments

Comments
 (0)