Skip to content

Commit

Permalink
fix contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Mar 30, 2021
1 parent 0a99ba2 commit 2d88c6b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
34 changes: 17 additions & 17 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ pub fn transfer(
let sender_raw = deps.api.canonical_address(&info.sender)?;

let mut accounts = balances(deps.storage);
accounts.update(&sender_raw, |balance: Option<Uint128>| {
balance.unwrap_or_default() - send
accounts.update(&sender_raw, |balance: Option<Uint128>| -> StdResult<_> {
Ok(balance.unwrap_or_default().checked_sub(send)?)
})?;
accounts.update(&rcpt_raw, |balance: Option<Uint128>| -> StdResult<_> {
Ok(balance.unwrap_or_default() + send)
Ok(balance.unwrap_or_default().checked_add(send)?)
})?;

let res = Response {
Expand Down Expand Up @@ -123,7 +123,7 @@ fn get_bonded(querier: &QuerierWrapper, contract: &HumanAddr) -> StdResult<Uint1
denom, &d.amount.denom
)))
} else {
Ok(acc + d.amount.amount)
Ok(acc.checked_add(d.amount.amount)?)
}
})
}
Expand Down Expand Up @@ -164,13 +164,13 @@ pub fn bond(deps: DepsMut, env: Env, info: MessageInfo) -> StdResult<Response> {
} else {
payment.amount.multiply_ratio(supply.issued, bonded)
};
supply.bonded = bonded + payment.amount;
supply.issued += to_mint;
supply.bonded = bonded.checked_add(payment.amount)?;
supply.issued = supply.issued.checked_add(to_mint)?;
totals.save(&supply)?;

// update the balance of the sender
balances(deps.storage).update(&sender_raw, |balance| -> StdResult<_> {
Ok(balance.unwrap_or_default() + to_mint)
Ok(balance.unwrap_or_default().checked_add(to_mint)?)
})?;

// bond them to the validator
Expand Down Expand Up @@ -209,12 +209,12 @@ pub fn unbond(deps: DepsMut, env: Env, info: MessageInfo, amount: Uint128) -> St
// deduct all from the account
let mut accounts = balances(deps.storage);
accounts.update(&sender_raw, |balance| -> StdResult<_> {
balance.unwrap_or_default() - amount
Ok(balance.unwrap_or_default().checked_sub(amount)?)
})?;
if tax > Uint128(0) {
// add tax to the owner
accounts.update(&invest.owner, |balance: Option<Uint128>| -> StdResult<_> {
Ok(balance.unwrap_or_default() + tax)
Ok(balance.unwrap_or_default().checked_add(tax)?)
})?;
}

Expand All @@ -231,12 +231,12 @@ pub fn unbond(deps: DepsMut, env: Env, info: MessageInfo, amount: Uint128) -> St
let unbond = remainder.multiply_ratio(bonded, supply.issued);
supply.bonded = (bonded - unbond)?;
supply.issued = (supply.issued - remainder)?;
supply.claims += unbond;
supply.claims = supply.claims.checked_add(unbond)?;
totals.save(&supply)?;

// add a claim to this user to get their tokens after the unbonding period
claims(deps.storage).update(&sender_raw, |claim| -> StdResult<_> {
Ok(claim.unwrap_or_default() + unbond)
Ok(claim.unwrap_or_default().checked_add(unbond)?)
})?;

// unbond them
Expand Down Expand Up @@ -273,10 +273,10 @@ pub fn claim(deps: DepsMut, env: Env, info: MessageInfo) -> StdResult<Response>
// check how much to send - min(balance, claims[sender]), and reduce the claim
let sender_raw = deps.api.canonical_address(&info.sender)?;
let mut to_send = balance.amount;
claims(deps.storage).update(sender_raw.as_slice(), |claim| {
claims(deps.storage).update(sender_raw.as_slice(), |claim| -> StdResult<_> {
let claim = claim.ok_or_else(|| StdError::generic_err("no claim for this address"))?;
to_send = to_send.min(claim);
claim - to_send
Ok(claim.checked_sub(to_send)?)
})?;

// update total supply (lower claim)
Expand Down Expand Up @@ -356,12 +356,12 @@ pub fn _bond_all_tokens(
balance.amount = (balance.amount - supply.claims)?;
// this just triggers the "no op" case if we don't have min_withdrawal left to reinvest
(balance.amount - invest.min_withdrawal)?;
supply.bonded += balance.amount;
supply.bonded = supply.bonded.checked_add(balance.amount)?;
Ok(supply)
}) {
Ok(_) => {}
// if it is below the minimum, we do a no-op (do not revert other state from withdrawal)
Err(StdError::Underflow { .. }) => return Ok(Response::default()),
Err(StdError::Overflow(_)) => return Ok(Response::default()),
Err(e) => return Err(e.into()),
}

Expand Down Expand Up @@ -739,7 +739,7 @@ mod tests {
let res = execute(deps.as_mut(), mock_env(), info, unbond_msg);
match res.unwrap_err() {
StakingError::Std {
original: StdError::Underflow { .. },
original: StdError::Overflow(_),
} => {}
err => panic!("Unexpected error: {:?}", err),
}
Expand Down Expand Up @@ -778,7 +778,7 @@ mod tests {
let ratio = Decimal::from_str("1.5").unwrap();

let invest = query_investment(deps.as_ref()).unwrap();
assert_eq!(invest.token_supply, bobs_balance + owner_cut);
assert_eq!(Ok(invest.token_supply), bobs_balance + owner_cut);
assert_eq!(invest.staked_tokens, coin(690, "ustake")); // 1500 - 810
assert_eq!(invest.nominal_value, ratio);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub use crate::addresses::{CanonicalAddr, HumanAddr};
pub use crate::binary::{Binary, ByteArray};
pub use crate::coins::{coin, coins, has_coins, Coin};
pub use crate::deps::{Deps, DepsMut, OwnedDeps};
pub use crate::errors::{RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError};
pub use crate::errors::{
OverflowError, OverflowOperation, RecoverPubkeyError, StdError, StdResult, SystemError,
VerificationError,
};
#[cfg(feature = "stargate")]
pub use crate::ibc::{
ChannelResponse, IbcAcknowledgement, IbcBasicResponse, IbcChannel, IbcEndpoint, IbcMsg,
Expand Down
12 changes: 9 additions & 3 deletions packages/storage/src/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod tests {
use cosmwasm_std::testing::MockStorage;
use serde::{Deserialize, Serialize};

use cosmwasm_std::StdError;
use cosmwasm_std::{OverflowError, OverflowOperation, StdError};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Config {
Expand Down Expand Up @@ -251,9 +251,15 @@ mod tests {
};
writer.save(&cfg).unwrap();

let output = writer.update(&|_c| Err(StdError::underflow(4, 7)));
let output = writer.update(&|_c| {
Err(StdError::from(OverflowError::new(
OverflowOperation::Sub,
4,
7,
)))
});
match output.unwrap_err() {
StdError::Underflow { .. } => {}
StdError::Overflow(_) => {}
err => panic!("Unexpected error: {:?}", err),
}
assert_eq!(writer.load().unwrap(), cfg);
Expand Down

0 comments on commit 2d88c6b

Please sign in to comment.