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

Multitest bank events #337

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/multi-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cw0 = { path = "../../packages/cw0", version = "0.7.0" }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.7.0" , features = ["iterator"]}
cosmwasm-std = { version = "0.15.0", features = ["staking"] }
cosmwasm-storage = { version = "0.15.0", features = ["iterator"] }
itertools = "0.10.1"
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
prost = "0.8.0"
38 changes: 26 additions & 12 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ where
mod test {
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{
attr, coin, coins, AllBalanceResponse, BankMsg, BankQuery, Reply, SubMsg, WasmMsg,
attr, coin, coins, AllBalanceResponse, BankMsg, BankQuery, Event, Reply, SubMsg, WasmMsg,
};

use crate::test_helpers::{
Expand Down Expand Up @@ -390,9 +390,14 @@ mod test {
let res = app
.execute_contract(random.clone(), contract_addr.clone(), &EmptyMsg {}, &[])
.unwrap();
assert_eq!(1, res.events.len());
assert_eq!(2, res.events.len());
let custom_attrs = res.custom_attrs(0);
assert_eq!(&[attr("action", "payout")], &custom_attrs);
let expected_transfer = Event::new("transfer")
.attr("recipient", "random")
.attr("sender", &contract_addr)
.attr("amount", "5eth");
assert_eq!(&expected_transfer, &res.events[1]);

// random got cash
let funds = get_balance(&app, &random);
Expand Down Expand Up @@ -450,7 +455,7 @@ mod test {
.unwrap();

// ensure the attributes were relayed from the sub-message
assert_eq!(2, res.events.len(), "{:?}", res.events);
assert_eq!(3, res.events.len(), "{:?}", res.events);
// first event was the call to reflect
let first = &res.events[0];
assert_eq!(first.ty.as_str(), "wasm");
Expand All @@ -468,7 +473,9 @@ mod test {
&second.attributes[0]
);
assert_eq!(&attr("action", "payout"), &second.attributes[1]);
// FIXME? reply didn't add any more events itself...
// third event is the transfer from bank
let third = &res.events[2];
assert_eq!(third.ty.as_str(), "transfer");

// ensure transfer was executed with reflect as sender
let funds = get_balance(&app, &reflect_addr);
Expand Down Expand Up @@ -520,10 +527,14 @@ mod test {
.execute_contract(random.clone(), reflect_addr.clone(), &msgs, &[])
.unwrap();
// only one wasm event with no custom attributes
assert_eq!(1, res.events.len());
assert_eq!(2, res.events.len());
assert_eq!(1, res.events[0].attributes.len());
assert_eq!("wasm", res.events[0].ty.as_str());
assert_eq!("contract_address", res.events[0].attributes[0].key.as_str());
// second event is the transfer from bank
let transfer = &res.events[1];
assert_eq!(transfer.ty.as_str(), "transfer");

// ensure random got paid
let funds = get_balance(&app, &random);
assert_eq!(funds, coins(7, "eth"));
Expand Down Expand Up @@ -641,16 +652,19 @@ mod test {
.execute_contract(random.clone(), reflect_addr.clone(), &msgs, &[])
.unwrap();
// we should get 2 events, the wasm one and the custom event
assert_eq!(2, res.events.len(), "{:?}", res.events);
assert_eq!(3, res.events.len(), "{:?}", res.events);
// the first one is just the standard wasm message with custom_address (no more attrs)
let attrs = res.custom_attrs(0);
assert_eq!(0, attrs.len());
// the second one is a custom event
let second = &res.events[1];
assert_eq!("wasm-custom", second.ty.as_str());
assert_eq!(2, second.attributes.len());
assert_eq!(&attr("from", "reply"), &second.attributes[0]);
assert_eq!(&attr("to", "test"), &second.attributes[1]);
// second event is the transfer from bank
let transfer = &res.events[1];
assert_eq!(transfer.ty.as_str(), "transfer");
// the third one is a custom event (from reply)
let custom = &res.events[2];
assert_eq!("wasm-custom", custom.ty.as_str());
assert_eq!(2, custom.attributes.len());
assert_eq!(&attr("from", "reply"), &custom.attributes[0]);
assert_eq!(&attr("to", "test"), &custom.attributes[1]);

// ensure success was written
let res: Reply = app.wrap().query_wasm_smart(&reflect_addr, &query).unwrap();
Expand Down
20 changes: 16 additions & 4 deletions packages/multi-test/src/bank.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use cosmwasm_std::{
coin, to_binary, Addr, AllBalanceResponse, BalanceResponse, BankMsg, BankQuery, Binary, Coin,
Storage,
Event, Storage,
};

use crate::executor::AppResponse;
use cosmwasm_storage::{prefixed, prefixed_read};
use cw0::NativeBalance;
use cw_storage_plus::Map;
use itertools::Itertools;

const BALANCES: Map<&Addr, NativeBalance> = Map::new("balances");

Expand Down Expand Up @@ -96,6 +97,13 @@ impl BankKeeper {
}
}

fn coins_to_string(coins: &[Coin]) -> String {
coins
.iter()
.map(|c| format!("{}{}", c.amount, c.denom))
.join(",")
}

impl Bank for BankKeeper {
fn execute(
&self,
Expand All @@ -106,18 +114,22 @@ impl Bank for BankKeeper {
let mut bank_storage = prefixed(storage, NAMESPACE_BANK);
match msg {
BankMsg::Send { to_address, amount } => {
// see https://github.com/cosmos/cosmos-sdk/blob/v0.42.7/x/bank/keeper/send.go#L142-L147
let events = vec![Event::new("transfer")
.attr("recipient", &to_address)
.attr("sender", &sender)
.attr("amount", coins_to_string(&amount))];
self.send(
&mut bank_storage,
sender,
Addr::unchecked(to_address),
amount,
)?;
// TODO: add some proper events here
Ok(AppResponse::default())
Ok(AppResponse { events, data: None })
}
BankMsg::Burn { amount } => {
// burn doesn't seem to emit any events
self.burn(&mut bank_storage, sender, amount)?;
// TODO: add some proper events here
Ok(AppResponse::default())
}
m => Err(format!("Unsupported bank message: {:?}", m)),
Expand Down