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

cw20-escrow: Upgrade to 0.15 #310

Merged
merged 2 commits into from
Jun 29, 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
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions contracts/cw20-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ library = []
cw0 = { path = "../../packages/cw0", version = "0.6.2" }
cw2 = { path = "../../packages/cw2", version = "0.6.2" }
cw20 = { path = "../../packages/cw20", version = "0.6.2" }
cosmwasm-std = { version = "0.14.0", features = ["iterator"] }
cosmwasm-std = { version = "0.15.0", features = ["iterator"] }
cw-storage-plus = { path = "../../packages/storage-plus", version = "0.6.2", features = ["iterator"] }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.20" }
thiserror = { version = "1.0.23" }

[dev-dependencies]
cosmwasm-schema = { version = "0.14.0" }
cosmwasm-schema = { version = "0.15.0" }
cw-multi-test = { path = "../../packages/multi-test", version = "0.6.2" }
cw20-base = { path = "../cw20-base", version = "0.6.2", features = ["library"] }
77 changes: 38 additions & 39 deletions contracts/cw20-escrow/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, from_binary, to_binary, Addr, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Env,
MessageInfo, Response, StdResult, WasmMsg,
attr, from_binary, to_binary, Addr, BankMsg, Binary, Deps, DepsMut, Env, MessageInfo, Response,
StdResult, SubMsg, WasmMsg,
};

use cw2::set_contract_version;
Expand Down Expand Up @@ -167,17 +167,17 @@ pub fn execute_approve(
ESCROWS.remove(deps.storage, &id);

// send all tokens out
let messages = send_tokens(&escrow.recipient, &escrow.balance)?;
let messages: Vec<SubMsg> = send_tokens(&escrow.recipient, &escrow.balance)?;

let attributes = vec![
attr("action", "approve"),
attr("id", id),
attr("to", escrow.recipient),
];
Ok(Response {
submessages: vec![],
messages,
attributes,
events: vec![],
data: None,
})
}
Expand Down Expand Up @@ -208,24 +208,23 @@ pub fn execute_refund(
attr("to", escrow.source),
];
Ok(Response {
submessages: vec![],
messages,
attributes,
events: vec![],
data: None,
})
}
}

fn send_tokens(to: &Addr, balance: &GenericBalance) -> StdResult<Vec<CosmosMsg>> {
fn send_tokens(to: &Addr, balance: &GenericBalance) -> StdResult<Vec<SubMsg>> {
let native_balance = &balance.native;
let mut msgs: Vec<CosmosMsg> = if native_balance.is_empty() {
let mut msgs: Vec<SubMsg> = if native_balance.is_empty() {
vec![]
} else {
vec![BankMsg::Send {
vec![SubMsg::new(BankMsg::Send {
to_address: to.into(),
amount: native_balance.to_vec(),
}
.into()]
})]
};

let cw20_balance = &balance.cw20;
Expand All @@ -236,12 +235,12 @@ fn send_tokens(to: &Addr, balance: &GenericBalance) -> StdResult<Vec<CosmosMsg>>
recipient: to.into(),
amount: c.amount,
};
let exec = WasmMsg::Execute {
let exec = SubMsg::new(WasmMsg::Execute {
contract_addr: c.address.to_string(),
msg: to_binary(&msg)?,
send: vec![],
};
Ok(exec.into())
funds: vec![],
});
Ok(exec)
})
.collect();
msgs.append(&mut cw20_msgs?);
Expand Down Expand Up @@ -357,10 +356,10 @@ mod tests {
assert_eq!(attr("action", "approve"), res.attributes[0]);
assert_eq!(
res.messages[0],
CosmosMsg::Bank(BankMsg::Send {
SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: create.recipient,
amount: balance,
})
}))
);

// second attempt fails (not found)
Expand Down Expand Up @@ -391,7 +390,7 @@ mod tests {
};
let receive = Cw20ReceiveMsg {
sender: String::from("source"),
amount: Uint128(100),
amount: Uint128::new(100),
msg: to_binary(&ExecuteMsg::Create(create.clone())).unwrap(),
};
let token_contract = String::from("my-cw20-token");
Expand All @@ -415,7 +414,7 @@ mod tests {
native_balance: vec![],
cw20_balance: vec![Cw20Coin {
address: String::from("my-cw20-token"),
amount: Uint128(100),
amount: Uint128::new(100),
}],
cw20_whitelist: vec![String::from("other-token"), String::from("my-cw20-token")],
}
Expand All @@ -433,11 +432,11 @@ mod tests {
};
assert_eq!(
res.messages[0],
CosmosMsg::Wasm(WasmMsg::Execute {
SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_contract,
msg: to_binary(&send_msg).unwrap(),
send: vec![],
})
funds: vec![]
}))
);

