Skip to content

Commit

Permalink
feat: use incentives native tokens structs in staking impl
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemc98 authored and Your Name committed May 1, 2024
1 parent 7a21e87 commit 6ccda69
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 32 deletions.
77 changes: 49 additions & 28 deletions cw-dex-astroport/src/staking.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,70 @@
//! Staking/rewards traits implementations for Astroport

use apollo_cw_asset::{AssetInfo, AssetList};
use apollo_utils::assets::separate_natives_and_cw20s;
use cosmwasm_schema::cw_serde;
use astroport::asset::{Asset as AstroAsset};
use cosmwasm_schema::{cw_serde, QueryResponses};
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,
}

#[cw_serde]
pub enum AstroportV5IncentivesExecuteMsg {
/// Stake LP tokens in the Generator. LP tokens staked on behalf of
/// recipient if recipient is set. Otherwise LP tokens are staked on
/// behalf of message sender.
Deposit { recipient: Option<String> },
/// Withdraw LP tokens from the Generator
Withdraw {
/// The LP token cw20 address or token factory denom
lp_token: String,
/// The amount to withdraw. Must not exceed total staked amount.
amount: Uint128,
},
/// Update rewards and return it to user.
ClaimRewards {
/// The LP token cw20 address or token factory denom
lp_tokens: Vec<String>,
},
}

/// We copied this in because versioning on Astroport is a mess and it's easier to simply copy it in.
/// These incentive related structs come from here: https://github.com/astroport-fi/hidden_astroport_core/blob/main/packages/astroport/src/incentives.rs#L92
/// This will be released as Astroport V5 at some point in the future

#[cw_serde]
#[derive(QueryResponses)]
pub enum AstroportV5IncentivesQueryMsg {
/// PendingToken returns the amount of rewards that can be claimed by an
/// account that deposited a specific LP token in a generator
#[returns(Vec<AstroAsset>)]
PendingRewards { lp_token: String, user: String },
}

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 })?,
})?,
funds: vec![],
contract_addr: self.incentives.to_string(),
msg: to_json_binary(&AstroportV5IncentivesExecuteMsg::Deposit { recipient: None })?,
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 +85,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(&AstroportV5IncentivesExecuteMsg::ClaimRewards {
lp_tokens: vec![self.lp_token.to_string()],
})?,
funds: vec![],
});
Expand Down Expand Up @@ -116,8 +137,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(&AstroportV5IncentivesQueryMsg::PendingRewards {
lp_token: self.lp_token.to_string(),
user: user.to_string(),
})?,
}))?
Expand All @@ -133,8 +154,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(&AstroportV5IncentivesExecuteMsg::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,

Check failure on line 305 in test-helpers/src/astroport.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find value `lp_token` in this scope
incentives_addr,
astro_token,
liquidity_manager_addr,
Expand Down

0 comments on commit 6ccda69

Please sign in to comment.