Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 32 additions & 5 deletions contracts/incentives/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn execute(
),
ExecuteMsg::BalanceChange {
user_addr,
account_id,
denom,
user_amount_scaled_before,
total_amount_scaled_before,
Expand All @@ -112,18 +113,21 @@ pub fn execute(
env,
info,
user_addr,
account_id,
denom,
user_amount_scaled_before,
total_amount_scaled_before,
),
ExecuteMsg::ClaimRewards {
account_id,
start_after_collateral_denom,
start_after_incentive_denom,
limit,
} => execute_claim_rewards(
deps,
env,
info,
account_id,
start_after_collateral_denom,
start_after_incentive_denom,
limit,
Expand Down Expand Up @@ -339,6 +343,7 @@ pub fn execute_balance_change(
env: Env,
info: MessageInfo,
user_addr: Addr,
account_id: Option<String>,
collateral_denom: String,
user_amount_scaled_before: Uint128,
total_amount_scaled_before: Uint128,
Expand All @@ -349,10 +354,17 @@ pub fn execute_balance_change(
return Err(MarsError::Unauthorized {}.into());
}

let acc_id = account_id.clone().unwrap_or("".to_string());

let base_event = Event::new("mars/incentives/balance_change")
.add_attribute("action", "balance_change")
.add_attribute("denom", collateral_denom.clone())
.add_attribute("user", user_addr.to_string());
let base_event = if account_id.is_some() {
base_event.add_attribute("account_id", &acc_id)
} else {
base_event
};
let mut events = vec![base_event];

let incentive_states = INCENTIVE_STATES
Expand All @@ -371,7 +383,7 @@ pub fn execute_balance_change(

// Check if user has accumulated uncomputed rewards (which means index is not up to date)
let user_asset_index_key =
USER_ASSET_INDICES.key((&user_addr, &collateral_denom, &incentive_denom));
USER_ASSET_INDICES.key(((&user_addr, &acc_id), &collateral_denom, &incentive_denom));

let user_asset_index =
user_asset_index_key.may_load(deps.storage)?.unwrap_or_else(Decimal::zero);
Expand All @@ -391,6 +403,7 @@ pub fn execute_balance_change(
state::increase_unclaimed_rewards(
deps.storage,
&user_addr,
&acc_id,
&collateral_denom,
&incentive_denom,
accrued_rewards,
Expand All @@ -415,17 +428,25 @@ pub fn execute_claim_rewards(
mut deps: DepsMut,
env: Env,
info: MessageInfo,
account_id: Option<String>,
start_after_collateral_denom: Option<String>,
start_after_incentive_denom: Option<String>,
limit: Option<u32>,
) -> Result<Response, ContractError> {
let red_bank_addr = query_red_bank_address(deps.as_ref())?;
let user_addr = info.sender;
let acc_id = account_id.clone().unwrap_or("".to_string());

let red_bank_addr = query_red_bank_address(deps.as_ref())?;

let mut response = Response::new();
let base_event = Event::new("mars/incentives/claim_rewards")
.add_attribute("action", "claim_rewards")
.add_attribute("user", user_addr.to_string());
let base_event = if account_id.is_some() {
base_event.add_attribute("account_id", &acc_id)
} else {
base_event
};
let mut events = vec![base_event];

let asset_incentives = state::paginate_incentive_states(
Expand All @@ -445,14 +466,15 @@ pub fn execute_claim_rewards(
&env.block,
&red_bank_addr,
&user_addr,
&account_id,
&collateral_denom,
&incentive_denom,
)?;

// clear unclaimed rewards
USER_UNCLAIMED_REWARDS.save(
deps.storage,
(&user_addr, &collateral_denom, &incentive_denom),
((&user_addr, &acc_id), &collateral_denom, &incentive_denom),
&Uint128::zero(),
)?;

Expand Down Expand Up @@ -532,13 +554,15 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
)?),
QueryMsg::UserUnclaimedRewards {
user,
account_id,
start_after_collateral_denom,
start_after_incentive_denom,
limit,
} => to_binary(&query_user_unclaimed_rewards(
deps,
env,
user,
account_id,
start_after_collateral_denom,
start_after_incentive_denom,
limit,
Expand Down Expand Up @@ -635,13 +659,15 @@ pub fn query_user_unclaimed_rewards(
deps: Deps,
env: Env,
user: String,
account_id: Option<String>,
start_after_collateral_denom: Option<String>,
start_after_incentive_denom: Option<String>,
limit: Option<u32>,
) -> StdResult<Vec<Coin>> {
let red_bank_addr = query_red_bank_address(deps)?;
) -> Result<Vec<Coin>, ContractError> {
let user_addr = deps.api.addr_validate(&user)?;

let red_bank_addr = query_red_bank_address(deps)?;

let incentive_states = state::paginate_incentive_states(
deps.storage,
start_after_collateral_denom,
Expand All @@ -658,6 +684,7 @@ pub fn query_user_unclaimed_rewards(
&env.block,
&red_bank_addr,
&user_addr,
&account_id,
&collateral_denom,
&incentive_denom,
)?;
Expand Down
10 changes: 7 additions & 3 deletions contracts/incentives/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,22 @@ pub fn compute_user_unclaimed_rewards(
block: &BlockInfo,
red_bank_addr: &Addr,
user_addr: &Addr,
account_id: &Option<String>,
collateral_denom: &str,
incentive_denom: &str,
) -> StdResult<Uint128> {
let acc_id = account_id.clone().unwrap_or("".to_string());

let mut unclaimed_rewards = USER_UNCLAIMED_REWARDS
.may_load(storage.to_storage(), (user_addr, collateral_denom, incentive_denom))?
.may_load(storage.to_storage(), ((user_addr, &acc_id), collateral_denom, incentive_denom))?
.unwrap_or_else(Uint128::zero);

// Get asset user balances and total supply
let collateral: red_bank::UserCollateralResponse = querier.query_wasm_smart(
red_bank_addr,
&red_bank::QueryMsg::UserCollateral {
user: user_addr.to_string(),
account_id: account_id.clone(),
denom: collateral_denom.to_string(),
},
)?;
Expand All @@ -292,7 +296,7 @@ pub fn compute_user_unclaimed_rewards(
)?;

let user_asset_index = USER_ASSET_INDICES
.may_load(storage.to_storage(), (user_addr, collateral_denom, incentive_denom))?
.may_load(storage.to_storage(), ((user_addr, &acc_id), collateral_denom, incentive_denom))?
.unwrap_or_else(Decimal::zero);

if user_asset_index != incentive_state.index {
Expand All @@ -310,7 +314,7 @@ pub fn compute_user_unclaimed_rewards(
if user_asset_index != incentive_state.index {
USER_ASSET_INDICES.save(
*storage,
(user_addr, collateral_denom, incentive_denom),
((user_addr, &acc_id), collateral_denom, incentive_denom),
&incentive_state.index,
)?
}
Expand Down
12 changes: 7 additions & 5 deletions contracts/incentives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ pub const INCENTIVE_STATES: Map<(&str, &str), IncentiveState> = Map::new("incent
pub const EMISSIONS: Map<(&str, &str, u64), Uint128> = Map::new("emissions");

/// A map containing the incentive index for a given user, collateral denom and incentive denom.
/// The key is (user address, collateral denom, incentive denom).
pub const USER_ASSET_INDICES: Map<(&Addr, &str, &str), Decimal> = Map::new("indices");
/// The key is (user address with optional account id, collateral denom, incentive denom).
pub const USER_ASSET_INDICES: Map<((&Addr, &str), &str, &str), Decimal> = Map::new("indices");

/// A map containing the amount of unclaimed incentives for a given user and incentive denom.
/// The key is (user address, collateral denom, incentive denom).
pub const USER_UNCLAIMED_REWARDS: Map<(&Addr, &str, &str), Uint128> = Map::new("unclaimed_rewards");
/// The key is (user address with optional account id, collateral denom, incentive denom).
pub const USER_UNCLAIMED_REWARDS: Map<((&Addr, &str), &str, &str), Uint128> =
Map::new("unclaimed_rewards");

/// The default limit for pagination
pub const DEFAULT_LIMIT: u32 = 5;
Expand All @@ -50,13 +51,14 @@ pub const MAX_LIMIT: u32 = 10;
pub fn increase_unclaimed_rewards(
storage: &mut dyn Storage,
user_addr: &Addr,
acc_id: &str,
collateral_denom: &str,
incentive_denom: &str,
accrued_rewards: Uint128,
) -> StdResult<()> {
USER_UNCLAIMED_REWARDS.update(
storage,
(user_addr, collateral_denom, incentive_denom),
((user_addr, acc_id), collateral_denom, incentive_denom),
|ur: Option<Uint128>| -> StdResult<Uint128> {
Ok(ur.map_or_else(|| accrued_rewards, |r| r + accrued_rewards))
},
Expand Down
Loading