⚠️ This package is a sub-package of the secret-toolkit
package. Please see its crate page for more context.
This package contains various uncategorized tools. It should be thought of as the shed in your back yard where you put the stuff that doesn't belong elsewhere. There isn't an overarching theme for the items in this package.
This module contains traits used to call another contract. Do not forget to add the use
statement for the traits you want.
rust
use secret_toolkit::utils::{InitCallback, HandleCallback, Query};
Also, don't forget to add the toolkit dependency to your Cargo.toml
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 ```rust
pub struct CounterInitMsg { pub count: i32, }
impl InitCallback for CounterInitMsg {
const BLOCKSIZE: usize = 256;
}
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.
rust
let counterinit_msg = CounterInitMsg {
count: 100
};
let cosmosmsg = counterinitmsg.tocosmosmsg( "newcontractlabel".tostring(), 123, "CODEHASHOFCONTRACTYOUWANTTOINSTANTIATE".tostring(), None, )?;
Ok(HandleResponse {
messages: vec![cosmosmsg],
log: vec![],
data: None,
})
``
Next, in the init or handle function that will instantiate the other contract, you will create an instance of the CounterInitMsg, call its
tocosmosmsg, and place the resulting CosmosMsg in the
messagesVec 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
tocosmos_msgcall 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.
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 ```rust
pub enum CounterHandleMsg { Reset { count: i32 }, }
impl HandleCallback for CounterHandleMsg {
const BLOCKSIZE: usize = 256;
}
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).
rust
let resetmsg = CounterHandleMsg::Reset {
count: 200,
};
let cosmosmsg = resetmsg.tocosmosmsg( "CODEHASHOFCONTRACTYOUWANTTOEXECUTE".tostring(), HumanAddr("ADDRESSOFCONTRACTYOUARECALLING".tostring()), None, )?;
Ok(HandleResponse {
messages: vec![cosmosmsg],
log: vec![],
data: None,
})
``
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
tocosmosmsg, and place the resulting CosmosMsg in the
messagesVec 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
tocosmos_msgcall 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.
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 ```rust
pub enum CounterQueryMsg { GetCount {}, }
impl Query for CounterQueryMsg {
const BLOCK_SIZE: usize = 256;
}
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).
rust
pub struct CountResponse { pub count: i32, } ``` 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.
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, I would recommend using the SNIP20 toolkit function, but just for the sake of example, let's say you forgot that toolkit existed. ```rust
pub struct TokenInfo {
pub name: String,
pub symbol: String,
pub decimals: u8,
pub total_supply: Option
pub struct TokenInfoResponse { pub tokeninfo: TokenInfo, } ``` 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 (tokeninfo). 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.
Now to perform the query
rust
let get_count = CounterQueryMsg::GetCount {};
let count_response: CountResponse = get_count.query(
&deps.querier,
"CODE_HASH_OF_CONTRACT_YOU_WANT_TO_QUERY".to_string(),
HumanAddr("ADDRESS_OF_CONTRACT_YOU_ARE_QUERYING".to_string()),
)?;
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.