diff --git a/packages/utils/Readme.md b/packages/utils/Readme.md index 22ebe11..cee44b5 100644 --- a/packages/utils/Readme.md +++ b/packages/utils/Readme.md @@ -44,7 +44,7 @@ You would copy/paste its InitMsg, and rename it so that it does not conflict wit ```rust # use secret_toolkit_utils::InitCallback; -# use cosmwasm_std::{StdResult, StdError, Response}; +# use cosmwasm_std::{StdResult, StdError, Response, Coin}; # use serde::{Serialize, Deserialize}; # use schemars::JsonSchema; # @@ -63,19 +63,21 @@ let counter_init_msg = CounterInitMsg { count: 100 }; +let funds = vec![Coin::new(1234, "uscrt")]; + let cosmos_msg = counter_init_msg.to_cosmos_msg( None, "new_contract_label".to_string(), 123, "CODE_HASH_OF_CONTRACT_YOU_WANT_TO_INSTANTIATE".to_string(), - None, + funds, )?; response = Ok(Response::new().add_message(cosmos_msg)); # Ok::<(), StdError>(()) ``` -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. +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 sending 1234 SCRT with the InitMsg as seen in the `funds` parameter. The amount sent is in uscrt, but it can be sent in any denom of your choice. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing. ### Calling a handle function of another contract @@ -100,7 +102,7 @@ You would copy/paste the Reset variant of its HandleMsg enum, and rename the enu ```rust # use secret_toolkit_utils::HandleCallback; -# use cosmwasm_std::{StdResult, StdError, Response}; +# use cosmwasm_std::{StdResult, StdError, Response, Coin}; # use serde::{Serialize, Deserialize}; # use schemars::JsonSchema; # @@ -119,17 +121,19 @@ let reset_msg = CounterHandleMsg::Reset { count: 200, }; +let funds = vec![Coin::new(1234, "uscrt")]; + let cosmos_msg = reset_msg.to_cosmos_msg( "CODE_HASH_OF_CONTRACT_YOU_WANT_TO_EXECUTE".to_string(), "ADDRESS_OF_CONTRACT_YOU_ARE_CALLING".to_string(), - None, + funds, )?; response = Ok(Response::new().add_message(cosmos_msg)); # Ok::<(), StdError>(()) ``` -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. +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 sending 1234 SCRT with the Reset message, as seen in the `funds` parameter. The amount sent is in uscrt, but it can be sent in any denom of your choice. Any CosmosMsg placed in the `messages` Vec will be executed after your contract has finished its own processing. ### Querying another contract diff --git a/packages/utils/src/calls.rs b/packages/utils/src/calls.rs index fa21835..55b8e16 100755 --- a/packages/utils/src/calls.rs +++ b/packages/utils/src/calls.rs @@ -1,8 +1,8 @@ use serde::{de::DeserializeOwned, Serialize}; use cosmwasm_std::{ - to_binary, Coin, CosmosMsg, CustomQuery, QuerierWrapper, QueryRequest, StdResult, Uint128, - WasmMsg, WasmQuery, + to_binary, Coin, CosmosMsg, CustomQuery, QuerierWrapper, QueryRequest, StdResult, WasmMsg, + WasmQuery, }; use super::space_pad; @@ -26,14 +26,14 @@ pub trait InitCallback: Serialize { /// * `label` - String holding the label for the new contract instance /// * `code_id` - code ID of the contract to be instantiated /// * `code_hash` - String holding the code hash of the contract to be instantiated - /// * `funds_amount` - Optional Uint128 amount of native coin to send with instantiation message + /// * `funds` - Vec of Coins to send with instantiation message fn to_cosmos_msg( &self, admin: Option, label: String, code_id: u64, code_hash: String, - funds_amount: Option, + funds: Vec, ) -> StdResult { let mut msg = to_binary(self)?; // can not have 0 block size @@ -43,13 +43,7 @@ pub trait InitCallback: Serialize { Self::BLOCK_SIZE }; space_pad(&mut msg.0, padding); - let mut funds = Vec::new(); - if let Some(amount) = funds_amount { - funds.push(Coin { - amount, - denom: String::from("uscrt"), - }); - } + let init = WasmMsg::Instantiate { admin, code_id, @@ -80,12 +74,12 @@ pub trait HandleCallback: Serialize { /// /// * `code_hash` - String holding the code hash of the contract to be executed /// * `contract_addr` - address of the contract being called - /// * `funds_amount` - Optional Uint128 amount of native coin to send with the handle message + /// * `funds` - Vec of Coins to send with the handle message fn to_cosmos_msg( &self, code_hash: String, contract_addr: String, - funds_amount: Option, + funds: Vec, ) -> StdResult { let mut msg = to_binary(self)?; // can not have 0 block size @@ -95,13 +89,7 @@ pub trait HandleCallback: Serialize { Self::BLOCK_SIZE }; space_pad(&mut msg.0, padding); - let mut funds = Vec::new(); - if let Some(amount) = funds_amount { - funds.push(Coin { - amount, - denom: String::from("uscrt"), - }); - } + let execute = WasmMsg::Execute { msg, contract_addr, @@ -192,12 +180,11 @@ mod tests { fn test_handle_callback_implementation_works() -> StdResult<()> { let address = "secret1xyzasdf".to_string(); let hash = "asdf".to_string(); - let amount = Uint128::new(1234); - + let amount = Coin::new(1234, "uscrt"); let cosmos_message: CosmosMsg = FooHandle::Var1 { f1: 1, f2: 2 }.to_cosmos_msg( hash.clone(), address.clone(), - Some(amount), + vec![amount], )?; match cosmos_message { @@ -212,7 +199,7 @@ mod tests { let mut expected_msg = r#"{"Var1":{"f1":1,"f2":2}}"#.as_bytes().to_vec(); space_pad(&mut expected_msg, 256); assert_eq!(msg.0, expected_msg); - assert_eq!(funds, vec![Coin::new(amount.u128(), "uscrt")]) + assert_eq!(funds, vec![Coin::new(1234, "uscrt")]) } other => panic!("unexpected CosmosMsg variant: {:?}", other), }; @@ -226,14 +213,14 @@ mod tests { let lbl = "testlabel".to_string(); let id = 17u64; let hash = "asdf".to_string(); - let amount = Uint128::new(1234); + let amount = Coin::new(1234, "uscrt"); let cosmos_message: CosmosMsg = FooInit { f1: 1, f2: 2 }.to_cosmos_msg( Some(adm.clone()), lbl.clone(), id, hash.clone(), - Some(amount), + vec![amount], )?; match cosmos_message { @@ -251,7 +238,7 @@ mod tests { space_pad(&mut expected_msg, 256); assert_eq!(msg.0, expected_msg); assert_eq!(code_hash, hash); - assert_eq!(funds, vec![Coin::new(amount.u128(), "uscrt")]); + assert_eq!(funds, vec![Coin::new(1234, "uscrt")]); assert_eq!(label, lbl) } other => panic!("unexpected CosmosMsg variant: {:?}", other),