Skip to content

Commit

Permalink
Merge pull request #34 from terra-money/fix/error-claiming-rewards
Browse files Browse the repository at this point in the history
fix: issue with querying rewards for asset not staked
  • Loading branch information
emidev98 authored Feb 8, 2024
2 parents ec6898d + 82d7cf5 commit caf5127
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
10 changes: 9 additions & 1 deletion contracts/alliance-hub/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn unstake(deps: DepsMut, info: MessageInfo, asset: Asset) -> Result<Response, C
}
Ok(balance - asset.amount)
}
None => Err(ContractError::InsufficientBalance {}),
None => Err(ContractError::AssetNotStaked {}),
}
},
)?;
Expand Down Expand Up @@ -308,6 +308,14 @@ fn _claim_reward(
asset: AssetInfo,
) -> Result<Uint128, ContractError> {
let asset_key = AssetInfoKey::from(&asset);
// If the user did not stake the asset, do nothing and return 0
if BALANCES
.load(storage, (user.clone(), asset_key.clone()))
.is_err()
{
return Ok(Uint128::zero());
}

let user_reward_rate = USER_ASSET_REWARD_RATE.load(storage, (user.clone(), asset_key.clone()));
let asset_reward_rate = ASSET_REWARD_RATE.load(storage, asset_key.clone())?;

Expand Down
6 changes: 3 additions & 3 deletions contracts/alliance-hub/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ 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_balance = BALANCES.load(deps.storage, key.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)
.unwrap_or(Uint128::zero());
Expand Down Expand Up @@ -131,7 +131,7 @@ 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());
let unclaimed_rewards = UNCLAIMED_REWARDS
.load(
deps.storage,
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
3 changes: 3 additions & 0 deletions packages/alliance-protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ pub enum ContractError {

#[error("Invalid total distribution: {0}")]
InvalidTotalDistribution(Decimal),

#[error("Asset not staked")]
AssetNotStaked {},
}

0 comments on commit caf5127

Please sign in to comment.