Skip to content

Commit

Permalink
wip: claim rewards refact
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Feb 9, 2024
1 parent d87c8fb commit 103203c
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 483 deletions.
10 changes: 5 additions & 5 deletions contracts/alliance-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult<Binary>
let asset_reward_rate =
ASSET_REWARD_RATE.load(deps.storage, AssetInfoKey::from(asset_query.asset.clone()))?;
let user_reward_rate = USER_ASSET_REWARD_RATE.load(deps.storage, key.clone()).unwrap_or(asset_reward_rate);
let user_balance = BALANCES.load(deps.storage, key.clone()).unwrap_or(Uint128::zero());
let user_balance = BALANCES.load(deps.storage, key.clone()).unwrap_or_default();
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(deps.storage, key)
.unwrap_or(Uint128::zero());
.unwrap_or_default();
let pending_rewards = (asset_reward_rate - user_reward_rate) * user_balance;

to_json_binary(&PendingRewardsRes {
Expand All @@ -105,7 +105,7 @@ fn get_all_staked_balances(deps: Deps, asset_query: AllStakedBalancesQuery) -> S
let stake_key = (addr.clone(), asset_info_key);
let balance = BALANCES
.load(deps.storage, stake_key)
.unwrap_or(Uint128::zero());
.unwrap_or_default();

// Append the request
res.push(StakedBalanceRes {
Expand All @@ -131,13 +131,13 @@ fn get_all_pending_rewards(deps: Deps, query: AllPendingRewardsQuery) -> StdResu
let user_balance = BALANCES.load(
deps.storage,
(addr.clone(), AssetInfoKey::from(asset.clone())),
).unwrap_or(Uint128::zero());
).unwrap_or_default();
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(
deps.storage,
(addr.clone(), AssetInfoKey::from(asset.clone())),
)
.unwrap_or(Uint128::zero());
.unwrap_or_default();
let pending_rewards = (asset_reward_rate - user_reward_rate) * user_balance;
Ok(PendingRewardsRes {
rewards: pending_rewards + unclaimed_rewards,
Expand Down
2 changes: 1 addition & 1 deletion contracts/alliance-hub/src/tests/stake_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn test_unstake_invalid() {
let info = mock_info("user2", &[]);
let msg = ExecuteMsg::Unstake(Asset::native("asset1", 100u128));
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::InsufficientBalance {});
assert_eq!(err, ContractError::AssetNotStaked {});

// User unstakes more than they have
let info = mock_info("user1", &[]);
Expand Down
Loading

0 comments on commit 103203c

Please sign in to comment.