Skip to content

Commit

Permalink
fix: query rewards without staking
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Feb 8, 2024
1 parent dbbc0be commit 82d7cf5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/alliance-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ fn get_pending_rewards(deps: Deps, asset_query: AssetQuery) -> StdResult<Binary>
let config = CONFIG.load(deps.storage)?;
let addr = deps.api.addr_validate(&asset_query.address)?;
let key = (addr, AssetInfoKey::from(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, 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 unclaimed_rewards = UNCLAIMED_REWARDS
.load(deps.storage, key)
Expand Down
80 changes: 80 additions & 0 deletions contracts/alliance-hub/src/tests/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,86 @@ fn claim_user_rewards() {
);
}

#[test]
fn claim_user_rewards_without_stake() {
let mut deps = mock_dependencies_with_balance(&[coin(2000000, "uluna")]);
setup_contract(deps.as_mut());
set_alliance_asset(deps.as_mut());
whitelist_assets(
deps.as_mut(),
HashMap::from([(
"chain-1".to_string(),
vec![AssetInfo::Native("aWHALE".to_string())],
)]),
);
stake(deps.as_mut(), "user1", 1000000, "aWHALE");

ASSET_REWARD_DISTRIBUTION
.save(
deps.as_mut().storage,
&vec![
AssetDistribution {
asset: AssetInfo::Native("aWHALE".to_string()),
distribution: Decimal::percent(50),
},
AssetDistribution {
asset: AssetInfo::Native("bWHALE".to_string()),
distribution: Decimal::percent(50),
},
],
)
.unwrap();
TEMP_BALANCE
.save(deps.as_mut().storage, &Uint128::new(1000000))
.unwrap();
execute(
deps.as_mut(),
mock_env(),
mock_info("cosmos2contract", &[]),
ExecuteMsg::UpdateRewardsCallback {},
)
.unwrap();

let res = claim_rewards(deps.as_mut(), "user2", "aWHALE");
assert_eq!(
res,
Response::new()
.add_attributes(vec![
("action", "claim_rewards"),
("user", "user2"),
("asset", "native:aWHALE"),
("reward_amount", "0"),
])
);

USER_ASSET_REWARD_RATE
.load(
deps.as_ref().storage,
(
Addr::unchecked("user2"),
AssetInfoKey::from(AssetInfo::Native("aWHALE".to_string())),
),
)
.unwrap_err();

let rewards = query_rewards(deps.as_ref(), "user2", "aWHALE");
assert_eq!(
rewards,
PendingRewardsRes {
rewards: Uint128::new(0),
reward_asset: AssetInfo::Native("uluna".to_string()),
staked_asset: AssetInfo::Native("aWHALE".to_string()),
}
);

let all_rewards = query_all_rewards(deps.as_ref(), "user2");
assert_eq!(
all_rewards,
vec![]
);
}


#[test]
fn claim_user_rewards_after_staking() {
let mut deps = mock_dependencies_with_balance(&[coin(2000000, "uluna")]);
Expand Down

0 comments on commit 82d7cf5

Please sign in to comment.