|
| 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