Skip to content

Commit

Permalink
Test cw20 happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jun 26, 2020
1 parent 10b186d commit cdb7e16
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion contracts/cw20-escrow/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ fn query_list<S: Storage, A: Api, Q: Querier>(deps: &Extern<S, A, Q>) -> StdResu
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coins, CosmosMsg, StdError};
use cosmwasm_std::{coins, CosmosMsg, StdError, Uint128};

const CANONICAL_LENGTH: usize = 20;

Expand Down Expand Up @@ -386,6 +386,64 @@ mod tests {
e => panic!("Expected NotFound, got {}", e),
}
}

#[test]
fn happy_path_cw20() {
let mut deps = mock_dependencies(CANONICAL_LENGTH, &[]);

// init an empty contract
let init_msg = InitMsg {};
let env = mock_env(&deps.api, &HumanAddr::from("anyone"), &[]);
let res = init(&mut deps, env, init_msg).unwrap();
assert_eq!(0, res.messages.len());

// create an escrow
let create = CreateMsg {
id: "foobar".to_string(),
arbiter: HumanAddr::from("arbitrate"),
recipient: HumanAddr::from("recd"),
end_time: None,
end_height: None,
};
let receive = Cw20ReceiveMsg {
sender: HumanAddr::from("source"),
amount: Uint128(100),
msg: Some(to_binary(&HandleMsg::Create(create.clone())).unwrap()),
};
let token_contract = HumanAddr::from("my-cw20-token");
let env = mock_env(&deps.api, &token_contract, &[]);
let res = handle(&mut deps, env, HandleMsg::Receive(receive.clone())).unwrap();
assert_eq!(0, res.messages.len());
assert_eq!(log("action", "create"), res.log[0]);

// approve it
let id = create.id.clone();
let env = mock_env(&deps.api, &create.arbiter, &[]);
let res = handle(&mut deps, env, HandleMsg::Approve { id }).unwrap();
assert_eq!(1, res.messages.len());
assert_eq!(log("action", "approve"), res.log[0]);
let send_msg = Cw20HandleMsg::Transfer {
recipient: create.recipient,
amount: receive.amount,
};
assert_eq!(
res.messages[0],
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_contract,
msg: to_binary(&send_msg).unwrap(),
send: vec![]
})
);

// second attempt fails (not found)
let id = create.id.clone();
let env = mock_env(&deps.api, &create.arbiter, &[]);
let res = handle(&mut deps, env, HandleMsg::Approve { id });
match res.unwrap_err() {
StdError::NotFound { .. } => {}
e => panic!("Expected NotFound, got {}", e),
}
}
}

//
Expand Down

0 comments on commit cdb7e16

Please sign in to comment.