Skip to content

Commit

Permalink
Sylvia: Add good practices section to contract macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Jul 23, 2024
1 parent 7f51650 commit eeff068
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/pages/sylvia/macros/contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,34 @@ to mention are:
- If generic types are used as part of the method signature, generated messages
will require them to fulfill their trait bounds. In most cases it's enough to
add the `sylvia::types::CustomMsg + \'static` bounds.

## Good practices

### Prefer generic custom types

Although the [`sv::custom`](../attributes/custom) attribute is not mandatory, as
by default [`contract`](../contract) macro will use
[`Empty`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Empty.html)
type in place of custom types; we recommend that if your contract is meant to be
chain agnostic, define it with a generic custom message and custom query.

Adding custom generic types will help other developers to use your contract in
their tests. The
[`cw_multi_test::App`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html)
is generic over the custom types, and although in a real blockchain environment
we would be able to use the contract with custom types not set next to the
contract with custom types set, Rust type safety does not allow that.

```rust
pub struct Contract<CMsgT, CQueryT> {
...
}

#[contract]
#[sv::custom(msg=CMsgT, query=CQueryT)]
impl<CMsgT, CQueryT> Contract<CMsgT, CQueryT>
where
CMsgT: 'static + CustomMsg,
CQueryT: 'static + CustomQuery,
{}
```
8 changes: 4 additions & 4 deletions src/pages/sylvia/macros/interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ pub trait Interface {

## Generic types

`interface` macro does not support generics. Instead you can provide generic
functionality using the associated types. It's because Sylvia interfaces can be
implemented only a once per contract, and in such a case associated types are
semantically correct in Rust.
The `interface` macro does not support generics. Instead, you can provide
generic functionality using the associated types. It's because Sylvia interfaces
can be implemented only once per contract, and in such a case, associated types
are semantically correct in Rust.

```rust
#[interface]
Expand Down

0 comments on commit eeff068

Please sign in to comment.