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 Apr 30, 2024
1 parent 7a21e87 commit a033ee7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
67 changes: 42 additions & 25 deletions cw-dex-astroport/src/staking.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,66 @@
//! Staking/rewards traits implementations for Astroport

use apollo_cw_asset::AssetList;
use apollo_utils::assets::separate_natives_and_cw20s;
use cosmwasm_schema::cw_serde;
use astroport::asset::{Asset as AstroAsset, Asset};

Check failure on line 4 in cw-dex-astroport/src/staking.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `Asset`
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 token factory denom of the associated LP token
pub lp_token_denom: String,
/// The address of the astroport incentives contract
pub incentives: Addr,
}

#[cw_serde]
pub enum IncentivesExecuteMsg {
/// 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>,
},
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum IncentivesQueryMsg {
/// PendingToken returns the amount of rewards that can be claimed by an
/// account that deposited a specific LP token in a generator
#[returns(Vec<Asset>)]
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(&IncentivesExecuteMsg::Deposit { recipient: None })?,
funds: coins(amount.into(), self.lp_token_denom.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_denom.to_string())
.add_attribute("incentives contract address", self.incentives.to_string());

Ok(Response::new().add_message(stake_msg).add_event(event))
Expand All @@ -65,7 +82,7 @@ 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()],
lp_tokens: vec![self.lp_token_denom.to_string()],
})?,
funds: vec![],
});
Expand Down Expand Up @@ -117,7 +134,7 @@ impl Rewards for AstroportStaking {
.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(),
lp_token: self.lp_token_denom.to_string(),
user: user.to_string(),
})?,
}))?
Expand All @@ -134,7 +151,7 @@ impl Unstake for AstroportStaking {
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(),
lp_token: self.lp_token_denom.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_denom: msg.lp_token_denom,
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_denom: 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_denom,
incentives_addr,
astro_token,
liquidity_manager_addr,
Expand Down

0 comments on commit a033ee7

Please sign in to comment.