Skip to content

Commit

Permalink
Merge pull request #62 from CosmWasm/jawoznia/sylvia/entry_points
Browse files Browse the repository at this point in the history
Sylvia(macros): Describe entry points
  • Loading branch information
jawoznia authored Jun 28, 2024
2 parents 3eba4b0 + 7721c69 commit 92008c7
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/pages/sylvia/attributes/custom.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "attributes"]
---

import { Callout } from "nextra/components";

# `sv::custom` attribute
Expand All @@ -11,7 +15,7 @@ List of macros supporting the `sv::custom` attribute:

- [`contract`](https://cosmwasm-docs.vercel.app/sylvia/macros/contract)
- [`interface`](https://cosmwasm-docs.vercel.app/sylvia/macros/interface)
- [`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html)
- [`entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry_points)

## Usage

Expand Down Expand Up @@ -96,7 +100,9 @@ pub trait SomeInterface {

<Callout>
It's also possible to define custom types for the interface using associated
types. We cover it in the [`interface`] macro section.
types. We cover it in the
[`interface`](https://cosmwasm-docs.vercel.app/sylvia/macros/interface) macro
section.
</Callout>

[`interface`]: ../macros/interface#custom-types
6 changes: 5 additions & 1 deletion src/pages/sylvia/attributes/error.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "attributes"]
---

import { Callout } from "nextra/components";

# `sv::error` attribute
Expand All @@ -14,7 +18,7 @@ You can quickly provide this logic using, for example, [`thiserror`](https://doc
List of macros supporting the `sv::error` attribute:

- [`contract`](https://cosmwasm-docs.vercel.app/sylvia/macros/contract)
- [`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html)
- [`entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry_points)

<Callout>
The `interface` macro supports custom error types via the `Error` associated
Expand Down
6 changes: 5 additions & 1 deletion src/pages/sylvia/attributes/message.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "attributes"]
---

import { Callout } from "nextra/components";

# `sv::messages` attribute
Expand All @@ -15,7 +19,7 @@ interface.
List of macros supporting the `sv::messages` attribute:

- [`contract`](https://cosmwasm-docs.vercel.app/sylvia/macros/contract)
- [`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html)
- [`entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry_points)

## Usage

Expand Down
10 changes: 7 additions & 3 deletions src/pages/sylvia/attributes/msg.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "attributes"]
---

import { Callout } from "nextra/components";

# `sv::msg` attribute
Expand All @@ -10,11 +14,11 @@ List of macros supporting the `sv::msg` attribute:

- [`contract`](https://cosmwasm-docs.vercel.app/sylvia/macros/contract)
- [`interface`](https://cosmwasm-docs.vercel.app/sylvia/macros/interface)
- [`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html)
- [`entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry_points)

<Callout>
`interface` macro supports the `sv::msg` attribute only with `exec`, `query`
or `sudo` value.
[`interface`](https://cosmwasm-docs.vercel.app/sylvia/macros/interface) macro
supports the `sv::msg` attribute only with `exec`, `query` or `sudo` value.
</Callout>

## Usage
Expand Down
17 changes: 11 additions & 6 deletions src/pages/sylvia/attributes/override-entry-point.mdx
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
---
tags: ["sylvia", "attributes"]
---

import { Callout } from "nextra/components";

# `sv::override_entry_point` attribute

Use `sv::override_entry_point` if you want to define an entry point of your
contract yourself. This way, the `sv::entry_points` macro won't generate it for
you. The entry point will also be used as a dispatch method in the `MultiTest`
helpers generated by the `contract` macro.
contract yourself. This way, the
[`sv::entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry-points)
macro won't generate it for you. The entry point will also be used as a dispatch
method in the `MultiTest` helpers generated by the `contract` macro.

<Callout>
Use this attribute if you provide some custom logic in the entry points.
Otherwise, the `MultiTest` helpers won't cover that logic, and thus you will
miss the coverage.
Otherwise, the [`MultiTest`](https://cosmwasm-docs.vercel.app/cw-multi-test)
helpers won't cover that logic, and thus you will miss the coverage.
</Callout>

## Macros

List of macros supporting the `sv::override_entry_point` attribute:

- [`contract`](https://cosmwasm-docs.vercel.app/sylvia/macros/contract)
- [`entry_points`](https://docs.rs/sylvia/latest/sylvia/attr.entry_points.html)
- [`entry_points`](https://cosmwasm-docs.vercel.app/sylvia/macros/entry_points)

## Usage

Expand Down
4 changes: 4 additions & 0 deletions src/pages/sylvia/basics/contract-structure.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "basics"]
---

# Contract structure

Sylvia contracts are designed using the actor model. An actor is a contract
Expand Down
4 changes: 4 additions & 0 deletions src/pages/sylvia/basics/generate-contract.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "basics"]
---

# Generate contract

With the environment setup in the `getting-started` section, we can dive into
Expand Down
4 changes: 4 additions & 0 deletions src/pages/sylvia/basics/getting-started.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "basics"]
---

import { Callout } from "nextra/components";

# Prerequisites
Expand Down
3 changes: 2 additions & 1 deletion src/pages/sylvia/macros/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"interface": "Interface",
"contract": "Contract"
"contract": "Contract",
"entry-points": "Entry-points"
}
4 changes: 4 additions & 0 deletions src/pages/sylvia/macros/contract.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "macros"]
---

import { Callout } from "nextra/components";

# Contract
Expand Down
145 changes: 145 additions & 0 deletions src/pages/sylvia/macros/entry-points.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
tags: ["sylvia", "macros"]
---

import { Callout } from "nextra/components";

# Entry_points

Use the `entry_points` macro to generate entry points of your contract.

<Callout>
Use `entry_points` macro on top of `contract` macro. The `contract` macro erases all the Sylvia attributes, which may cause
the `entry_points` macro to fail. The `entry_points` macro also depends on the messages generated by the `contract` macro.

</Callout>

## Attributes

List of attributes supported by `entry_points` macro:

- [`custom`](https://cosmwasm-docs.vercel.app/sylvia/attributes/custom)
- [`error`](https://cosmwasm-docs.vercel.app/sylvia/attributes/error)
- [`msg`](https://cosmwasm-docs.vercel.app/sylvia/attributes/msg)
- [`override_entry_point`](https://cosmwasm-docs.vercel.app/sylvia/attributes/override-entry-point)

## Usage

```rust
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Response, StdResult};

use sylvia::contract;
use sylvia::types::{ExecCtx, InstantiateCtx};

#[cfg(not(feature = "library"))]
use sylvia::entry_points;

pub struct CounterContract;

#[cw_serde]
pub struct SomeResponse;

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
impl CounterContract {
pub const fn new() -> Self {
Self
}

#[sv::msg(instantiate)]
fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> {
Ok(Response::new())
}

#[sv::msg(exec)]
fn some_exec(&self, ctx: ExecCtx) -> StdResult<Response> {
Ok(Response::new())
}
}
```

The `entry_points` macro scans for
[`sv::msg`](https://cosmwasm-docs.vercel.app/sylvia/attributes/msg) attributes.
By default it generates: instantiate, exec, and query entry points.

## Custom types

You can construct your entry points to work with some specific custom types with
the [`sv::custom`](https://cosmwasm-docs.vercel.app/sylvia/attributes/custom).

## Generic types

CosmWasm entry points cannot be generic. We are thus forced to provide concrete
types to be used in place of generic types used in the contract.

```rust
use std::marker::PhantomData;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Response, StdResult};

use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::types::{CustomMsg, ExecCtx, InstantiateCtx};

#[cfg(not(feature = "library"))]
use sylvia::cw_std::Empty;
#[cfg(not(feature = "library"))]
use sylvia::entry_points;

pub struct CounterContract<ExecParamT, FieldT> {
field: Item<FieldT>,
_phantom: PhantomData<ExecParamT>,
}

#[cw_serde]
pub struct SomeResponse;

#[cfg_attr(not(feature = "library"), entry_points(generics<Empty, Empty>))]
#[contract]
impl<ExecParamT, FieldT> CounterContract<ExecParamT, FieldT>
where
ExecParamT: CustomMsg + 'static,
FieldT: 'static,
{
pub const fn new() -> Self {
Self {
field: Item::new("field"),
_phantom: PhantomData,
}
}

#[sv::msg(instantiate)]
fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> {
Ok(Response::new())
}

#[sv::msg(exec)]
fn some_exec(&self, ctx: ExecCtx, param: ExecParamT) -> StdResult<Response> {
Ok(Response::new())
}
}
```

We do that by adding parantheses after the `entry_points` macro and passing
`generics` attribute with concrete types passed in brackets.

<Callout>
Remember to pass the types in the order reflecting the order of generics
defined on the contract.
</Callout>

If the contract uses generic custom types we have to do some more work.

```rust
#[cfg_attr(not(feature = "library"), entry_points(generics<Empty, Empty>, custom(msg=SvCustomMsg, query=SvCustomQuery)))]
```

After coma we have to pass another parameter `custom`, and in the paranthesis
specify which types should be used in place of custom message and custom query.
The syntax reflects one used in the
[`custom`](https://cosmwasm-docs.vercel.app/sylvia/attributes/custom).

This is required as, for at least now, Sylvia is unable to determine which
concrete types are supposed to be used in place of generic custom types.
4 changes: 4 additions & 0 deletions src/pages/sylvia/macros/interface.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
tags: ["sylvia", "macros"]
---

import { Callout } from "nextra/components";

# Interface
Expand Down

0 comments on commit 92008c7

Please sign in to comment.