Skip to content

Commit fba9f32

Browse files
author
Bartłomiej Kuras
committed
Test verifying that all funds are properly visible on contract execution
1 parent d4b9c4a commit fba9f32

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

packages/multi-test/src/app.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ mod test {
269269
attr, coin, coins, AllBalanceResponse, BankMsg, BankQuery, Event, Reply, SubMsg, WasmMsg,
270270
};
271271

272-
use crate::test_helpers::contracts::{payout, reflect};
272+
use crate::test_helpers::contracts::{hackatom, payout, reflect};
273273
use crate::test_helpers::{CustomMsg, EmptyMsg};
274274
use crate::transactions::StorageTransaction;
275275
use crate::BankKeeper;
@@ -776,4 +776,45 @@ mod test {
776776
let committed = query_app(&app, &rcpt);
777777
assert_eq!(coins(37, "eth"), committed);
778778
}
779+
780+
#[test]
781+
fn sent_funds_properly_visible_on_execution() {
782+
// Testing if funds on contract are properly visible on contract.
783+
// Hackatom contract is initialized with 10btc. Then, the contract is executed, with
784+
// additional 20btc. Then beneficiary balance is checked - expeced value is 30btc. 10btc
785+
// would mean that sending tokens with message is not visible for this very message, and
786+
// 20btc means, that only such just send funds are visible.
787+
let mut app = mock_app();
788+
789+
let owner = Addr::unchecked("owner");
790+
let beneficiary = Addr::unchecked("beneficiary");
791+
app.init_bank_balance(&owner, coins(30, "btc")).unwrap();
792+
793+
let contract_id = app.store_code(hackatom::contract());
794+
let contract = app
795+
.instantiate_contract(
796+
contract_id,
797+
owner.clone(),
798+
&hackatom::InitMsg {
799+
beneficiary: beneficiary.as_str().to_owned(),
800+
},
801+
&coins(10, "btc"),
802+
"Hackatom",
803+
)
804+
.unwrap();
805+
806+
app.execute_contract(
807+
owner.clone(),
808+
contract.clone(),
809+
&EmptyMsg {},
810+
&coins(20, "btc"),
811+
)
812+
.unwrap();
813+
814+
// Check balance of all accounts to ensure no tokens where burned or created, and they are
815+
// in correct places
816+
assert_eq!(get_balance(&app, &owner), &[]);
817+
assert_eq!(get_balance(&app, &contract), &[]);
818+
assert_eq!(get_balance(&app, &beneficiary), coins(30, "btc"));
819+
}
779820
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Module for simple contracts to be used in tests
22
33
pub mod error;
4+
pub mod hackatom;
45
pub mod payout;
56
pub mod reflect;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Simplified contract which when executed releases the funds to beneficiary
2+
3+
use cosmwasm_std::{
4+
to_binary, BankMsg, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdError,
5+
};
6+
use cw_storage_plus::Item;
7+
use serde::{Deserialize, Serialize};
8+
9+
use crate::{test_helpers::EmptyMsg, Contract, ContractWrapper};
10+
11+
#[derive(Debug, Clone, Serialize, Deserialize)]
12+
pub struct InitMsg {
13+
pub beneficiary: String,
14+
}
15+
16+
const HACKATOM: Item<InitMsg> = Item::new("hackatom");
17+
18+
fn instantiate(
19+
deps: DepsMut,
20+
_env: Env,
21+
_info: MessageInfo,
22+
msg: InitMsg,
23+
) -> Result<Response, StdError> {
24+
HACKATOM.save(deps.storage, &msg)?;
25+
Ok(Response::default())
26+
}
27+
28+
fn execute(
29+
deps: DepsMut,
30+
env: Env,
31+
_info: MessageInfo,
32+
_msg: EmptyMsg,
33+
) -> Result<Response, StdError> {
34+
let init = HACKATOM.load(deps.storage)?;
35+
let balance = deps.querier.query_all_balances(env.contract.address)?;
36+
37+
let resp = Response::new().add_message(BankMsg::Send {
38+
to_address: init.beneficiary.clone(),
39+
amount: balance,
40+
});
41+
42+
Ok(resp)
43+
}
44+
45+
fn query(_deps: Deps, _env: Env, msg: EmptyMsg) -> Result<Binary, StdError> {
46+
to_binary(&msg)
47+
}
48+
49+
pub fn contract() -> Box<dyn Contract<Empty>> {
50+
let contract = ContractWrapper::new(execute, instantiate, query);
51+
Box::new(contract)
52+
}

0 commit comments

Comments
 (0)