Skip to content

Commit

Permalink
fix: incorrectly adding to lp amount rather than subtracting
Browse files Browse the repository at this point in the history
  • Loading branch information
markonmars committed Jun 20, 2024
1 parent cf750ee commit a112321
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/incentives/src/astro_incentives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn decrement_staked_lp(
.ok_or(ContractError::NoDeposits {
denom: lp_coin.denom.clone(),
})?
.checked_add(lp_coin.amount)?)
.checked_sub(lp_coin.amount)?)
})?;

Ok(())
Expand Down
270 changes: 270 additions & 0 deletions contracts/incentives/tests/tests/test_claim_astro_lp_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,273 @@ fn assert_only_credit_manager() {
)
.expect_err("Unauthorized");
}

#[test]
fn unstake_correctly_updates_lp_states() {
// SETUP
let env = mock_env();
let mut deps: OwnedDeps<MemoryStorage, MockApi, MarsMockQuerier> = th_setup();

// Users
let user_a_id = "1";
let user_b_id = "2";

let credit_manager = Addr::unchecked("credit_manager");
let astroport_incentives_addr = Addr::unchecked("astroport_incentives");
deps.querier.set_astroport_incentives_address(astroport_incentives_addr.clone());

let lp_denom = "uusd/ubtc";

// State:
// - LP in incentives = 0
// - Rewards available = 0
assert_eq!(ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(), None);

deposit_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_a_id.to_string(),
Coin::new(100u128, lp_denom),
)
.unwrap();

deposit_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_b_id.to_string(),
Coin::new(200u128, lp_denom),
)
.unwrap();

// LP in incentives = 300
// User a = 100
// User b = 200
assert_eq!(
ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(),
Some(Uint128::new(300u128))
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_a_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(100u128)
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_b_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(200u128)
);

unstake_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_a_id.to_string(),
ActionCoin {
denom: lp_denom.to_string(),
amount: mars_types::credit_manager::ActionAmount::Exact(Uint128::new(50u128)),
},
)
.unwrap();

// LP in incentives = 250
// User a = 50
// User b = 200
assert_eq!(
ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(),
Some(Uint128::new(250u128))
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_a_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(50u128)
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_b_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(200u128)
);

unstake_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_b_id.to_string(),
ActionCoin {
denom: lp_denom.to_string(),
amount: mars_types::credit_manager::ActionAmount::AccountBalance,
},
)
.unwrap();

// LP in incentives = 50
// User a = 50
// User b = 0
assert_eq!(
ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(),
Some(Uint128::new(50u128))
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_a_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(50u128)
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_b_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(0u128)
);
}

#[test]
fn stake_correctly_updates_lp_states() {
// SETUP
let env = mock_env();
let mut deps: OwnedDeps<MemoryStorage, MockApi, MarsMockQuerier> = th_setup();

// Users
let user_a_id = "1";
let user_b_id = "2";

let credit_manager = Addr::unchecked("credit_manager");
let astroport_incentives_addr = Addr::unchecked("astroport_incentives");
deps.querier.set_astroport_incentives_address(astroport_incentives_addr.clone());

let lp_denom = "uusd/ubtc";

// State:
// - LP in incentives = 0
// - Rewards available = 0
assert_eq!(ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(), None);

deposit_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_a_id.to_string(),
Coin::new(100u128, lp_denom),
).unwrap();

deposit_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_b_id.to_string(),
Coin::new(200u128, lp_denom),
).unwrap();

// LP in incentives = 300
// User a = 100
// User b = 200
assert_eq!(
ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(),
Some(Uint128::new(300u128))
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_a_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(100u128)
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_b_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(200u128)
);

deposit_for_user(
deps.as_mut(),
env.clone(),
credit_manager.as_str(),
user_a_id.to_string(),
Coin::new(50u128, lp_denom),
).unwrap();

// LP in incentives = 350
// User a = 150
// User b = 200
assert_eq!(
ASTRO_TOTAL_LP_DEPOSITS.may_load(&deps.storage, lp_denom).unwrap(),
Some(Uint128::new(350u128))
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_a_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(150u128)
);
assert_eq!(
query::query_staked_astro_lp_position(
deps.as_ref(),
env.clone(),
user_b_id.to_string(),
lp_denom.to_string()
)
.unwrap()
.lp_coin
.amount,
Uint128::new(200u128)
);
}

0 comments on commit a112321

Please sign in to comment.