Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Oak no. 3 - stake lp when no existing position #412

Merged
merged 3 commits into from
Jun 24, 2024
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
33 changes: 21 additions & 12 deletions contracts/incentives/src/astro_incentives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,29 @@ fn update_user_lp_position(
credit_manager_addr: &str,
modification: LpModification,
) -> Result<Response, ContractError> {
let staked_lp_amount = ASTRO_USER_LP_DEPOSITS
.may_load(deps.storage, (&account_id, &lp_coin.denom))?
.unwrap_or(Uint128::zero());
// Astroport raises an error if there is no existing position and we query rewards.
// Therefore, we check first to ensure we don't fail first time somebody stakes
// https://github.com/astroport-fi/astroport-core/blob/main/contracts/tokenomics/incentives/src/state.rs#L539
let total_staked_lp_amount =
ASTRO_TOTAL_LP_DEPOSITS.may_load(deps.storage, &lp_coin.denom)?.unwrap_or(Uint128::zero());

// Claim all rewards from astroport before any modification
piobab marked this conversation as resolved.
Show resolved Hide resolved
let mut res = claim_rewards_for_staked_lp_position(
&mut deps,
astroport_incentives_addr,
mars_incentives_addr,
credit_manager_addr,
account_id,
&lp_coin.denom,
staked_lp_amount,
)?;
let mut res = if !total_staked_lp_amount.is_zero() {
let staked_lp_amount = ASTRO_USER_LP_DEPOSITS
.may_load(deps.storage, (&account_id, &lp_coin.denom))?
.unwrap_or(Uint128::zero());
claim_rewards_for_staked_lp_position(
piobab marked this conversation as resolved.
Show resolved Hide resolved
&mut deps,
astroport_incentives_addr,
mars_incentives_addr,
credit_manager_addr,
account_id,
&lp_coin.denom,
staked_lp_amount,
)?
} else {
Response::new()
};

res = match modification {
// Deposit stakes lp coin in astroport incentives
Expand Down
4 changes: 3 additions & 1 deletion contracts/mock-astroport-incentives/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub fn query_rewards(
)
.collect()),

None => Ok(vec![]),
None => Err(cosmwasm_std::StdError::NotFound {
kind: "position not found".to_string(),
}),
}
}
29 changes: 29 additions & 0 deletions integration-tests/tests/test_astro_lp_incentives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ use mars_testing::integration::mock_env::MockEnvBuilder;
use crate::helpers::default_asset_params;
mod helpers;

#[test]
fn can_be_first_staker() {
let owner = Addr::unchecked("owner");
let mut mock_env = MockEnvBuilder::new(None, owner).build();

// Contracts
let params = mock_env.params.clone();
let incentives = mock_env.incentives.clone();
let credit_manager = mock_env.credit_manager.clone();

// Params
let lp_denom = "factory12345";
let lp_coin = coin(1_000_000_000, lp_denom.to_string());

// Set asset params for lp token
let (_, asset_params) = default_asset_params(lp_denom);
params.init_params(&mut mock_env, asset_params);

// Fund accounts
let funded_amt = 10_000_000_000u128;
mock_env.fund_account(&credit_manager, &[coin(funded_amt, lp_denom)]);

incentives.stake_astro_lp(&mut mock_env, &credit_manager, "1".to_string(), lp_coin.clone());

let astro_lp_balance =
mock_env.query_balance(&mock_env.astro_incentives.contract_addr, lp_denom).unwrap();
assert_eq!(astro_lp_balance, lp_coin)
}

// User A stakes lp in astro and claims rewards
#[test]
fn claim_rewards() {
Expand Down
Loading