Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
Withdraw claimed rewards to user's address. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
piobab authored Oct 5, 2023
1 parent 7c9d2f2 commit 1176c73
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
50 changes: 15 additions & 35 deletions contracts/credit-manager/src/claim_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ use cosmwasm_std::{
};
use mars_rover::{
error::{ContractError, ContractResult},
msg::{
execute::{CallbackMsg, ChangeExpected},
ExecuteMsg,
},
msg::{execute::CallbackMsg, ExecuteMsg},
traits::Denoms,
};
use mars_rover_health_types::AccountKind;

use crate::{
state::INCENTIVES,
update_coin_balances::query_balance,
utils::{get_account_kind, update_balances_msgs},
};
use crate::{state::INCENTIVES, update_coin_balances::query_balance};

pub fn claim_rewards(
deps: DepsMut,
Expand All @@ -31,34 +23,22 @@ pub fn claim_rewards(
return Err(ContractError::NoAmount);
}

// For HLS accounts there are special requirements enforced for this account type.
// assert_hls_rules only allows assets with HLS params set in the params contract
// and where the collateral is whitelisted.
// We withdraw all claimed rewards for HLS accounts to the recipient address.
let kind = get_account_kind(deps.storage, account_id)?;
let msgs = match kind {
AccountKind::Default => update_balances_msgs(
&deps.querier,
&env.contract.address,
account_id,
unclaimed_rewards.to_denoms(),
ChangeExpected::Increase,
)?,
AccountKind::HighLeveredStrategy => {
let msg = send_rewards_msg(
&deps.querier,
&env.contract.address,
account_id,
recipient.clone(),
unclaimed_rewards.to_denoms(),
)?;
vec![msg]
}
};
// Incentive denom may not be listed in params contract.
// When rewards are claimed to the account, they are considered deposits (collateral).
// If the account requires HF check, health contract won't be able to find
// incentive denom params (such as MaxLTV) for HF calculation and the TX will be rejected.
// To address this issue we withdraw all claimed rewards to the recipient address.
let msg = send_rewards_msg(
&deps.querier,
&env.contract.address,
account_id,
recipient.clone(),
unclaimed_rewards.to_denoms(),
)?;

Ok(Response::new()
.add_message(incentives.claim_rewards_msg(account_id)?)
.add_messages(msgs)
.add_message(msg)
.add_attribute("action", "claim_rewards")
.add_attribute("account_id", account_id)
.add_attribute("recipient", recipient.to_string()))
Expand Down
35 changes: 18 additions & 17 deletions contracts/credit-manager/tests/test_claim_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ fn claiming_a_single_reward() {

// Check account id deposit balance
let positions = mock.query_positions(&account_id);
assert_eq!(positions.deposits.len(), 1);
assert_eq!(positions.deposits.first().unwrap().amount, Uint128::new(123));
assert_eq!(positions.deposits.first().unwrap().denom, coin_info.denom);
assert_eq!(positions.deposits.len(), 0);

// Ensure money is in bank module for credit manager
let cm_balance = mock.query_balance(&mock.rover, &coin_info.denom);
assert_eq!(cm_balance.amount, Uint128::new(123));
// Ensure money is in user's wallet
let balance = mock.query_balance(&mock.rover, &coin_info.denom);
assert_eq!(balance.amount, Uint128::zero());

let balance = mock.query_balance(&user, &coin_info.denom);
assert_eq!(balance.amount, Uint128::new(123));
}

#[test]
Expand Down Expand Up @@ -79,25 +80,25 @@ fn claiming_multiple_rewards() {

// Check account id deposit balance
let positions = mock.query_positions(&account_id);
assert_eq!(positions.deposits.len(), 3);
assert_eq!(positions.deposits.len(), 0);

let osmo_claimed = get_coin(&osmo_info.denom, &positions.deposits);
assert_eq!(osmo_claimed.amount, Uint128::new(123));
// Ensure money is in user's wallet
let osmo_balance = mock.query_balance(&mock.rover, &osmo_info.denom);
assert_eq!(osmo_balance.amount, Uint128::zero());

let atom_claimed = get_coin(&atom_info.denom, &positions.deposits);
assert_eq!(atom_claimed.amount, Uint128::new(555));
let atom_balance = mock.query_balance(&mock.rover, &atom_info.denom);
assert_eq!(atom_balance.amount, Uint128::zero());

let jake_claimed = get_coin(&jake_info.denom, &positions.deposits);
assert_eq!(jake_claimed.amount, Uint128::new(12));
let jake_balance = mock.query_balance(&mock.rover, &jake_info.denom);
assert_eq!(jake_balance.amount, Uint128::zero());

// Ensure money is in bank module for credit manager
let osmo_balance = mock.query_balance(&mock.rover, &osmo_info.denom);
let osmo_balance = mock.query_balance(&user, &osmo_info.denom);
assert_eq!(osmo_balance.amount, Uint128::new(123));

let atom_balance = mock.query_balance(&mock.rover, &atom_info.denom);
let atom_balance = mock.query_balance(&user, &atom_info.denom);
assert_eq!(atom_balance.amount, Uint128::new(555));

let jake_balance = mock.query_balance(&mock.rover, &jake_info.denom);
let jake_balance = mock.query_balance(&user, &jake_info.denom);
assert_eq!(jake_balance.amount, Uint128::new(12));
}

Expand Down

0 comments on commit 1176c73

Please sign in to comment.