Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec<Coin> for submessage funds #95

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions packages/utils/src/calls.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String>,
label: String,
code_id: u64,
code_hash: String,
funds_amount: Option<Uint128>,
funds: Vec<Coin>,
) -> StdResult<CosmosMsg> {
let mut msg = to_binary(self)?;
// can not have 0 block size
Expand All @@ -43,19 +43,13 @@ 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,
msg,
code_hash,
funds,
funds, // Using the passed funds directly
label,
};
Ok(init.into())
Expand All @@ -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_amount` - Vec of Coins to send with the handle message
fn to_cosmos_msg(
&self,
code_hash: String,
contract_addr: String,
funds_amount: Option<Uint128>,
funds: Vec<Coin>,
) -> StdResult<CosmosMsg> {
let mut msg = to_binary(self)?;
// can not have 0 block size
Expand All @@ -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,
Expand Down Expand Up @@ -156,6 +144,7 @@ mod tests {
use super::*;
use cosmwasm_std::{
to_vec, Binary, ContractResult, Empty, Querier, QuerierResult, SystemError, SystemResult,
Uint128,
};
use serde::Deserialize;

Expand Down Expand Up @@ -193,12 +182,10 @@ mod tests {
let address = "secret1xyzasdf".to_string();
let hash = "asdf".to_string();
let amount = Uint128::new(1234);
let coin = vec![Coin::new(1234, "uscrt")];

let cosmos_message: CosmosMsg = FooHandle::Var1 { f1: 1, f2: 2 }.to_cosmos_msg(
hash.clone(),
address.clone(),
Some(amount),
)?;
let cosmos_message: CosmosMsg =
FooHandle::Var1 { f1: 1, f2: 2 }.to_cosmos_msg(hash.clone(), address.clone(), coin)?;

match cosmos_message {
CosmosMsg::Wasm(WasmMsg::Execute {
Expand Down Expand Up @@ -227,13 +214,14 @@ mod tests {
let id = 17u64;
let hash = "asdf".to_string();
let amount = Uint128::new(1234);
let coin = vec![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),
coin,
)?;

match cosmos_message {
Expand Down
Loading