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

feat: use incentives native tokens structs in staking impl #162

Merged
merged 12 commits into from
Aug 28, 2024
42 changes: 16 additions & 26 deletions cw-dex-astroport/src/staking.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
//! Staking/rewards traits implementations for Astroport

use apollo_cw_asset::{AssetInfo, AssetList};
use apollo_utils::assets::separate_natives_and_cw20s;
use astroport::asset::Asset as AstroAsset;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, Addr, CosmosMsg, Deps, Empty, Env, Event, QuerierWrapper, QueryRequest,
coins, to_json_binary, Addr, CosmosMsg, Deps, Empty, Env, Event, QuerierWrapper, QueryRequest,
Response, Uint128, WasmMsg, WasmQuery,
};
use cw20::Cw20ExecuteMsg;

use apollo_cw_asset::AssetList;
use astroport::asset::Asset as AstroAsset;
use astroport_v3::incentives::{
Cw20Msg as IncentivesCw20Msg, ExecuteMsg as IncentivesExecuteMsg,
QueryMsg as IncentivesQueryMsg,
};

use cw_dex::traits::{Rewards, Stake, Staking, Unstake};
use cw_dex::CwDexError;

/// Represents staking of tokens on Astroport
#[cw_serde]
pub struct AstroportStaking {
/// The address of the associated LP token contract
pub lp_token_addr: Addr,
/// The cw20 or token factory denom of the associated LP token
pub lp_token: AssetInfo,
/// The address of the astroport incentives contract
pub incentives: Addr,
}
Expand All @@ -32,18 +24,16 @@ impl Staking for AstroportStaking {}
impl Stake for AstroportStaking {
fn stake(&self, _deps: Deps, _env: &Env, amount: Uint128) -> Result<Response, CwDexError> {
let stake_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.lp_token_addr.to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: self.incentives.to_string(),
amount,
msg: to_json_binary(&IncentivesCw20Msg::Deposit { recipient: None })?,
contract_addr: self.incentives.to_string(),
msg: to_json_binary(&astroport_v3::incentives::ExecuteMsg::Deposit {
recipient: None,
})?,
funds: vec![],
funds: coins(amount.into(), self.lp_token.to_string()),
});

let event = Event::new("apollo/cw-dex/stake")
.add_attribute("type", "astroport_staking")
.add_attribute("asset", self.lp_token_addr.to_string())
.add_attribute("asset", self.lp_token.to_string())
.add_attribute("incentives contract address", self.incentives.to_string());

Ok(Response::new().add_message(stake_msg).add_event(event))
Expand All @@ -64,8 +54,8 @@ impl Rewards for AstroportStaking {

let claim_rewards_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.incentives.to_string(),
msg: to_json_binary(&IncentivesExecuteMsg::ClaimRewards {
lp_tokens: vec![self.lp_token_addr.to_string()],
msg: to_json_binary(&astroport_v3::incentives::ExecuteMsg::ClaimRewards {
lp_tokens: vec![self.lp_token.to_string()],
})?,
funds: vec![],
});
Expand Down Expand Up @@ -116,8 +106,8 @@ impl Rewards for AstroportStaking {
let pending_rewards: Vec<AstroAsset> = querier
.query::<Vec<AstroAsset>>(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: self.incentives.to_string(),
msg: to_json_binary(&IncentivesQueryMsg::PendingRewards {
lp_token: self.lp_token_addr.to_string(),
msg: to_json_binary(&astroport_v3::incentives::QueryMsg::PendingRewards {
lp_token: self.lp_token.to_string(),
user: user.to_string(),
})?,
}))?
Expand All @@ -133,8 +123,8 @@ impl Unstake for AstroportStaking {
fn unstake(&self, _deps: Deps, _env: &Env, amount: Uint128) -> Result<Response, CwDexError> {
let unstake_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.incentives.to_string(),
msg: to_json_binary(&IncentivesExecuteMsg::Withdraw {
lp_token: self.lp_token_addr.to_string(),
msg: to_json_binary(&astroport_v3::incentives::ExecuteMsg::Withdraw {
lp_token: self.lp_token.to_string(),
amount,
})?,
funds: vec![],
Expand Down
2 changes: 1 addition & 1 deletion test-contracts/astroport-test-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn instantiate(
STAKING.save(
deps.storage,
&AstroportStaking {
lp_token_addr: Addr::unchecked(msg.lp_token_addr),
lp_token: AssetInfo::from_str(deps.api, &msg.lp_token),
incentives: Addr::unchecked(msg.incentives_addr),
},
)?;
Expand Down
2 changes: 1 addition & 1 deletion test-contracts/package/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct OsmosisTestContractInstantiateMsg {
#[cw_serde]
pub struct AstroportContractInstantiateMsg {
pub pair_addr: String,
pub lp_token_addr: String,
pub lp_token: String,
pub incentives_addr: String,
pub astro_token: AssetInfo,
pub liquidity_manager_addr: String,
Expand Down
4 changes: 2 additions & 2 deletions test-helpers/src/astroport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@ pub fn instantiate_test_astroport_contract<'a, R: Runner<'a>>(
pair_addr: String,
incentives_addr: String,
astro_token: AssetInfo,
lp_token_addr: String,
lp_token_denom: String,
liquidity_manager_addr: String,
signer: &SigningAccount,
) -> RunnerResult<String> {
let init_msg = AstroportContractInstantiateMsg {
pair_addr,
lp_token_addr,
lp_token: lp_token_denom,
incentives_addr,
astro_token,
liquidity_manager_addr,
Expand Down
Loading