// second attempt fails (not found)
Expand Down Expand Up @@ -465,26 +464,26 @@ mod tests {
let foo_token = Addr::unchecked("foo_token");
tokens.add_tokens(Balance::Cw20(Cw20CoinVerified {
address: foo_token.clone(),
amount: Uint128(12345),
amount: Uint128::new(12345),
}));
tokens.add_tokens(Balance::Cw20(Cw20CoinVerified {
address: bar_token.clone(),
amount: Uint128(777),
amount: Uint128::new(777),
}));
tokens.add_tokens(Balance::Cw20(Cw20CoinVerified {
address: foo_token.clone(),
amount: Uint128(23400),
amount: Uint128::new(23400),
}));
assert_eq!(
tokens.cw20,
vec![
Cw20CoinVerified {
address: foo_token,
amount: Uint128(35745),
amount: Uint128::new(35745),
},
Cw20CoinVerified {
address: bar_token,
amount: Uint128(777),
amount: Uint128::new(777),
}
]
);
Expand Down Expand Up @@ -537,7 +536,7 @@ mod tests {
};
let top_up = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("random"),
amount: Uint128(7890),
amount: Uint128::new(7890),
msg: to_binary(&base).unwrap(),
});
let info = mock_info(&bar_token, &[]);
Expand All @@ -553,7 +552,7 @@ mod tests {
};
let top_up = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("random"),
amount: Uint128(7890),
amount: Uint128::new(7890),
msg: to_binary(&base).unwrap(),
});
let info = mock_info(&baz_token, &[]);
Expand All @@ -567,7 +566,7 @@ mod tests {
};
let top_up = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: String::from("random"),
amount: Uint128(888),
amount: Uint128::new(888),
msg: to_binary(&base).unwrap(),
});
let info = mock_info(&foo_token, &[]);
Expand All @@ -585,38 +584,38 @@ mod tests {
// first message releases all native coins
assert_eq!(
res.messages[0],
CosmosMsg::Bank(BankMsg::Send {
SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: create.recipient.clone(),
amount: vec![coin(100, "fee"), coin(500, "stake"), coin(250, "random")],
})
}))
);

// second one release bar cw20 token
let send_msg = Cw20ExecuteMsg::Transfer {
recipient: create.recipient.clone(),
amount: Uint128(7890),
amount: Uint128::new(7890),
};
assert_eq!(
res.messages[1],
CosmosMsg::Wasm(WasmMsg::Execute {
SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: bar_token,
msg: to_binary(&send_msg).unwrap(),
send: vec![],
})
funds: vec![]
}))
);

// third one release foo cw20 token
let send_msg = Cw20ExecuteMsg::Transfer {
recipient: create.recipient,
amount: Uint128(888),
amount: Uint128::new(888),
};
assert_eq!(
res.messages[2],
CosmosMsg::Wasm(WasmMsg::Execute {
SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: foo_token,
msg: to_binary(&send_msg).unwrap(),
send: vec![],
})
funds: vec![]
}))
);
}
}
20 changes: 10 additions & 10 deletions contracts/cw20-escrow/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn escrow_happy_path_cw20_tokens() {
decimals: 2,
initial_balances: vec![Cw20Coin {
address: owner.to_string(),
amount: Uint128(5000),
amount: Uint128::new(5000),
}],
mint: None,
};
Expand All @@ -73,9 +73,9 @@ fn escrow_happy_path_cw20_tokens() {

// ensure our balances
let owner_balance = cash.balance(&router, owner.clone()).unwrap();
assert_eq!(owner_balance, Uint128(5000));
assert_eq!(owner_balance, Uint128::new(5000));
let escrow_balance = cash.balance(&router, escrow_addr.clone()).unwrap();
assert_eq!(escrow_balance, Uint128(0));
assert_eq!(escrow_balance, Uint128::zero());

// send some tokens to create an escrow
let arb = Addr::unchecked("arbiter");
Expand All @@ -92,7 +92,7 @@ fn escrow_happy_path_cw20_tokens() {
let create_bin = to_binary(&create_msg).unwrap();
let send_msg = Cw20ExecuteMsg::Send {
contract: escrow_addr.to_string(),
amount: Uint128(1200),
amount: Uint128::new(1200),
msg: Some(create_bin),
};
let res = router
Expand All @@ -103,9 +103,9 @@ fn escrow_happy_path_cw20_tokens() {

// ensure balances updated
let owner_balance = cash.balance(&router, owner.clone()).unwrap();
assert_eq!(owner_balance, Uint128(3800));
assert_eq!(owner_balance, Uint128::new(3800));
let escrow_balance = cash.balance(&router, escrow_addr.clone()).unwrap();
assert_eq!(escrow_balance, Uint128(1200));
assert_eq!(escrow_balance, Uint128::new(1200));

// ensure escrow properly created
let details: DetailsResponse = router
Expand All @@ -118,7 +118,7 @@ fn escrow_happy_path_cw20_tokens() {
assert_eq!(
vec![Cw20Coin {
address: cash_addr.to_string(),
amount: Uint128(1200)
amount: Uint128::new(1200)
}],
details.cw20_balance
);
Expand All @@ -131,9 +131,9 @@ fn escrow_happy_path_cw20_tokens() {

// ensure balances updated - release to ben
let owner_balance = cash.balance(&router, owner).unwrap();
assert_eq!(owner_balance, Uint128(3800));
assert_eq!(owner_balance, Uint128::new(3800));
let escrow_balance = cash.balance(&router, escrow_addr).unwrap();
assert_eq!(escrow_balance, Uint128(0));
assert_eq!(escrow_balance, Uint128::zero());
let ben_balance = cash.balance(&router, ben).unwrap();
assert_eq!(ben_balance, Uint128(1200));
assert_eq!(ben_balance, Uint128::new(1200));
}