Skip to content

Commit 1b0d56c

Browse files
committed
Sylvia: Describe multitest types
1 parent c4d0176 commit 1b0d56c

File tree

3 files changed

+139
-14
lines changed

3 files changed

+139
-14
lines changed

src/pages/sylvia/macros/generated-types/multitest.mdx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ where
123123

124124
The `CodeId` structure imitates the id of a contracts binary uploaded to the
125125
actual blockchain. It links the code id with the
126-
[`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html) on which
127-
the contract is stored. Call the `instantiate` method and pass to it parameters
128-
you would pass to the `InstantiateMsg` to get the `InstantiateProxy` instance.
126+
[`App`](../../types/multitest#app) on which the contract is stored. Call the
127+
`instantiate` method and pass to it parameters you would pass to the
128+
`InstantiateMsg` to get the `InstantiateProxy` instance.
129129

130130
### InstantiateProxy
131131

@@ -212,17 +212,13 @@ call.
212212
</Callout>
213213

214214
The `call` method instantiates the contract on the
215-
[`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html) and
216-
return the
217-
[`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html) to
218-
communicate with it.
215+
[`App`](../../types/multitest#app) and return the
216+
[`Proxy`](../../types/multitest#proxy) to communicate with it.
219217

220218
### Proxy trait
221219

222-
By itself the
223-
[`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html)
224-
stores the address of the contract as well as the reference to the
225-
[`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html).
220+
By itself the [`Proxy`](../../types/multitest#proxy) stores the address of the
221+
contract as well as the reference to the [`App`](../../types/multitest#app).
226222

227223
<Tabs items={['contract', 'interface']} defaultIndex={0}>
228224
<Tabs.Tab>
@@ -375,7 +371,7 @@ stores the address of the contract as well as the reference to the
375371

376372
The proxy trait declares methods marked with the [`sv::msg`](../../attributes/msg) attribute, and found in the contract impl block
377373

378-
The trait is then implemented on the [`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html) generic over the contract type. Implementation of the methods instantiates appropriate messages and sends them to the [`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html)
374+
The trait is then implemented on the [`Proxy`](../../types/multitest#proxy) generic over the contract type. Implementation of the methods instantiates appropriate messages and sends them to the [`App`](../../types/multitest#app)
379375
so that we don't have to do it manually.
380376

381377
</Tabs.Tab>
@@ -512,7 +508,7 @@ stores the address of the contract as well as the reference to the
512508
```
513509

514510
Code generated by the [`interface`](../interface) macro works the same way as in the case of the [`contract`](../contract) macro.
515-
The only difference, is that we do the blanket implementation of the trait for every [`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html) generic over contract implementing our trait.
511+
The only difference, is that we do the blanket implementation of the trait for every [`Proxy`](../../types/multitest#proxy) generic over contract implementing our trait.
516512

517513
</Tabs.Tab>
518514
</Tabs>

src/pages/sylvia/types/_meta.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"context": "Context",
3-
"communication": "Communication"
3+
"communication": "Communication",
4+
"multitest": "Multitest"
45
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
tags: ["sylvia", "types"]
3+
---
4+
5+
import { Callout } from "nextra/components";
6+
7+
# Multitest
8+
9+
Sylvia exposes several types that build on top of the
10+
[`Multitest`](../../cw-multi-test). These types are supposed to be used next to
11+
[`the types`](../macros/generated-types/multitest) generated by
12+
[`contract`](../macros/contract) and [`interface`](../macros/interface) macros.
13+
14+
<Callout>
15+
The types are hidden behind the `mt` feature flag. We recommend enabling it in
16+
the dev-dependencies.
17+
</Callout>
18+
19+
Example usage of utilities described on this page as well as ones defined on the
20+
[`generated types`](../macros/generated-types/multitest) page:
21+
22+
```rust
23+
use sylvia::cw_multi_test::IntoAddr;
24+
use sylvia::multitest::App;
25+
26+
use super::interface::sv::mt::InterfaceProxy;
27+
use super::sv::mt::{CodeId, CounterContractProxy};
28+
29+
#[test]
30+
fn example_test() {
31+
// Define the [cosmwasm_std::Addr] of the message sender.
32+
let owner = "owner".into_addr();
33+
let dummy = "dummy".into_addr();
34+
35+
// Initialize the [sylvia::multitest::App].
36+
let app = App::default();
37+
38+
// Initialize the generated [contract::sv::mt::CodeId] by storing the contract code on the App.
39+
let code_id = CodeId::store_code(&app);
40+
41+
// Initialize the contract by calling the [contract::sv::mt::CodeId::instantiate] method.
42+
let contract = code_id
43+
.instantiate(true)
44+
.with_label("MyContract")
45+
.call(&owner)
46+
.unwrap();
47+
48+
// Send messages through the [sylvia::multitest::Proxy] as you would call the methods on
49+
// the contract type.
50+
contract.some_exec(dummy.to_string()).call(&owner).unwrap();
51+
contract.some_query(dummy.to_string()).unwrap();
52+
53+
// Send interface messages the same way.
54+
contract
55+
.interface_exec(dummy.to_string())
56+
.call(&owner)
57+
.unwrap();
58+
contract.interface_query(dummy.to_string()).unwrap();
59+
}
60+
```
61+
62+
## App
63+
64+
The [`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html) is a
65+
wrapper around the
66+
[`cw-multi-test::App`](https://docs.rs/cw-multi-test/latest/cw_multi_test/struct.App.html),
67+
stored inside the `RefCell`. It is done this way because the
68+
[`App`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.App.html) has to
69+
be passed by reference to multiple types, and we have to use the interior
70+
mutability to affect it's state.
71+
72+
## Proxy
73+
74+
The [`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html)
75+
represents a single, initialized contract. It is generic over it, and the `App`
76+
on which it's installed. The `Proxy` is the structure through which you act on
77+
behalf of the contract, sending messages and querying the state.
78+
79+
<Callout>
80+
The
81+
[`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html)
82+
should not be constructed manually. Instead, create the contracts
83+
[`CodeId`](../macros/generated-types/multitest#codeid) instance and call
84+
`CodeId::instantiate`, which returns the
85+
[`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html).
86+
</Callout>
87+
88+
The [`Proxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.Proxy.html)
89+
by itself doesn't have the methods defined in your contract. They are instead
90+
implemented via auto-generated traits by [`contract`](../macros/contract) and
91+
[`interface`](../macros/interface) macros. Import the generated proxy trait into
92+
your test scope to use these methods. If you want to learn more about the proxy
93+
traits, you can go to
94+
[`this section`](../macros/generated-types/multitest#proxy-trait).
95+
96+
## ExecProxy
97+
98+
The
99+
[`ExecProxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.ExecProxy.html)
100+
is an intermediate type, for setting additional data for the execute messages.
101+
It's main functionality is the
102+
[`call`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.ExecProxy.html#method.call)
103+
method, requiring the user to specify the caller of the message.
104+
105+
<Callout>
106+
The
107+
[`ExecProxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.ExecProxy.html)
108+
should not be constructed manually. Instead, it's supposed to be returned by
109+
execute methods exposed in the [`proxy
110+
traits`](../macros/generated-types/multitest#proxy-trait).
111+
</Callout>
112+
113+
## MigrateProxy
114+
115+
The
116+
[`MigrateProxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.MigrateProxy.html)
117+
is an intermediate type for setting additional data for the migrate messages.
118+
It's main functionality is the
119+
[`call`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.MigrateProxy.html#method.call)
120+
method, requiring the user to specify the caller of the message.
121+
122+
<Callout>
123+
The
124+
[`MigrateProxy`](https://docs.rs/sylvia/latest/sylvia/multitest/struct.MigrateProxy.html)
125+
should not be constructed manually. Instead, it's supposed to be returned by
126+
migrate methods exposed in the [`proxy
127+
traits`](../macros/generated-types/multitest#proxy-trait).
128+
</Callout>

0 commit comments

Comments
 (0)