Skip to content

Commit

Permalink
cleaned up the Balance query
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenpo committed Sep 21, 2020
1 parent d582d5f commit 3300691
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
77 changes: 33 additions & 44 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{
to_binary, Api, BankMsg, Binary, CanonicalAddr, Coin, CosmosMsg, Decimal, Env, Extern,
HandleResponse, HumanAddr, InitResponse, Querier, QueryResult, ReadonlyStorage, StdError,
StdResult, Storage, Uint128, WasmMsg,
to_binary, Api, BankMsg, Binary, CanonicalAddr, Coin, CosmosMsg, Env, Extern, HandleResponse,
HumanAddr, InitResponse, Querier, QueryResult, ReadonlyStorage, StdError, StdResult, Storage,
Uint128, WasmMsg,
};

use crate::msg::{
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
// Native
HandleMsg::Deposit { .. } => try_deposit(deps, env),
HandleMsg::Redeem { amount, .. } => try_redeem(deps, env, amount),
HandleMsg::Balance /* todo move to query? */ { .. } => try_balance(deps, env),
HandleMsg::Balance { .. } => try_balance(deps, env),
// Base
HandleMsg::Transfer {
recipient, amount, ..
Expand All @@ -92,10 +92,16 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
HandleMsg::SetViewingKey { key, .. } => try_set_key(deps, env, key),
// Allowance
HandleMsg::IncreaseAllowance {
spender, amount, expiration, ..
spender,
amount,
expiration,
..
} => try_increase_allowance(deps, env, spender, amount, expiration),
HandleMsg::DecreaseAllowance {
spender, amount, expiration, ..
spender,
amount,
expiration,
..
} => try_decrease_allowance(deps, env, spender, amount, expiration),
HandleMsg::TransferFrom {
owner,
Expand All @@ -110,9 +116,14 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
msg,
..
} => try_send_from(deps, env, &owner, &recipient, amount, msg),
HandleMsg::BurnFrom {owner, amount, ..} => try_burn_from(deps, env, &owner, amount),
HandleMsg::BurnFrom { owner, amount, .. } => try_burn_from(deps, env, &owner, amount),
// Other
HandleMsg::Swap { amount, network, destination, .. } => try_swap(deps, env, amount, network, destination),
HandleMsg::Swap {
amount,
network,
destination,
..
} => try_swap(deps, env, amount, network, destination),
};

response.map(|mut response| {
Expand Down Expand Up @@ -222,7 +233,10 @@ pub fn query_balance<S: Storage, A: Api, Q: Querier>(
) -> StdResult<Binary> {
let address = deps.api.canonical_address(account)?;

Ok(Binary(Vec::from(get_balance(deps, &address)?)))
let response = QueryAnswer::Balance {
amount: Uint128(get_balance(&deps.storage, &address)),
};
to_binary(&response)
}

fn change_admin<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -372,36 +386,19 @@ pub fn try_balance<S: Storage, A: Api, Q: Querier>(
env: Env,
) -> StdResult<HandleResponse> {
let sender_address = deps.api.canonical_address(&env.message.sender)?;
let account_balance = get_balance(deps, &sender_address);
let account_balance = get_balance(&deps.storage, &sender_address);

if let Err(_e) = account_balance {
Ok(HandleResponse {
messages: vec![],
log: vec![],
data: None,
})
} else {
Ok(HandleResponse {
messages: vec![],
log: vec![],
data: None,
})
}
Ok(HandleResponse {
messages: vec![],
log: vec![],
data: Some(to_binary(&HandleAnswer::Balance {
amount: Uint128(account_balance),
})?),
})
}

fn get_balance<S: Storage, A: Api, Q: Querier>(
deps: &Extern<S, A, Q>,
account: &CanonicalAddr,
) -> StdResult<String> {
let account_balance = ReadonlyBalances::from_storage(&deps.storage).account_amount(account);

let consts = ReadonlyConfig::from_storage(&deps.storage).constants()?;

Ok(to_display_token(
account_balance,
&consts.symbol,
consts.decimals,
))
fn get_balance<S: Storage>(storage: &S, account: &CanonicalAddr) -> u128 {
ReadonlyBalances::from_storage(storage).account_amount(account)
}

fn try_deposit<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -913,14 +910,6 @@ fn is_valid_symbol(symbol: &str) -> bool {
len_is_valid && symbol.bytes().all(|byte| b'A' <= byte && byte <= b'Z')
}

fn to_display_token(amount: u128, symbol: &str, decimals: u8) -> String {
let base: u32 = 10;

let amnt: Decimal = Decimal::from_ratio(amount, (base.pow(decimals.into())) as u128);

format!("{} {}", amnt, symbol)
}

// pub fn migrate<S: Storage, A: Api, Q: Querier>(
// _deps: &mut Extern<S, A, Q>,
// _env: Env,
Expand Down
6 changes: 6 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub enum HandleAnswer {
Swap {
status: ResponseStatus,
},
Balance {
amount: Uint128,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -244,6 +247,9 @@ pub enum QueryAnswer {
Swap {
result: Swap,
},
Balance {
amount: Uint128,
},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
Expand Down

0 comments on commit 3300691

Please sign in to comment.