Skip to content

Commit

Permalink
Merge pull request #87 from CosmWasm/native-balance-refactoring
Browse files Browse the repository at this point in the history
Native balance refactoring
  • Loading branch information
ethanfrey committed Sep 15, 2020
2 parents 243801c + 35bd6c2 commit 322f192
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 81 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.

14 changes: 7 additions & 7 deletions contracts/cw1-subkeys/schema/all_allowances_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"properties": {
"balance": {
"$ref": "#/definitions/Balance"
"$ref": "#/definitions/NativeBalance"
},
"expires": {
"$ref": "#/definitions/Expiration"
Expand All @@ -33,12 +33,6 @@
}
}
},
"Balance": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
},
"Coin": {
"type": "object",
"required": [
Expand Down Expand Up @@ -101,6 +95,12 @@
"HumanAddr": {
"type": "string"
},
"NativeBalance": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
},
"Uint128": {
"type": "string"
}
Expand Down
14 changes: 7 additions & 7 deletions contracts/cw1-subkeys/schema/allowance.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
],
"properties": {
"balance": {
"$ref": "#/definitions/Balance"
"$ref": "#/definitions/NativeBalance"
},
"expires": {
"$ref": "#/definitions/Expiration"
}
},
"definitions": {
"Balance": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
},
"Coin": {
"type": "object",
"required": [
Expand Down Expand Up @@ -80,6 +74,12 @@
}
]
},
"NativeBalance": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
},
"Uint128": {
"type": "string"
}
Expand Down
30 changes: 15 additions & 15 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ pub fn query_all_permissions<S: Storage, A: Api, Q: Querier>(
#[cfg(test)]
mod tests {
use super::*;
use crate::balance::Balance;
use crate::state::Permissions;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins, StakingMsg};
use cw0::NativeBalance;
use cw1_whitelist::msg::AdminListResponse;
use cw2::{get_contract_version, ContractVersion};

Expand Down Expand Up @@ -550,15 +550,15 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone()]),
balance: NativeBalance(vec![allow1.clone()]),
expires: expires_never.clone(),
}
);
let allowance = query_allowance(&deps, spender2.clone()).unwrap();
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone()]),
balance: NativeBalance(vec![allow1.clone()]),
expires: expires_never.clone(),
}
);
Expand Down Expand Up @@ -608,15 +608,15 @@ mod tests {
allowances[0],
AllowanceInfo {
spender: spender1,
balance: Balance(initial_allowances.clone()),
balance: NativeBalance(initial_allowances.clone()),
expires: Expiration::Never {},
}
);
assert_eq!(
allowances[1],
AllowanceInfo {
spender: spender2.clone(),
balance: Balance(initial_allowances.clone()),
balance: NativeBalance(initial_allowances.clone()),
expires: Expiration::Never {},
}
);
Expand All @@ -630,7 +630,7 @@ mod tests {
allowances[0],
AllowanceInfo {
spender: spender3,
balance: Balance(initial_allowances.clone()),
balance: NativeBalance(initial_allowances.clone()),
expires: expires_later,
}
);
Expand Down Expand Up @@ -934,7 +934,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![coin(amount1 * 2, &allow1.denom), allow2.clone()]),
balance: NativeBalance(vec![coin(amount1 * 2, &allow1.denom), allow2.clone()]),
expires: expires_height.clone(),
}
);
Expand All @@ -952,7 +952,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone(), allow2.clone(), allow3.clone()]),
balance: NativeBalance(vec![allow1.clone(), allow2.clone(), allow3.clone()]),
expires: expires_height.clone(),
}
);
Expand All @@ -970,7 +970,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone()]),
balance: NativeBalance(vec![allow1.clone()]),
expires: expires_never.clone(),
}
);
Expand All @@ -988,7 +988,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow2.clone()]),
balance: NativeBalance(vec![allow2.clone()]),
expires: expires_time,
}
);
Expand Down Expand Up @@ -1049,7 +1049,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone(), allow2.clone()]),
balance: NativeBalance(vec![allow1.clone(), allow2.clone()]),
expires: expires_height.clone(),
}
);
Expand All @@ -1067,7 +1067,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow1.clone()]),
balance: NativeBalance(vec![allow1.clone()]),
expires: expires_never.clone(),
}
);
Expand All @@ -1085,7 +1085,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![
balance: NativeBalance(vec![
coin(amount1 / 2 + (amount1 & 1), denom1),
allow2.clone()
]),
Expand Down Expand Up @@ -1129,7 +1129,7 @@ mod tests {
assert_eq!(
allowance,
Allowance {
balance: Balance(vec![allow2]),
balance: NativeBalance(vec![allow2]),
expires: expires_height.clone(),
}
);
Expand Down Expand Up @@ -1378,7 +1378,7 @@ mod tests {
let coin = coin(amount, denom);

let allow = Allowance {
balance: Balance(vec![coin.clone()]),
balance: NativeBalance(vec![coin.clone()]),
expires: Expiration::Never {},
};
let perm = Permissions {
Expand Down
1 change: 0 additions & 1 deletion contracts/cw1-subkeys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod balance;
pub mod contract;
pub mod msg;
pub mod state;
Expand Down
5 changes: 2 additions & 3 deletions contracts/cw1-subkeys/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use serde::{Deserialize, Serialize};
use std::fmt;

use cosmwasm_std::{Coin, CosmosMsg, Empty, HumanAddr};
use cw0::Expiration;
use cw0::{Expiration, NativeBalance};

use crate::balance::Balance;
use crate::state::Permissions;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -88,7 +87,7 @@ pub struct AllAllowancesResponse {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct AllowanceInfo {
pub spender: HumanAddr,
pub balance: Balance,
pub balance: NativeBalance,
pub expires: Expiration,
}

Expand Down
5 changes: 2 additions & 3 deletions contracts/cw1-subkeys/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use serde::{Deserialize, Serialize};

use cosmwasm_std::{ReadonlyStorage, StdError, Storage};
use cosmwasm_storage::{bucket, bucket_read, Bucket, ReadonlyBucket};
use cw0::Expiration;
use cw0::{Expiration, NativeBalance};

use crate::balance::Balance;
use std::fmt;

// Permissions struct defines users message execution permissions.
Expand Down Expand Up @@ -70,7 +69,7 @@ impl From<PermissionErr> for StdError {

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Default)]
pub struct Allowance {
pub balance: Balance,
pub balance: NativeBalance,
pub expires: Expiration,
}

Expand Down
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
16 changes: 12 additions & 4 deletions contracts/cw20-atomic-swap/src/balance.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

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

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

impl Default for Balance {
fn default() -> Balance {
Balance::Native(vec![])
Balance::Native(NativeBalance(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(_) => {}
}
}
}
9 changes: 5 additions & 4 deletions contracts/cw20-atomic-swap/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -36,7 +37,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(NativeBalance(sent_funds)))
}
HandleMsg::Release { id, preimage } => try_release(deps, env, id, preimage),
HandleMsg::Refund { id } => try_refund(deps, env, id),
Expand Down Expand Up @@ -72,7 +73,7 @@ pub fn try_create<S: Storage, A: Api, Q: Querier>(
return Err(StdError::generic_err("Invalid atomic swap id"));
}

// FIXME: normalize array first (remove zero-valued coins), and then check for empty
// this ignores 0 value coins, must have one or more with positive balance
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(NativeBalance(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(coins) => BalanceHuman::Native(coins.0),
Balance::Cw20(coin) => BalanceHuman::Cw20(Cw20CoinHuman {
address: deps.api.human_address(&coin.address)?,
amount: coin.amount,
Expand Down
Loading

0 comments on commit 322f192

Please sign in to comment.