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 refactoring: Unify handling of native and cw20 #92

Merged
merged 7 commits into from
Sep 23, 2020
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions contracts/cw20-atomic-swap/src/balance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Coin;
use cw0::NativeBalance;
use cw20::Cw20Coin;

Expand Down Expand Up @@ -33,3 +34,15 @@ impl Balance {
}
}
}

impl From<Vec<Coin>> for Balance {
fn from(coins: Vec<Coin>) -> Balance {
Balance::Native(NativeBalance(coins))
}
}

impl From<Cw20Coin> for Balance {
fn from(cw20_coin: Cw20Coin) -> Balance {
Balance::Cw20(cw20_coin)
}
}
9 changes: 4 additions & 5 deletions contracts/cw20-atomic-swap/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use cosmwasm_std::{
from_binary, log, to_binary, Api, BankMsg, Binary, CosmosMsg, Env, Extern, HandleResponse,
HumanAddr, InitResponse, Querier, StdError, StdResult, Storage, WasmMsg,
};
use cw0::NativeBalance;
use cw2::set_contract_version;
use cw20::{Cw20Coin, Cw20CoinHuman, Cw20HandleMsg, Cw20ReceiveMsg};

Expand Down Expand Up @@ -37,7 +36,7 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
match msg {
HandleMsg::Create(msg) => {
let sent_funds = env.message.sent_funds.clone();
try_create(deps, env, msg, Balance::Native(NativeBalance(sent_funds)))
try_create(deps, env, msg, Balance::from(sent_funds))
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
}
HandleMsg::Release { id, preimage } => try_release(deps, env, id, preimage),
HandleMsg::Refund { id } => try_refund(deps, env, id),
Expand Down Expand Up @@ -198,11 +197,11 @@ fn send_tokens<A: Api>(
Ok(vec![])
} else {
match amount {
Balance::Native(NativeBalance(coins)) => {
Balance::Native(coins) => {
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
let msg = BankMsg::Send {
from_address: from.into(),
to_address: to.into(),
amount: coins,
amount: coins.into_vec(),
};
Ok(vec![msg.into()])
}
Expand Down Expand Up @@ -240,7 +239,7 @@ fn query_details<S: Storage, A: Api, Q: Querier>(

// Convert balance to human balance
let balance_human = match swap.balance {
Balance::Native(coins) => BalanceHuman::Native(coins.0),
Balance::Native(coins) => BalanceHuman::Native(coins.into_vec()),
Balance::Cw20(coin) => BalanceHuman::Cw20(Cw20CoinHuman {
address: deps.api.human_address(&coin.address)?,
amount: coin.amount,
Expand Down
2 changes: 2 additions & 0 deletions contracts/cw20-escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw20-atomic-swap = { path = "../cw20-atomic-swap", version = "0.2.1", default-features=false, features = ["library"] }
cw0 = { path = "../../packages/cw0", version = "0.2.1" }
cw2 = { path = "../../packages/cw2", version = "0.2.1" }
cw20 = { path = "../../packages/cw20", version = "0.2.1" }
cosmwasm-std = { version = "0.10.1", features = ["iterator"] }
Expand Down
Loading