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

Migrate to v0.11 #104

Merged
merged 20 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion contracts/cw20-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cosmwasm-std = { version = "0.11.0-alpha3", features = ["iterator"] }
cosmwasm-storage = { version = "0.11.0-alpha3", features = ["iterator"] }
schemars = "0.7"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
snafu = { version = "0.6.3" }
thiserror = { version = "1.0.20" }

[dev-dependencies]
cosmwasm-schema = { version = "0.11.0-alpha3" }
47 changes: 21 additions & 26 deletions contracts/cw20-escrow/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cosmwasm_std::{
attr, from_binary, to_binary, Api, BankMsg, Binary, Coin, CosmosMsg, Env, Extern,
HandleResponse, HumanAddr, InitResponse, Querier, StdError, StdResult, Storage, WasmMsg,
attr, from_binary, to_binary, Api, BankMsg, Binary, CosmosMsg, Env, Extern, HandleResponse,
HumanAddr, InitResponse, Querier, StdResult, Storage, WasmMsg,
};
use cosmwasm_storage::prefixed;

use cw2::set_contract_version;
use cw20::{Cw20Coin, Cw20CoinHuman, Cw20HandleMsg, Cw20ReceiveMsg};
use cw20_atomic_swap::balance::Balance;

use crate::error::ContractError;
use crate::msg::{
CreateMsg, DetailsResponse, HandleMsg, InitMsg, ListResponse, QueryMsg, ReceiveMsg,
};
Expand All @@ -31,7 +32,7 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
msg: HandleMsg,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
match msg {
HandleMsg::Create(msg) => try_create(
deps,
Expand All @@ -50,10 +51,10 @@ pub fn try_receive<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
wrapper: Cw20ReceiveMsg,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
let msg: ReceiveMsg = match wrapper.msg {
Some(bin) => from_binary(&bin),
None => Err(StdError::parse_err("ReceiveMsg", "no data")),
Some(bin) => Ok(from_binary(&bin)?),
None => Err(ContractError::NoData {}),
}?;
let balance = Balance::Cw20(Cw20Coin {
address: deps.api.canonical_address(&env.message.sender)?,
Expand All @@ -70,9 +71,9 @@ pub fn try_create<S: Storage, A: Api, Q: Querier>(
msg: CreateMsg,
balance: Balance,
sender: &HumanAddr,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
if balance.is_empty() {
return Err(StdError::generic_err("Send some coins to create an escrow"));
return Err(ContractError::EmptyBalance {});
}

let mut cw20_whitelist = msg.canonical_whitelist(&deps.api)?;
Expand Down Expand Up @@ -107,7 +108,7 @@ pub fn try_create<S: Storage, A: Api, Q: Querier>(
// try to store it, fail if the id was already in use
escrows(&mut deps.storage).update(msg.id.as_bytes(), |existing| match existing {
None => Ok(escrow),
Some(_) => Err(StdError::generic_err("escrow id already in use")),
Some(_) => Err(ContractError::AlreadyInUse {}),
})?;

let mut res = HandleResponse::default();
Expand All @@ -119,21 +120,17 @@ pub fn try_top_up<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
id: String,
balance: Balance,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
if balance.is_empty() {
return Err(StdError::generic_err(
"Send some amount to increase an escrow",
));
return Err(ContractError::EmptyBalance {});
}
// this fails is no escrow there
let mut escrow = escrows_read(&deps.storage).load(id.as_bytes())?;

if let Balance::Cw20(token) = &balance {
// ensure the token is on the whitelist
if !escrow.cw20_whitelist.iter().any(|t| t == &token.address) {
return Err(StdError::generic_err(
"Only accepts tokens on the cw20_whitelist",
));
return Err(ContractError::NotInWhitelist {});
}
};

Expand All @@ -151,14 +148,14 @@ pub fn try_approve<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
id: String,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
// this fails is no escrow there
let escrow = escrows_read(&deps.storage).load(id.as_bytes())?;

if deps.api.canonical_address(&env.message.sender)? != escrow.arbiter {
Err(StdError::unauthorized())
Err(ContractError::Unauthorized {})
} else if escrow.is_expired(&env) {
Err(StdError::generic_err("escrow expired"))
Err(ContractError::Expired {})
} else {
// we delete the escrow (TODO: expose this in Bucket for simpler API)
prefixed(&mut deps.storage, PREFIX_ESCROW).remove(id.as_bytes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here... time to update

Expand All @@ -181,15 +178,15 @@ pub fn try_refund<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
id: String,
) -> StdResult<HandleResponse> {
) -> Result<HandleResponse, ContractError> {
// this fails is no escrow there
let escrow = escrows_read(&deps.storage).load(id.as_bytes())?;

// the arbiter can send anytime OR anyone can send after expiration
if !escrow.is_expired(&env)
&& deps.api.canonical_address(&env.message.sender)? != escrow.arbiter
{
Err(StdError::unauthorized())
Err(ContractError::Unauthorized {})
} else {
// we delete the escrow (TODO: expose this in Bucket for simpler API)
prefixed(&mut deps.storage, PREFIX_ESCROW).remove(id.as_bytes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this is now in Bucket.remove you can use that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change those in another branch / PR.

Expand Down Expand Up @@ -373,7 +370,7 @@ mod tests {
let env = mock_env(&create.arbiter, &[]);
let res = handle(&mut deps, env, HandleMsg::Approve { id });
match res.unwrap_err() {
StdError::NotFound { .. } => {}
ContractError::Std(StdError::NotFound { .. }) => {}
e => panic!("Expected NotFound, got {}", e),
}
}
Expand Down Expand Up @@ -455,7 +452,7 @@ mod tests {
let env = mock_env(&create.arbiter, &[]);
let res = handle(&mut deps, env, HandleMsg::Approve { id });
match res.unwrap_err() {
StdError::NotFound { .. } => {}
ContractError::Std(StdError::NotFound { .. }) => {}
e => panic!("Expected NotFound, got {}", e),
}
}
Expand Down Expand Up @@ -571,9 +568,7 @@ mod tests {
let env = mock_env(&baz_token, &[]);
let res = handle(&mut deps, env, top_up);
match res.unwrap_err() {
StdError::GenericErr { msg, .. } => {
assert_eq!(msg, "Only accepts tokens on the cw20_whitelist")
}
ContractError::NotInWhitelist {} => {}
e => panic!("Unexpected error: {}", e),
}

Expand Down
26 changes: 26 additions & 0 deletions contracts/cw20-escrow/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("No data in ReceiveMsg")]
NoData {},

#[error("Unauthorized")]
Unauthorized {},

#[error("Only accepts tokens in the cw20_whitelist")]
NotInWhitelist {},

#[error("Escrow is expired")]
Expired {},

#[error("Send some coins to create an escrow")]
EmptyBalance {},

#[error("Escrow id already in use")]
AlreadyInUse {},
}
1 change: 1 addition & 0 deletions contracts/cw20-escrow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod contract;
mod error;
pub mod msg;
pub mod state;

Expand Down