Skip to content

Commit

Permalink
Add whitelist info to details query and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jul 6, 2020
1 parent 73eb5a6 commit de627a3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
8 changes: 8 additions & 0 deletions contracts/cw20-escrow/schema/details_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"required": [
"arbiter",
"cw20_balance",
"cw20_whitelist",
"id",
"native_balance",
"recipient",
Expand All @@ -26,6 +27,13 @@
"$ref": "#/definitions/Cw20CoinHuman"
}
},
"cw20_whitelist": {
"description": "Whitelisted cw20 tokens",
"type": "array",
"items": {
"$ref": "#/definitions/HumanAddr"
}
},
"end_height": {
"description": "When end height set and block height exceeds this value, the escrow is expired. Once an escrow is expired, it can be returned to the original funder (via \"refund\").",
"type": [
Expand Down
47 changes: 45 additions & 2 deletions contracts/cw20-escrow/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ fn query_details<S: Storage, A: Api, Q: Querier>(
) -> StdResult<DetailsResponse> {
let escrow = escrows_read(&deps.storage).load(id.as_bytes())?;

let cw20_whitelist = escrow.human_whitelist(&deps.api)?;

// transform tokens
let cw20_balance: StdResult<Vec<_>> = escrow
.cw20_balance
Expand All @@ -334,6 +336,7 @@ fn query_details<S: Storage, A: Api, Q: Querier>(
end_time: escrow.end_time,
native_balance: escrow.native_balance,
cw20_balance: cw20_balance?,
cw20_whitelist,
};
Ok(details)
}
Expand Down Expand Up @@ -369,7 +372,7 @@ mod tests {
arbiter: HumanAddr::from("arbitrate"),
recipient: HumanAddr::from("recd"),
end_time: None,
end_height: None,
end_height: Some(123456),
cw20_whitelist: None,
};
let sender = HumanAddr::from("source");
Expand All @@ -379,6 +382,23 @@ mod tests {
assert_eq!(0, res.messages.len());
assert_eq!(log("action", "create"), res.log[0]);

// ensure the details is what we expect
let details = query_details(&deps, "foobar".to_string()).unwrap();
assert_eq!(
details,
DetailsResponse {
id: "foobar".to_string(),
arbiter: HumanAddr::from("arbitrate"),
recipient: HumanAddr::from("recd"),
source: HumanAddr::from("source"),
end_height: Some(123456),
end_time: None,
native_balance: balance.clone(),
cw20_balance: vec![],
cw20_whitelist: vec![],
}
);

// approve it
let id = create.id.clone();
let env = mock_env(&deps.api, &create.arbiter, &[]);
Expand Down Expand Up @@ -421,7 +441,7 @@ mod tests {
recipient: HumanAddr::from("recd"),
end_time: None,
end_height: None,
cw20_whitelist: None,
cw20_whitelist: Some(vec![HumanAddr::from("other-token")]),
};
let receive = Cw20ReceiveMsg {
sender: HumanAddr::from("source"),
Expand All @@ -434,6 +454,29 @@ mod tests {
assert_eq!(0, res.messages.len());
assert_eq!(log("action", "create"), res.log[0]);

// ensure the whitelist is what we expect
let details = query_details(&deps, "foobar".to_string()).unwrap();
assert_eq!(
details,
DetailsResponse {
id: "foobar".to_string(),
arbiter: HumanAddr::from("arbitrate"),
recipient: HumanAddr::from("recd"),
source: HumanAddr::from("source"),
end_height: None,
end_time: None,
native_balance: vec![],
cw20_balance: vec![Cw20CoinHuman {
address: HumanAddr::from("my-cw20-token"),
amount: Uint128(100)
}],
cw20_whitelist: vec![
HumanAddr::from("other-token"),
HumanAddr::from("my-cw20-token")
],
}
);

// approve it
let id = create.id.clone();
let env = mock_env(&deps.api, &create.arbiter, &[]);
Expand Down
2 changes: 2 additions & 0 deletions contracts/cw20-escrow/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub struct DetailsResponse {
pub native_balance: Vec<Coin>,
/// Balance in cw20 tokens
pub cw20_balance: Vec<Cw20CoinHuman>,
/// Whitelisted cw20 tokens
pub cw20_whitelist: Vec<HumanAddr>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
Expand Down
10 changes: 9 additions & 1 deletion contracts/cw20-escrow/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
CanonicalAddr, Coin, Env, Order, ReadonlyStorage, StdError, StdResult, Storage, Uint128,
Api, CanonicalAddr, Coin, Env, HumanAddr, Order, ReadonlyStorage, StdError, StdResult, Storage,
Uint128,
};
use cosmwasm_storage::{bucket, bucket_read, prefixed_read, Bucket, ReadonlyBucket};

Expand Down Expand Up @@ -51,6 +52,13 @@ impl Escrow {

false
}

pub fn human_whitelist<A: Api>(&self, api: &A) -> StdResult<Vec<HumanAddr>> {
self.cw20_whitelist
.iter()
.map(|h| api.human_address(h))
.collect()
}
}

pub const PREFIX_ESCROW: &[u8] = b"escrow";
Expand Down

0 comments on commit de627a3

Please sign in to comment.