-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mp 2347 integrate astro staking into credit manager (#394)
* feat: stake lp in astroport incentives * tidy * move mars / astroport incentives to individual files * correctly add files for previous commit * fmt * fix tests * add missing import * don't swallow errors, update naming * update credit_manager assertion * credit manager astroport incentives implementation * fix lp rewards query * Mock astroport incentives dependency * tidy structure * astro_lp incentives integration test * add lp rewards query * tidy * additional tests * fmt * add cm tests for astro incentives * Fix astro deps in incentives. * add rewards recieved to event attributes * fix - increment not decrement * error if lp balance lt requested * improve tests * fix staking issue * add unstake tests * Add staked lp to account collateral * clippy + fmt * schema * fix deps * add staked lp to positions * remove redundant code * improve test coverage * generate types * add incentive query tests * naming * generate schema * comments * add incentives documentation (#399) * add incentives documentation * remove todo * better error handling * rename files * add rewards as attribute * improve naming, attributes on response * improve test_claim_lp_rewards * improve test_stake_astro_lp * improve test_unstake_astro_lp * improve test_querying * small fixes and naming * improve test_astro_lp_incentives * more naming * improve tests * update types * merge 'dev' into MP-2347-integrate-astro-staking * updated types * Reorder in tests. * Rename claming test case for astro. --------- Co-authored-by: piobab <[email protected]>
- Loading branch information
1 parent
9572675
commit 1fb38b1
Showing
137 changed files
with
3,615 additions
and
2,126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use cosmwasm_std::{DepsMut, Response}; | ||
use mars_types::traits::Stringify; | ||
|
||
use crate::{ | ||
error::{ContractError, ContractResult}, | ||
state::INCENTIVES, | ||
utils::increment_coin_balance, | ||
}; | ||
|
||
pub fn claim_lp_rewards( | ||
deps: DepsMut, | ||
account_id: &str, | ||
lp_denom: &str, | ||
) -> ContractResult<Response> { | ||
let incentives = INCENTIVES.load(deps.storage)?; | ||
|
||
// Query rewards user is receiving, update balance | ||
let rewards = incentives.query_staked_astro_lp_rewards(&deps.querier, account_id, lp_denom)?; | ||
if rewards.is_empty() { | ||
return Err(ContractError::NoAmount); | ||
} | ||
|
||
for reward in rewards.iter() { | ||
increment_coin_balance(deps.storage, account_id, reward)?; | ||
} | ||
|
||
let claim_rewards_msg = incentives.claim_staked_astro_lp_rewards_msg(account_id, lp_denom)?; | ||
let mut res = Response::new() | ||
.add_message(claim_rewards_msg) | ||
.add_attribute("action", "claim_astro_lp_rewards") | ||
.add_attribute("account_id", account_id); | ||
|
||
if !rewards.is_empty() { | ||
res = res.add_attribute("rewards", rewards.as_slice().to_string()); | ||
} | ||
|
||
Ok(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use cosmwasm_std::{coin, DepsMut, Response}; | ||
use mars_types::{ | ||
credit_manager::{ActionAmount, ActionCoin}, | ||
traits::Stringify, | ||
}; | ||
|
||
use crate::{ | ||
error::ContractResult, | ||
state::{COIN_BALANCES, INCENTIVES}, | ||
utils::{decrement_coin_balance, increment_coin_balance}, | ||
}; | ||
|
||
pub fn stake_lp(deps: DepsMut, account_id: &str, lp_coin: ActionCoin) -> ContractResult<Response> { | ||
let incentives = INCENTIVES.load(deps.storage)?; | ||
|
||
// Query rewards user is receiving to update their balances | ||
let rewards = incentives.query_staked_astro_lp_rewards( | ||
&deps.querier, | ||
account_id, | ||
lp_coin.denom.as_str(), | ||
)?; | ||
|
||
let coin_balance = | ||
COIN_BALANCES.may_load(deps.storage, (account_id, &lp_coin.denom))?.unwrap_or_default(); | ||
let new_amount = match lp_coin.amount { | ||
ActionAmount::Exact(amt) => amt, | ||
ActionAmount::AccountBalance => coin_balance, | ||
}; | ||
|
||
let updated_coin = coin(new_amount.u128(), lp_coin.denom.as_str()); | ||
|
||
decrement_coin_balance(deps.storage, account_id, &updated_coin)?; | ||
|
||
for reward in rewards.iter() { | ||
increment_coin_balance(deps.storage, account_id, reward)?; | ||
} | ||
|
||
// stake msg | ||
let stake_msg = incentives.stake_astro_lp_msg(account_id, updated_coin)?; | ||
let mut res = Response::new() | ||
.add_message(stake_msg) | ||
.add_attribute("action", "stake_astro_lp") | ||
.add_attribute("account_id", account_id); | ||
|
||
if !rewards.is_empty() { | ||
res = res.add_attribute("rewards", rewards.as_slice().to_string()); | ||
} | ||
|
||
Ok(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use cosmwasm_std::{coin, DepsMut, Response}; | ||
use mars_types::{ | ||
credit_manager::{ActionAmount, ActionCoin}, | ||
traits::Stringify, | ||
}; | ||
|
||
use crate::{ | ||
error::{ContractError, ContractResult}, | ||
state::INCENTIVES, | ||
utils::increment_coin_balance, | ||
}; | ||
|
||
pub fn unstake_lp( | ||
deps: DepsMut, | ||
account_id: &str, | ||
lp_coin: ActionCoin, | ||
) -> ContractResult<Response> { | ||
let incentives = INCENTIVES.load(deps.storage)?; | ||
|
||
// Query rewards user is receiving, update balance | ||
let lp_position = incentives.query_staked_astro_lp_position( | ||
&deps.querier, | ||
account_id, | ||
lp_coin.denom.as_str(), | ||
)?; | ||
|
||
for reward in lp_position.rewards.iter() { | ||
increment_coin_balance(deps.storage, account_id, reward)?; | ||
} | ||
|
||
let new_amount = match lp_coin.amount { | ||
ActionAmount::Exact(amt) => { | ||
if lp_position.lp_coin.amount.lt(&amt) { | ||
return Err(ContractError::InsufficientFunds { | ||
requested: amt, | ||
available: lp_position.lp_coin.amount, | ||
}); | ||
} else { | ||
lp_position.lp_coin.amount.checked_sub(amt)? | ||
} | ||
} | ||
ActionAmount::AccountBalance => lp_position.lp_coin.amount, | ||
}; | ||
|
||
let updated_coin = coin(new_amount.u128(), lp_coin.denom.as_str()); | ||
|
||
increment_coin_balance(deps.storage, account_id, &updated_coin)?; | ||
|
||
// unstake msg | ||
let unstake_msg = incentives.unstake_astro_lp_msg(account_id, &updated_coin)?; | ||
|
||
let mut res = Response::new() | ||
.add_message(unstake_msg) | ||
.add_attribute("action", "unstake_astro_lp") | ||
.add_attribute("account_id", account_id) | ||
.add_attribute("lp_unstaked", updated_coin.to_string()); | ||
|
||
if !lp_position.rewards.is_empty() { | ||
res = res.add_attribute("rewards", lp_position.rewards.as_slice().to_string()); | ||
} | ||
|
||
Ok(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.