Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ben2x4 committed Jan 19, 2022
1 parent bd247ff commit 199e66f
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions contracts/stake-cw20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use cosmwasm_std::{

use cw20::{Cw20QueryMsg, Cw20ReceiveMsg};

use crate::msg::{ExecuteMsg, GetChangeLogResponse, InstantiateMsg, QueryMsg, ReceiveMsg, StakedBalanceAtHeightResponse, StakedValueResponse, TotalStakedAtHeightResponse, TotalValueResponse, UnstakingDurationResponse};
use crate::msg::{
ExecuteMsg, GetChangeLogResponse, InstantiateMsg, QueryMsg, ReceiveMsg,
StakedBalanceAtHeightResponse, StakedValueResponse, TotalStakedAtHeightResponse,
TotalValueResponse, UnstakingDurationResponse,
};
use crate::state::{Config, CLAIMS, CONFIG, STAKED_BALANCES, STAKED_TOTAL};
use crate::ContractError;
pub use cw20_base::allowances::{
Expand Down Expand Up @@ -218,12 +222,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::TotalStakedAtHeight { height } => {
to_binary(&query_total_staked_at_height(deps, env, height)?)
}
QueryMsg::StakedValue { address} => {
to_binary(&query_staked_value(deps, env, address)?)
}
QueryMsg::TotalValue {} => {
to_binary(&query_total_value(deps, env)?)
}
QueryMsg::StakedValue { address } => to_binary(&query_staked_value(deps, env, address)?),
QueryMsg::TotalValue {} => to_binary(&query_total_value(deps, env)?),
QueryMsg::UnstakingDuration {} => to_binary(&query_unstaking_duration(deps)?),
QueryMsg::Claims { address } => to_binary(&query_claims(deps, address)?),
QueryMsg::GetChangelog {
Expand Down Expand Up @@ -266,11 +266,7 @@ pub fn query_total_staked_at_height(
Ok(TotalStakedAtHeightResponse { total, height })
}

pub fn query_staked_value(
deps: Deps,
env: Env,
address: String,
) -> StdResult<StakedValueResponse> {
pub fn query_staked_value(deps: Deps, env: Env, address: String) -> StdResult<StakedValueResponse> {
let config = CONFIG.load(deps.storage)?;
let address = deps.api.addr_validate(&address)?;
let balance: cw20::BalanceResponse = deps.querier.query_wasm_smart(
Expand All @@ -283,29 +279,32 @@ pub fn query_staked_value(
let staked = STAKED_BALANCES
.load(deps.storage, &address)
.unwrap_or_default();
let total = STAKED_TOTAL
.load(deps.storage)
.unwrap_or_default();
let total = STAKED_TOTAL.load(deps.storage).unwrap_or_default();
if balance == Uint128::zero() || staked == Uint128::zero() || total == Uint128::zero() {
Ok(StakedValueResponse{value: Uint128::zero()})
Ok(StakedValueResponse {
value: Uint128::zero(),
})
} else {
let value = staked.checked_mul(balance).map_err(StdError::overflow)?.checked_div(total).map_err(StdError::divide_by_zero)?;
Ok(StakedValueResponse{value})
let value = staked
.checked_mul(balance)
.map_err(StdError::overflow)?
.checked_div(total)
.map_err(StdError::divide_by_zero)?;
Ok(StakedValueResponse { value })
}
}

pub fn query_total_value(
deps: Deps,
env: Env,
) -> StdResult<TotalValueResponse> {
pub fn query_total_value(deps: Deps, env: Env) -> StdResult<TotalValueResponse> {
let config = CONFIG.load(deps.storage)?;
let balance: cw20::BalanceResponse = deps.querier.query_wasm_smart(
&config.token_address,
&Cw20QueryMsg::Balance {
address: env.contract.address.to_string(),
},
)?;
Ok(TotalValueResponse {total: balance.balance})
Ok(TotalValueResponse {
total: balance.balance,
})
}

pub fn query_unstaking_duration(deps: Deps) -> StdResult<UnstakingDurationResponse> {
Expand Down Expand Up @@ -367,7 +366,11 @@ pub fn query_changelog(
mod tests {
use std::borrow::BorrowMut;

use crate::msg::{ExecuteMsg, GetChangeLogResponse, QueryMsg, ReceiveMsg, StakedBalanceAtHeightResponse, StakedValueResponse, TotalStakedAtHeightResponse, TotalValueResponse, UnstakingDurationResponse};
use crate::msg::{
ExecuteMsg, GetChangeLogResponse, QueryMsg, ReceiveMsg, StakedBalanceAtHeightResponse,
StakedValueResponse, TotalStakedAtHeightResponse, TotalValueResponse,
UnstakingDurationResponse,
};
use crate::ContractError;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{to_binary, Addr, Empty, MessageInfo, Uint128};
Expand Down Expand Up @@ -497,15 +500,13 @@ mod tests {
let msg = QueryMsg::StakedValue {
address: address.into(),
};
let result: StakedValueResponse =
app.wrap().query_wasm_smart(contract_addr, &msg).unwrap();
let result: StakedValueResponse = app.wrap().query_wasm_smart(contract_addr, &msg).unwrap();
result.value
}

fn query_total_value<T: Into<String>>(app: &App, contract_addr: T) -> Uint128 {
let msg = QueryMsg::TotalValue {};
let result: TotalValueResponse =
app.wrap().query_wasm_smart(contract_addr, &msg).unwrap();
let result: TotalValueResponse = app.wrap().query_wasm_smart(contract_addr, &msg).unwrap();
result.total
}

Expand Down Expand Up @@ -1142,4 +1143,3 @@ mod tests {
assert_eq!(get_balance(&app, &cw20_addr, ADDR2), Uint128::from(65u128));
}
}

0 comments on commit 199e66f

Please sign in to comment.