diff --git a/contracts/staking/src/contract.rs b/contracts/staking/src/contract.rs index ed92229278..02e9c71793 100644 --- a/contracts/staking/src/contract.rs +++ b/contracts/staking/src/contract.rs @@ -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| { - balance.unwrap_or_default() - send + accounts.update(&sender_raw, |balance: Option| -> StdResult<_> { + Ok(balance.unwrap_or_default().checked_sub(send)?) })?; accounts.update(&rcpt_raw, |balance: Option| -> StdResult<_> { - Ok(balance.unwrap_or_default() + send) + Ok(balance.unwrap_or_default().checked_add(send)?) })?; let res = Response { @@ -123,7 +123,7 @@ fn get_bonded(querier: &QuerierWrapper, contract: &HumanAddr) -> StdResult StdResult { } 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 @@ -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| -> StdResult<_> { - Ok(balance.unwrap_or_default() + tax) + Ok(balance.unwrap_or_default().checked_add(tax)?) })?; } @@ -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 @@ -273,10 +273,10 @@ pub fn claim(deps: DepsMut, env: Env, info: MessageInfo) -> StdResult // 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) @@ -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()), }