diff --git a/contracts/alliance-hub/src/contract.rs b/contracts/alliance-hub/src/contract.rs index 0802896..a33b7bc 100644 --- a/contracts/alliance-hub/src/contract.rs +++ b/contracts/alliance-hub/src/contract.rs @@ -33,7 +33,7 @@ use alliance_protocol::alliance_protocol::{ use crate::error::ContractError; use crate::migrations::migrate_state; use crate::state::{ - ASSET_CONFIG, ASSET_REWARD_DISTRIBUTION, ASSET_REWARD_RATE, BALANCES, CONFIG, TEMP_BALANCE, + ASSET_CONFIG, ASSET_REWARD_DISTRIBUTION, ASSET_REWARD_RATE, CONFIG, SHARES, TEMP_BALANCE, TOTAL_BALANCES_SHARES, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE, VALIDATORS, WHITELIST, }; use crate::token_factory::{CustomExecuteMsg, DenomUnit, Metadata, TokenExecuteMsg}; @@ -332,7 +332,7 @@ fn stake( let (_, asset_available) = _take(&mut deps, &env, &asset, balance, true)?; let share_amount = compute_share_amount(shares, amount, asset_available); - BALANCES.update( + SHARES.update( deps.storage, (recipient.clone(), &asset), |balance| -> Result<_, ContractError> { @@ -398,7 +398,7 @@ fn unstake( let mut withdraw_amount = asset.amount; let mut share_amount = compute_share_amount(shares, withdraw_amount, asset_available); - let current_user_share = BALANCES + let current_user_share = SHARES .may_load(deps.storage, (sender.clone(), &asset.info))? .unwrap_or_default(); @@ -412,7 +412,7 @@ fn unstake( withdraw_amount = compute_balance_amount(shares, share_amount, asset_available) } - BALANCES.save( + SHARES.save( deps.storage, (sender, &asset.info), &(current_user_share.checked_sub(share_amount)?), @@ -485,7 +485,7 @@ fn _claim_reward( let asset_reward_rate = ASSET_REWARD_RATE.load(storage, &asset_info)?; if let Ok(user_reward_rate) = user_reward_rate { - let user_staked = BALANCES.load(storage, (user.clone(), &asset_info))?; + let user_staked = SHARES.load(storage, (user.clone(), &asset_info))?; let rewards = ((asset_reward_rate - user_reward_rate) * Decimal::from_atomics(user_staked, 0)?) .to_uint_floor(); diff --git a/contracts/alliance-hub/src/migrations.rs b/contracts/alliance-hub/src/migrations.rs index 4f1a6a8..8ccbfee 100644 --- a/contracts/alliance-hub/src/migrations.rs +++ b/contracts/alliance-hub/src/migrations.rs @@ -1,9 +1,9 @@ -use crate::error::ContractError; -use cosmwasm_std::{DepsMut, Order, Uint128}; +use cosmwasm_std::{Addr, DepsMut, Order, Uint128}; use cw_asset::AssetInfo; use cw_storage_plus::Map; -use crate::state::TOTAL_BALANCES_SHARES; +use crate::error::ContractError; +use crate::state::{SHARES, TOTAL_BALANCES_SHARES}; pub(crate) fn migrate_state(deps: DepsMut) -> Result<(), ContractError> { const OLD_TOTAL_BALANCES: Map<&AssetInfo, Uint128> = Map::new("total_balances"); @@ -22,5 +22,21 @@ pub(crate) fn migrate_state(deps: DepsMut) -> Result<(), ContractError> { TOTAL_BALANCES_SHARES.save(deps.storage, &key, &(value, value))?; } + const OLD_BALANCES: Map<(Addr, &AssetInfo), Uint128> = Map::new("balances"); + + let old_map = OLD_BALANCES + .range(deps.storage, None, None, Order::Ascending) + .map(|item| { + let (key, value) = item.unwrap(); + (key, value) + }) + .collect::>(); + + OLD_BALANCES.clear(deps.storage); + + for ((addr, asset_info), value) in old_map { + SHARES.save(deps.storage, (addr, &asset_info), &value)?; + } + Ok(()) } diff --git a/contracts/alliance-hub/src/query.rs b/contracts/alliance-hub/src/query.rs index d5914b4..1da7e1f 100644 --- a/contracts/alliance-hub/src/query.rs +++ b/contracts/alliance-hub/src/query.rs @@ -10,7 +10,7 @@ use cw_asset::AssetInfo; use std::collections::HashMap; use crate::state::{ - ASSET_CONFIG, ASSET_REWARD_DISTRIBUTION, ASSET_REWARD_RATE, BALANCES, CONFIG, + ASSET_CONFIG, ASSET_REWARD_DISTRIBUTION, ASSET_REWARD_RATE, CONFIG, SHARES, TOTAL_BALANCES_SHARES, UNCLAIMED_REWARDS, USER_ASSET_REWARD_RATE, VALIDATORS, WHITELIST, }; @@ -63,7 +63,7 @@ fn get_rewards_distribution(deps: Deps) -> StdResult { fn get_staked_balance(deps: Deps, asset_query: AssetQuery) -> StdResult { let addr = deps.api.addr_validate(&asset_query.address)?; let key = (addr, &asset_query.asset); - let user_shares = BALANCES.load(deps.storage, key)?; + let user_shares = SHARES.load(deps.storage, key)?; let (balance, shares) = TOTAL_BALANCES_SHARES.load(deps.storage, &asset_query.asset)?; let config = ASSET_CONFIG .may_load(deps.storage, &asset_query.asset)? @@ -83,7 +83,7 @@ fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult let key = (addr, &asset_query.asset.clone()); let user_reward_rate = USER_ASSET_REWARD_RATE.load(deps.storage, key.clone())?; let asset_reward_rate = ASSET_REWARD_RATE.load(deps.storage, &asset_query.asset.clone())?; - let user_balance = BALANCES.load(deps.storage, key.clone())?; + let user_balance = SHARES.load(deps.storage, key.clone())?; let unclaimed_rewards = UNCLAIMED_REWARDS .load(deps.storage, key) .unwrap_or(Uint128::zero()); @@ -105,7 +105,7 @@ fn get_all_staked_balances(deps: Deps, asset_query: AllStakedBalancesQuery) -> S // Build the required key to recover the BALANCES let (asset_info, _) = asset_res?; let stake_key = (addr.clone(), &asset_info); - let user_shares = BALANCES + let user_shares = SHARES .load(deps.storage, stake_key) .unwrap_or(Uint128::zero()); let (balance, shares) = TOTAL_BALANCES_SHARES.load(deps.storage, &asset_info)?; @@ -136,7 +136,7 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu .map(|item| { let (asset_info, user_reward_rate) = item?; let asset_reward_rate = ASSET_REWARD_RATE.load(deps.storage, &asset_info)?; - let user_balance = BALANCES.load(deps.storage, (addr.clone(), &asset_info))?; + let user_balance = SHARES.load(deps.storage, (addr.clone(), &asset_info))?; let unclaimed_rewards = UNCLAIMED_REWARDS .load(deps.storage, (addr.clone(), &asset_info)) .unwrap_or(Uint128::zero()); diff --git a/contracts/alliance-hub/src/state.rs b/contracts/alliance-hub/src/state.rs index 6b1086f..85924b5 100644 --- a/contracts/alliance-hub/src/state.rs +++ b/contracts/alliance-hub/src/state.rs @@ -7,20 +7,15 @@ use ve3_shared::msgs_asset_staking::AssetConfigRuntime; pub const CONFIG: Item = Item::new("config"); pub const WHITELIST: Map<&AssetInfo, ChainId> = Map::new("whitelist"); -//todo to rename to SHARES -pub const BALANCES: Map<(Addr, &AssetInfo), Uint128> = Map::new("balances"); +pub const SHARES: Map<(Addr, &AssetInfo), Uint128> = Map::new("shares"); pub const TOTAL_BALANCES_SHARES: Map<&AssetInfo, (Uint128, Uint128)> = Map::new("total_balances_shares"); - pub const VALIDATORS: Item> = Item::new("validators"); - pub const ASSET_CONFIG: Map<&AssetInfo, AssetConfigRuntime> = Map::new("asset_config"); - pub const ASSET_REWARD_DISTRIBUTION: Item> = Item::new("asset_reward_distribution"); pub const ASSET_REWARD_RATE: Map<&AssetInfo, Decimal> = Map::new("asset_reward_rate"); pub const USER_ASSET_REWARD_RATE: Map<(Addr, &AssetInfo), Decimal> = Map::new("user_asset_reward_rate"); pub const UNCLAIMED_REWARDS: Map<(Addr, &AssetInfo), Uint128> = Map::new("unclaimed_rewards"); - pub const TEMP_BALANCE: Item = Item::new("temp_balance"); diff --git a/contracts/alliance-hub/src/tests/stake_unstake.rs b/contracts/alliance-hub/src/tests/stake_unstake.rs index b777db9..55df272 100644 --- a/contracts/alliance-hub/src/tests/stake_unstake.rs +++ b/contracts/alliance-hub/src/tests/stake_unstake.rs @@ -1,6 +1,6 @@ use crate::contract::execute; use crate::error::ContractError; -use crate::state::{BALANCES, TOTAL_BALANCES_SHARES}; +use crate::state::{SHARES, TOTAL_BALANCES_SHARES}; use crate::tests::helpers::{ query_all_staked_balances, setup_contract, stake, stake_cw20, unstake, unstake_cw20, whitelist_assets, @@ -44,7 +44,7 @@ mod cw20_support { ]) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -67,7 +67,7 @@ mod cw20_support { ("share", "100"), ]) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -154,7 +154,7 @@ mod cw20_support { })) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -186,7 +186,7 @@ mod cw20_support { })) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -234,7 +234,7 @@ fn test_stake() { ]) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -257,7 +257,7 @@ fn test_stake() { ("share", "100"), ]) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -367,7 +367,7 @@ fn test_unstake() { })) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, ( @@ -394,7 +394,7 @@ fn test_unstake() { })) ); - let balance = BALANCES + let balance = SHARES .load( deps.as_ref().storage, (