-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from CosmWasm/aw/core-conventions
Documentation about conventions
- Loading branch information
Showing
15 changed files
with
190 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
tags: ["core", "conventions"] | ||
--- | ||
|
||
import { Callout } from "nextra/components"; | ||
|
||
# Conventions | ||
|
||
Just like with any platform, there are certain conventions on how to write | ||
contracts for CosmWasm. This section will give you a quick overview over the | ||
different conventions set in the CosmWasm ecosystem. | ||
|
||
These conventions are about recommended programming patterns, and | ||
recommendations on how you should structure your contract to make it fit into | ||
the existing CosmWasm ecosystem better. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"library-feature": "Library feature", | ||
"enum-dispatch": "Enum dispatch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
tags: ["core", "conventions"] | ||
--- | ||
|
||
import { Callout } from "nextra/components"; | ||
|
||
# Enum dispatch | ||
|
||
In most production contracts you want to handle multiple message types in a | ||
single contract. Unfortunately you can't create multiple endpoints of the same | ||
type in a single contract, so you need to dispatch based on the message type. | ||
|
||
The most common way to do this is to use an enum to represent the different | ||
message types and then match on that enum in the endpoint. | ||
|
||
<Callout> | ||
If this sounds like something you don't want to do manually, we got you covered! | ||
The `sylvia` crate provides a procedural macro to generate the boilerplate code for you. | ||
|
||
You can find the documentation of the `sylvia` crate [here](../../sylvia.mdx). | ||
|
||
</Callout> | ||
|
||
```rust filename="contract.rs" template="core" | ||
#[cw_serde] | ||
enum ExecuteMsg { | ||
Add { value: i32 }, | ||
Subtract { value: i32 }, | ||
} | ||
|
||
#[entry_point] | ||
pub fn execute( | ||
deps: DepsMut, | ||
env: Env, | ||
info: MessageInfo, | ||
msg: ExecuteMsg, | ||
) -> StdResult<Response> { | ||
match msg { | ||
ExecuteMsg::Add { value } => { | ||
// TODO: Add operation | ||
} | ||
ExecuteMsg::Subtract { value } => { | ||
// TODO: Subtract operation | ||
} | ||
} | ||
|
||
Ok(Response::new()) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
tags: ["core", "conventions"] | ||
--- | ||
|
||
import { Callout } from "nextra/components"; | ||
|
||
# Library feature | ||
|
||
In the ecosystem, there is the convention to gate the entrypoints of your | ||
contract behind a compile-time feature. The feature is conventionally called | ||
`library`. | ||
|
||
So instead of doing this: | ||
|
||
```rust filename="contract.rs" template="core" | ||
#[entry_point] | ||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
``` | ||
|
||
You should do this: | ||
|
||
```rust filename="contract.rs" template="core" | ||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> StdResult<Response> { | ||
Ok(Response::new()) | ||
} | ||
``` | ||
|
||
<Callout> | ||
You can see this in action in the [`cw-plus` contracts]. Here is an example in | ||
the [`cw4-stake` contract]. | ||
</Callout> | ||
|
||
The rationale behind this is that currently using `#[entry_point]` exports | ||
special symbols from the binary, and these symbols **must** be unique. | ||
|
||
That means if you have two `instantiate` endpoints annotated with | ||
`#[entry_point]` _somewhere_ in your project (that includes all your | ||
dependencies!), your contract will fail to compile. | ||
|
||
Using this library feature will enable developers to use your contract as a | ||
dependency and reuse your code. | ||
|
||
[`cw-plus` contracts]: https://github.com/CosmWasm/cw-plus | ||
[`cw4-stake` contract]: | ||
https://github.com/CosmWasm/cw-plus/blob/48bec694521655d5b3e688c51e4185f740ea4640/contracts/cw4-stake/Cargo.toml#L22-L24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters