Skip to content

Commit 49db9b7

Browse files
committed
Added conventions chapter.
1 parent ec0e41c commit 49db9b7

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

.vitepress/config.mts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,8 @@ export default withMermaid({
1616
items: [
1717
{text: 'Introduction', link: '/guide/cosmwasm-core/introduction'},
1818
{text: 'Installation', link: '/guide/cosmwasm-core/installation'},
19-
{
20-
text: 'Entrypoints', link: '/guide/cosmwasm-core/entrypoints/entrypoints',
21-
items: [
22-
{text: 'Instantiate', link: '/guide/cosmwasm-core/entrypoints/instantiate'},
23-
{text: 'Execute', link: '/guide/cosmwasm-core/entrypoints/execute'},
24-
{text: 'Query', link: '/guide/cosmwasm-core/entrypoints/query'},
25-
{text: 'Migrate', link: '/guide/cosmwasm-core/entrypoints/migrate'},
26-
{text: 'Sudo', link: '/guide/cosmwasm-core/entrypoints/sudo'},
27-
{text: 'Reply', link: '/guide/cosmwasm-core/entrypoints/reply'},
28-
]
29-
},
30-
{
31-
text: 'Architecture', link: '/guide/cosmwasm-core/architecture/architecture',
32-
items: [
33-
{text: 'Actor model', link: '/guide/cosmwasm-core/architecture/actor-model'},
34-
{text: 'Gas', link: '/guide/cosmwasm-core/architecture/gas'},
35-
{text: 'Transactions', link: '/guide/cosmwasm-core/architecture/transactions'},
36-
]
37-
}
19+
{text: 'Entrypoints', link: '/guide/cosmwasm-core/entrypoints/entrypoints'},
20+
{text: 'Architecture', link: '/guide/cosmwasm-core/architecture/architecture'}
3821
]
3922
},
4023
],
@@ -64,6 +47,13 @@ export default withMermaid({
6447
{text: 'Transactions', link: '/guide/cosmwasm-core/architecture/transactions'},
6548
]
6649
},
50+
{
51+
text: 'Conventions', collapsed: true, link: '/guide/cosmwasm-core/conventions/conventions',
52+
items: [
53+
{text: 'Library feature', link: '/guide/cosmwasm-core/conventions/library-feature'},
54+
{text: 'Enum dispatch', link: '/guide/cosmwasm-core/conventions/enum-dispatch'},
55+
]
56+
},
6757
]
6858
},
6959
],
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Conventions
2+
3+
Just like with any platform, there are certain conventions on how to write contracts for CosmWasm.
4+
This section will give you a quick overview of the different conventions set in the CosmWasm
5+
ecosystem.
6+
7+
These conventions are about recommended programming patterns, and recommendations on how you should
8+
structure your contract to make it fit into the existing CosmWasm ecosystem better.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Enum dispatch
2+
3+
In most production contracts you want to handle multiple message types in a single contract.
4+
Unfortunately you can't create multiple endpoints of the same type in a single contract, so you need
5+
to dispatch based on the message type.
6+
7+
The most common way to do this is to use an enum to represent the different message types and then
8+
match on that enum in the endpoint.
9+
10+
::: code-group
11+
12+
```Rust [contract.rs]
13+
#[cw_serde]
14+
enum ExecuteMsg {
15+
Add { value: i32 },
16+
Subtract { value: i32 },
17+
}
18+
19+
#[cfg_attr(not(feature = "library"), entry_point)]
20+
pub fn execute(
21+
deps: DepsMut,
22+
env: Env,
23+
info: MessageInfo,
24+
msg: ExecuteMsg,
25+
) -> StdResult<Response> {
26+
match msg {
27+
ExecuteMsg::Add { value } => {
28+
// TODO: Add operation
29+
}
30+
ExecuteMsg::Subtract { value } => {
31+
// TODO: Subtract operation
32+
}
33+
}
34+
Ok(Response::new())
35+
}
36+
```
37+
38+
:::
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[`cw-plus` contracts]: https://github.com/CosmWasm/cw-plus
2+
[`cw4-stake` contract]: https://github.com/CosmWasm/cw-plus/blob/48bec694521655d5b3e688c51e4185f740ea4640/contracts/cw4-stake/Cargo.toml#L22-L24
3+
4+
# Library feature
5+
6+
In the ecosystem, there is the convention to gate the entrypoints of your contract behind a
7+
compile-time feature. The feature is conventionally called `library`.
8+
9+
So instead of using the `#[entry_point]` macro directly, you should do this:
10+
11+
::: code-group
12+
13+
```Rust [contract.rs]
14+
#[cfg_attr(not(feature = "library"), entry_point)]
15+
pub fn instantiate(
16+
deps: DepsMut,
17+
env: Env,
18+
info: MessageInfo,
19+
msg: InstantiateMsg,
20+
) -> StdResult<Response> {
21+
Ok(Response::new())
22+
}
23+
```
24+
25+
:::
26+
27+
::: tip :bulb: Tip
28+
You can see this in action in the [`cw-plus` contracts]. Here is an example in the [`cw4-stake` contract].
29+
:::
30+
31+
The rationale behind this is that currently using `#[entry_point]` exports special symbols from the binary,
32+
and these symbols **must** be unique.
33+
34+
That means if you have two `instantiate` endpoints annotated with `#[entry_point]` _somewhere_ in your project
35+
(that includes all your dependencies!), your contract will fail to compile.
36+
37+
Using this library feature will enable developers to use your contract as a dependency and reuse your code.

0 commit comments

Comments
 (0)