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 03950f1
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 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

0 comments on commit 03950f1

Please sign in to comment.