Skip to content

Commit

Permalink
Update staking contract with new Buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 3, 2020
1 parent 55a35a9 commit a202e4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
48 changes: 27 additions & 21 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::msg::{
TokenInfoResponse,
};
use crate::state::{
balances, balances_read, claims, claims_read, invest_info, invest_info_read, token_info,
token_info_read, total_supply, total_supply_read, InvestmentInfo, Supply,
balances, claims, invest_info, invest_info_read, token_info, token_info_read, total_supply,
total_supply_read, InvestmentInfo, Supply,
};

const FALLBACK_RATIO: Decimal = Decimal::one();
Expand Down Expand Up @@ -77,13 +77,17 @@ pub fn transfer<S: Storage, A: Api, Q: Querier>(
let rcpt_raw = deps.api.canonical_address(&recipient)?;
let sender_raw = deps.api.canonical_address(&env.message.sender)?;

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

let res = HandleResponse {
messages: vec![],
Expand Down Expand Up @@ -164,7 +168,7 @@ pub fn bond<S: Storage, A: Api, Q: Querier>(
totals.save(&supply)?;

// update the balance of the sender
balances(&mut deps.storage).update(sender_raw.as_slice(), |balance| {
balances().update(&mut deps.storage, sender_raw.as_slice(), |balance| {
Ok(balance.unwrap_or_default() + to_mint)
})?;

Expand Down Expand Up @@ -205,15 +209,17 @@ pub fn unbond<S: Storage, A: Api, Q: Querier>(
let tax = amount * invest.exit_tax;

// deduct all from the account
let mut accounts = balances(&mut deps.storage);
accounts.update(sender_raw.as_slice(), |balance| {
let accounts = balances();
accounts.update(&mut deps.storage, sender_raw.as_slice(), |balance| {
balance.unwrap_or_default() - amount
})?;
if tax > Uint128(0) {
// add tax to the owner
accounts.update(invest.owner.as_slice(), |balance: Option<Uint128>| {
Ok(balance.unwrap_or_default() + tax)
})?;
accounts.update(
&mut deps.storage,
invest.owner.as_slice(),
|balance: Option<Uint128>| Ok(balance.unwrap_or_default() + tax),
)?;
}

// re-calculate bonded to ensure we have real values
Expand All @@ -233,7 +239,7 @@ pub fn unbond<S: Storage, A: Api, Q: Querier>(
totals.save(&supply)?;

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

Expand Down Expand Up @@ -273,7 +279,7 @@ pub fn claim<S: Storage, A: Api, Q: Querier>(
// check how much to send - min(balance, claims[sender]), and reduce the claim
let sender_raw = deps.api.canonical_address(&env.message.sender)?;
let mut to_send = balance.amount;
claims(&mut deps.storage).update(sender_raw.as_slice(), |claim| {
claims().update(&mut deps.storage, sender_raw.as_slice(), |claim| {
let claim = claim.ok_or_else(|| StdError::generic_err("no claim for this address"))?;
to_send = to_send.min(claim);
claim - to_send
Expand Down Expand Up @@ -402,8 +408,8 @@ pub fn query_balance<S: Storage, A: Api, Q: Querier>(
address: HumanAddr,
) -> StdResult<BalanceResponse> {
let address_raw = deps.api.canonical_address(&address)?;
let balance = balances_read(&deps.storage)
.may_load(address_raw.as_slice())?
let balance = balances()
.may_load(&deps.storage, address_raw.as_slice())?
.unwrap_or_default();
Ok(BalanceResponse { balance })
}
Expand All @@ -413,8 +419,8 @@ pub fn query_claims<S: Storage, A: Api, Q: Querier>(
address: HumanAddr,
) -> StdResult<ClaimsResponse> {
let address_raw = deps.api.canonical_address(&address)?;
let claims = claims_read(&deps.storage)
.may_load(address_raw.as_slice())?
let claims = claims()
.may_load(&deps.storage, address_raw.as_slice())?
.unwrap_or_default();
Ok(ClaimsResponse { claims })
}
Expand Down
21 changes: 5 additions & 16 deletions contracts/staking/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{CanonicalAddr, Decimal, HumanAddr, ReadonlyStorage, Storage, Uint128};
use cosmwasm_storage::{
bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, ReadonlySingleton,
Singleton,
};
use cosmwasm_storage::{bucket, singleton, singleton_read, Bucket, ReadonlySingleton, Singleton};

use crate::msg::TokenInfoResponse;

Expand All @@ -17,21 +14,13 @@ pub const PREFIX_BALANCE: &[u8] = b"balance";
pub const PREFIX_CLAIMS: &[u8] = b"claim";

/// balances are state of the erc20 tokens
pub fn balances<S: Storage>(storage: &mut S) -> Bucket<S, Uint128> {
bucket(storage, PREFIX_BALANCE)
}

pub fn balances_read<S: ReadonlyStorage>(storage: &S) -> ReadonlyBucket<S, Uint128> {
bucket_read(storage, PREFIX_BALANCE)
pub fn balances() -> Bucket<Uint128> {
bucket(PREFIX_BALANCE)
}

/// claims are the claims to money being unbonded
pub fn claims<S: Storage>(storage: &mut S) -> Bucket<S, Uint128> {
bucket(storage, PREFIX_CLAIMS)
}

pub fn claims_read<S: ReadonlyStorage>(storage: &S) -> ReadonlyBucket<S, Uint128> {
bucket_read(storage, PREFIX_CLAIMS)
pub fn claims() -> Bucket<Uint128> {
bucket(PREFIX_CLAIMS)
}

/// Investment info is fixed at initialization, and is used to control the function of the contract
Expand Down

0 comments on commit a202e4d

Please sign in to comment.