You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/utils/Readme.md
+19-18Lines changed: 19 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ elsewhere. There isn't an overarching theme for the items in this package.
13
13
14
14
## Calls module
15
15
16
-
This module contains traits used to call another contract. Do not forget to add the `use` statement for the traits you want.
16
+
This module contains traits used to call another contract. Do not forget to add the `use` statement for the traits you want.
17
17
18
18
```ignore
19
19
use secret_toolkit::utils::{InitCallback, HandleCallback};
@@ -23,7 +23,7 @@ Also, don't forget to add the toolkit dependency to your Cargo.toml
23
23
24
24
### Instantiating another contract
25
25
26
-
If you want to instantiate another contract, you should first copy/paste the InitMsg of that contract. For example, if you wanted to create an instance of the counter contract at <https://github.com/enigmampc/secret-template>
26
+
If you want to instantiate another contract, you should first copy/paste the InitMsg of that contract. For example, if you wanted to create an instance of the counter contract at <https://github.com/enigmampc/secret-template>
27
27
28
28
```rust
29
29
# usesecret_toolkit_utils::InitCallback;
@@ -40,7 +40,7 @@ impl InitCallback for CounterInitMsg {
40
40
}
41
41
```
42
42
43
-
You would copy/paste its InitMsg, and rename it so that it does not conflict with the InitMsg you have defined for your own contract. Then you would implement the `InitCallback` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your instantiation message padded to.
43
+
You would copy/paste its InitMsg, and rename it so that it does not conflict with the InitMsg you have defined for your own contract. Then you would implement the `InitCallback` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your instantiation message padded to.
44
44
45
45
```rust
46
46
# usesecret_toolkit_utils::InitCallback;
@@ -60,10 +60,11 @@ You would copy/paste its InitMsg, and rename it so that it does not conflict wit
Next, in the init or handle function that will instantiate the other contract, you will create an instance of the CounterInitMsg, call its `to_cosmos_msg`, and place the resulting CosmosMsg in the `messages` Vec of the InitResponse or HandleResponse that your function is returning. In this example, we are pretending that the code id of the counter contract is 123. Also, in this example, you are not sending any SCRT with the InitMsg, but if you needed to send 1 SCRT, you would replace the None in the `to_cosmos_msg` call with `Some(Uint128(1000000))`. The amount sent is in uscrt. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing.
78
+
Next, in the init or handle function that will instantiate the other contract, you will create an instance of the CounterInitMsg, call its `to_cosmos_msg`, and place the resulting CosmosMsg in the `messages` Vec of the InitResponse or HandleResponse that your function is returning. In this example, we are pretending that the code id of the counter contract is 123. Also, in this example, you are not sending any SCRT with the InitMsg, but if you needed to send 1 SCRT, you would replace the None in the `to_cosmos_msg` call with `Some(Uint128(1000000))`. The amount sent is in uscrt. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing.
78
79
79
80
### Calling a handle function of another contract
80
81
81
-
You should first copy/paste the specific HandleMsg(s) you want to call. For example, if you wanted to reset the counter you instantiated above
82
+
You should first copy/paste the specific HandleMsg(s) you want to call. For example, if you wanted to reset the counter you instantiated above
82
83
83
84
```rust
84
85
# usesecret_toolkit_utils::HandleCallback;
@@ -95,7 +96,7 @@ impl HandleCallback for CounterHandleMsg {
95
96
}
96
97
```
97
98
98
-
You would copy/paste the Reset variant of its HandleMsg enum, and rename the enum so that it does not conflict with the HandleMsg enum you have defined for your own contract. Then you would implement the `HandleCallback` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your Reset message padded to. If you need to call multiple different Handle messages, even if they are to different contracts, you can include all the Handle messages as variants in the same enum (you can not have two variants with the same name within the same enum, though).
99
+
You would copy/paste the Reset variant of its HandleMsg enum, and rename the enum so that it does not conflict with the HandleMsg enum you have defined for your own contract. Then you would implement the `HandleCallback` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your Reset message padded to. If you need to call multiple different Handle messages, even if they are to different contracts, you can include all the Handle messages as variants in the same enum (you can not have two variants with the same name within the same enum, though).
99
100
100
101
```rust
101
102
# usesecret_toolkit_utils::HandleCallback;
@@ -107,7 +108,7 @@ You would copy/paste the Reset variant of its HandleMsg enum, and rename the enu
Next, in the init or handle function that will call the other contract, you will create an instance of the CounterHandleMsg::Reset variant, call its `to_cosmos_msg`, and place the resulting CosmosMsg in the `messages` Vec of the InitResponse or HandleResponse that your function is returning. In this example, you are not sending any SCRT with the Reset message, but if you needed to send 1 SCRT, you would replace the None in the `to_cosmos_msg` call with `Some(Uint128(1000000))`. The amount sent is in uscrt. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing.
132
+
Next, in the init or handle function that will call the other contract, you will create an instance of the CounterHandleMsg::Reset variant, call its `to_cosmos_msg`, and place the resulting CosmosMsg in the `messages` Vec of the InitResponse or HandleResponse that your function is returning. In this example, you are not sending any SCRT with the Reset message, but if you needed to send 1 SCRT, you would replace the None in the `to_cosmos_msg` call with `Some(Uint128(1000000))`. The amount sent is in uscrt. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing.
132
133
133
134
### Querying another contract
134
135
135
-
You should first copy/paste the specific QueryMsg(s) you want to call. For example, if you wanted to get the count of the counter you instantiated above
136
+
You should first copy/paste the specific QueryMsg(s) you want to call. For example, if you wanted to get the count of the counter you instantiated above
136
137
137
138
```rust
138
139
# usesecret_toolkit_utils::Query;
@@ -150,7 +151,7 @@ impl Query for CounterQueryMsg {
150
151
}
151
152
```
152
153
153
-
You would copy/paste the GetCount variant of its QueryMsg enum, and rename the enum so that it does not conflict with the QueryMsg enum you have defined for your own contract. Then you would implement the `Query` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your query message padded to. If you need to perform multiple different queries, even if they are to different contracts, you can include all the Query messages as variants in the same enum (you can not have two variants with the same name within the same enum, though).
154
+
You would copy/paste the GetCount variant of its QueryMsg enum, and rename the enum so that it does not conflict with the QueryMsg enum you have defined for your own contract. Then you would implement the `Query` trait as above, setting the BLOCK_SIZE constant to the size of the blocks you want your query message padded to. If you need to perform multiple different queries, even if they are to different contracts, you can include all the Query messages as variants in the same enum (you can not have two variants with the same name within the same enum, though).
154
155
155
156
```rust
156
157
# usesecret_toolkit_utils::Query;
@@ -163,9 +164,9 @@ pub struct CountResponse {
163
164
}
164
165
```
165
166
166
-
Next, you will copy/paste the response of the query. If the other contract defines its response to the query with a struct, you are good to go.
167
+
Next, you will copy/paste the response of the query. If the other contract defines its response to the query with a struct, you are good to go.
167
168
168
-
If, however, the other contract returns an enum variant, one approach is to copy the fields of the variant and place them in a struct. Because an enum variant gets serialized with the name of the variant, you will then also want to create a wrapper struct whose only field has the name of the variant, and whose type is the struct you defined with the variant's fields. For example, if you wanted to do a token_info query of the [SNIP20 reference implementation](https://github.com/enigmampc/snip20-reference-impl), I would recommend using the SNIP20 toolkit function, but just for the sake of example, let's say you forgot that toolkit existed.
169
+
If, however, the other contract returns an enum variant, one approach is to copy the fields of the variant and place them in a struct. Because an enum variant gets serialized with the name of the variant, you will then also want to create a wrapper struct whose only field has the name of the variant, and whose type is the struct you defined with the variant's fields. For example, if you wanted to do a token_info query of the [SNIP20 reference implementation](https://github.com/enigmampc/snip20-reference-impl), I would recommend using the SNIP20 toolkit function, but just for the sake of example, let's say you forgot that toolkit existed.
You would copy the QueryAnswer::TokenInfo enum variant and create a TokenInfo struct with those fields. You should make all those fields public if you need to access them. Then you would create the TokenInfoResponse wrapper struct, which has only one field whose name is the name of the QueryAnswer variant in snake case (token_info). As a reminder, you only need to do this to properly deserialize the response if it was defined as an enum in the other contract.
191
+
You would copy the QueryAnswer::TokenInfo enum variant and create a TokenInfo struct with those fields. You should make all those fields public if you need to access them. Then you would create the TokenInfoResponse wrapper struct, which has only one field whose name is the name of the QueryAnswer variant in snake case (token_info). As a reminder, you only need to do this to properly deserialize the response if it was defined as an enum in the other contract.
191
192
192
193
Now to perform the query
193
194
@@ -202,7 +203,7 @@ Now to perform the query
202
203
# pubenumCounterQueryMsg {
203
204
# GetCount {},
204
205
# }
205
-
#
206
+
#
206
207
# implQueryforCounterQueryMsg {
207
208
# constBLOCK_SIZE:usize=256;
208
209
# }
@@ -228,7 +229,7 @@ let count_response: StdResult<CountResponse> = get_count.query(
228
229
# Ok::<(), StdError>(())
229
230
```
230
231
231
-
You create an instance of the CounterQueryMsg::GetCount variant, and call its `query` function, returning its value to a variable of the response type. If you were doing a token_info query, you would write `let token_info_resp: TokenInfoResponse = ...`. You MUST use explicit type annotation here.
232
+
You create an instance of the CounterQueryMsg::GetCount variant, and call its `query` function, returning its value to a variable of the response type. If you were doing a token_info query, you would write `let token_info_resp: TokenInfoResponse = ...`. You MUST use explicit type annotation here.
232
233
233
234
## Feature Toggle
234
235
@@ -274,7 +275,7 @@ pub fn instantiate(
274
275
],
275
276
vec![info.sender], // Can put more than one pauser
0 commit comments