Skip to content

Commit

Permalink
Use cw0::Balance for atomic swap Balance
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Sep 14, 2020
1 parent 48329ed commit f25477e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions contracts/cw20-atomic-swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ singlepass = ["cosmwasm-vm/default-singlepass"]
library = []

[dependencies]
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.0", features = ["iterator"] }
Expand Down
15 changes: 11 additions & 4 deletions contracts/cw20-atomic-swap/src/balance.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Coin;
use cw20::Cw20Coin;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Balance {
Native(Vec<Coin>),
Native(cw0::Balance),
Cw20(Cw20Coin),
}

impl Default for Balance {
fn default() -> Balance {
Balance::Native(vec![])
Balance::Native(cw0::Balance(vec![]))
}
}

impl Balance {
pub fn is_empty(&self) -> bool {
match self {
Balance::Native(coins) => coins.is_empty(),
Balance::Native(balance) => balance.is_empty(),
Balance::Cw20(coin) => coin.is_empty(),
}
}

/// normalize Wallet
pub fn normalize(&mut self) {
match self {
Balance::Native(balance) => balance.normalize(),
Balance::Cw20(_) => {}
}
}
}
11 changes: 6 additions & 5 deletions contracts/cw20-atomic-swap/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,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(sent_funds))
try_create(deps, env, msg, Balance::Native(cw0::Balance(sent_funds)))
}
HandleMsg::Release { id, preimage } => try_release(deps, env, id, preimage),
HandleMsg::Refund { id } => try_refund(deps, env, id),
Expand Down Expand Up @@ -66,13 +66,14 @@ pub fn try_create<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
msg: CreateMsg,
balance: Balance,
mut balance: Balance,
) -> StdResult<HandleResponse> {
if !is_valid_name(&msg.id) {
return Err(StdError::generic_err("Invalid atomic swap id"));
}

// FIXME: normalize array first (remove zero-valued coins), and then check for empty
// Normalize coins first
balance.normalize();
if balance.is_empty() {
return Err(StdError::generic_err(
"Send some coins to create an atomic swap",
Expand Down Expand Up @@ -197,7 +198,7 @@ fn send_tokens<A: Api>(
Ok(vec![])
} else {
match amount {
Balance::Native(coins) => {
Balance::Native(cw0::Balance(coins)) => {
let msg = BankMsg::Send {
from_address: from.into(),
to_address: to.into(),
Expand Down Expand Up @@ -239,7 +240,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),
Balance::Native(cw0::Balance(coins)) => BalanceHuman::Native(coins),
Balance::Cw20(coin) => BalanceHuman::Cw20(Cw20CoinHuman {
address: deps.api.human_address(&coin.address)?,
amount: coin.amount,
Expand Down

0 comments on commit f25477e

Please sign in to comment.