Skip to content

Commit 9afe5e1

Browse files
committed
Sylvia: Interoperability between CosmWasm and Sylvia
1 parent ac5d188 commit 9afe5e1

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/pages/sylvia/basics/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"getting-started": "Getting started",
33
"generate-contract": "Generate contract",
4-
"contract-structure": "Contract structure"
4+
"contract-structure": "Contract structure",
5+
"interoperability": "Interoperability"
56
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
tags: ["sylvia", "basics"]
3+
---
4+
5+
import { Callout, Tabs } from "nextra/components";
6+
7+
# Interoperability
8+
9+
<Callout>
10+
Sylvia contracts are fully interoperable with legacy CosmWasm contracts.
11+
</Callout>
12+
13+
Sylvia macros expand into a regular CosmWasm code. Because of that, we can test
14+
and communicate with Sylvia contracts like we would with any CosmWasm contract.
15+
16+
Sylvia exposes, however additional QoL utilities like
17+
[`Remote`](../types/communication#remote) and [`MultiTest`](../types/multitest)
18+
helpers, which we recommend using alongside the Sylvia contracts.
19+
20+
## Communication
21+
22+
We can send messages from Sylvia as we would from any CosmWasm contract.
23+
24+
Execute messages in Sylvia return the
25+
[`Response`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Response.html)
26+
on which we can call the
27+
[`add_message`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Response.html#method.add_message).
28+
29+
```rust
30+
#[sv::msg(exec)]
31+
fn external_increment(&self, ctx: ExecCtx) -> StdResult<Response> {
32+
let remote = self.remote.load(ctx.deps.storage)?;
33+
let msg = WasmMsg::Execute {
34+
contract_addr: remote.to_string(),
35+
msg: to_json_binary(&ExternalExecMsg::Increment {})?,
36+
funds: vec![],
37+
};
38+
Ok(Response::new().add_message(msg))
39+
}
40+
```
41+
42+
Query messages can also be sent through the
43+
[`query_wasm_smart`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.QuerierWrapper.html#method.query_wasm_smart)
44+
method. We can access the
45+
[`Deps`](https://docs.rs/cosmwasm-std/latest/cosmwasm_std/struct.Deps.html)
46+
through the [`QueryCtx`](../types/context).
47+
48+
```rust
49+
#[sv::msg(query)]
50+
fn external_count(&self, ctx: QueryCtx) -> StdResult<ExternalResponse> {
51+
let remote = self.remote.load(ctx.deps.storage)?;
52+
53+
ctx.deps
54+
.querier
55+
.query_wasm_smart(remote, &ExternalQueryMsg::Count {})
56+
}
57+
```
58+
59+
As you see, we can send messages from the Sylvia contract as we would in case of
60+
a CosmWasm contract.
61+
62+
Although we could send messages to Sylvia contract in the same way, we recommend
63+
using the [`ExecutorBuilder`](../types/communication#executorbuilder) and
64+
[`BoundQuerier`](../types/communication#boundquerier) which are more user
65+
friendly.
66+
67+
## Testing
68+
69+
Although we could use the
70+
[`cw_multi_test::App`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html),
71+
we would lose the ability to use the MultiTest helpers generated for Sylvia
72+
contarcts. For this reason, we always recommend to use the Sylvia
73+
[`App`](../types/multitest#app).
74+
75+
```rust
76+
use sylvia::multitest::App;
77+
78+
let app = App::<BasicMtApp<Empty, Empty>>::default();
79+
```
80+
81+
<Callout>
82+
We must provide the full type for the [`App`](../types/multitest#app), as Rust
83+
cannot deduce it here.
84+
</Callout>
85+
86+
We can access the underlying
87+
[`cw_multi_test::App`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html)
88+
via
89+
[`app_mut`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html#method.app_mut)
90+
to
91+
[`store_code`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html#method.store_code)
92+
of the CosmWasm contract.
93+
94+
```rust
95+
fn cosmwasm_contract() -> Box<dyn Contract<Empty>> { ... }
96+
97+
let cosmwasm_code = app.app_mut().store_code(cosmwasm_contract());
98+
```
99+
100+
To instantiate the CosmWasm contract, we will also use the
101+
[`app_mut`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html#method.app_mut).
102+
103+
```rust
104+
let cosmwasm_contract = app
105+
.app_mut()
106+
.instantiate_contract(
107+
cosmwasm_code,
108+
owner.clone(),
109+
&InstantiateMsg {},
110+
&[],
111+
"cosmwasm_contract",
112+
None,
113+
)
114+
.unwrap();
115+
```
116+
117+
After that testing will be the same as with any CosmWasm and Sylvia contract.
118+
Check documentation about testing Sylvia's contract [`here`](../types/multitest)
119+
and about testing CosmWasm contracts [`here`](../../cw-multi-test).

0 commit comments

Comments
 (0)