Skip to content

Commit

Permalink
fix: make astroport LM optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ApolloGie committed Jun 27, 2024
1 parent 4ee80b7 commit 647bb22
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
64 changes: 30 additions & 34 deletions cw-dex-astroport/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub struct AstroportPool {
/// The type of pool represented: Constant product (*Xyk*) or *Stableswap*
pub pair_type: PairType,
/// The address of the Astroport liquidity manager contract
pub liquidity_manager: Addr,
pub liquidity_manager: Option<Addr>,
}

impl AstroportPool {
/// Creates a new instance of `AstroportPool`
///
/// Arguments:
/// - `pair_addr`: The address of the pair contract associated with the pool
pub fn new(deps: Deps, pair_addr: Addr, liquidity_manager: Addr) -> StdResult<Self> {
pub fn new(deps: Deps, pair_addr: Addr, liquidity_manager: Option<Addr>) -> StdResult<Self> {
let pair_info = deps
.querier
.query_wasm_smart::<astroport_v5::asset::PairInfo>(
Expand Down Expand Up @@ -84,7 +84,7 @@ impl AstroportPool {
pub fn get_pool_for_lp_token(
deps: Deps,
lp_token: &AssetInfo,
astroport_liquidity_manager: Addr,
astroport_liquidity_manager: Option<Addr>,
) -> Result<Self, CwDexError> {
match lp_token {
AssetInfo::Cw20(address) => {
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Pool for AstroportPool {
_deps: Deps,
env: &Env,
assets: AssetList,
min_out: Uint128,
_min_out: Uint128,
) -> Result<Response, CwDexError> {
let (funds, cw20s) = separate_natives_and_cw20s(&assets);

Expand All @@ -142,7 +142,7 @@ impl Pool for AstroportPool {
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: asset.address,
msg: to_json_binary(&Cw20ExecuteMsg::IncreaseAllowance {
spender: self.liquidity_manager.to_string(),
spender: self.pair_addr.to_string(),
amount: asset.amount,
expires: Some(Expiration::AtHeight(env.block.height + 1)),
})?,
Expand All @@ -161,16 +161,12 @@ impl Pool for AstroportPool {

// Create the provide liquidity message
let provide_liquidity_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.liquidity_manager.to_string(),
msg: to_json_binary(&liquidity_manager::ExecuteMsg::ProvideLiquidity {
pair_addr: self.pair_addr.to_string(),
min_lp_to_receive: Some(min_out),
pair_msg: astroport::pair::ExecuteMsg::ProvideLiquidity {
assets: assets_vec.into_elementwise(),
slippage_tolerance: Some(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?),
auto_stake: Some(false),
receiver: None,
},
contract_addr: self.pair_addr.to_string(),
msg: to_json_binary(&astroport::pair::ExecuteMsg::ProvideLiquidity {
assets: assets_vec.into_elementwise(),
slippage_tolerance: Some(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?),
auto_stake: Some(false),
receiver: None,
})?,
funds,
});
Expand Down Expand Up @@ -205,7 +201,13 @@ impl Pool for AstroportPool {
let withdraw_liquidity = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_addr.to_string(),
msg: to_json_binary(&Cw20ExecuteMsg::Send {
contract: self.liquidity_manager.to_string(),
contract: self
.liquidity_manager
.clone()
.ok_or(CwDexError::Std(StdError::generic_err(
"Custom pair type is not supported",
)))?
.to_string(),
amount: asset.amount,
msg: to_json_binary(&liquidity_manager::Cw20HookMsg::WithdrawLiquidity {
pair_msg: astroport::pair::Cw20HookMsg::WithdrawLiquidity {
Expand Down Expand Up @@ -239,11 +241,11 @@ impl Pool for AstroportPool {
let withdraw_liquidity = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: self.pair_addr.to_string(),
msg: to_json_binary(&astroport_v5::pair::ExecuteMsg::WithdrawLiquidity {
assets: vec![asset_to_astroport_v5_asset(asset.clone())],
assets: vec![asset_to_astroport_v5_asset(&asset.clone())],
min_assets_to_receive: Some(
min_out
.to_vec()
.into_iter()
.iter()
.map(asset_to_astroport_v5_asset)
.collect(),
),
Expand Down Expand Up @@ -326,15 +328,10 @@ impl Pool for AstroportPool {
assets: AssetList,
) -> Result<Asset, CwDexError> {
let amount: Uint128 = deps.querier.query_wasm_smart(
self.liquidity_manager.to_string(),
&liquidity_manager::QueryMsg::SimulateProvide {
pair_addr: self.pair_addr.to_string(),
pair_msg: astroport::pair::ExecuteMsg::ProvideLiquidity {
assets: assets.into(),
slippage_tolerance: Some(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?),
auto_stake: Some(false),
receiver: None,
},
self.pair_addr.to_string(),
&astroport_v5::pair::QueryMsg::SimulateProvide {
assets: assets.iter().map(asset_to_astroport_v5_asset).collect(),
slippage_tolerance: Some(Decimal::from_str(MAX_ALLOWED_SLIPPAGE)?),
},
)?;

Expand All @@ -352,10 +349,9 @@ impl Pool for AstroportPool {
lp_token: &Asset,
) -> Result<AssetList, CwDexError> {
let assets: Vec<AstroAsset> = deps.querier.query_wasm_smart(
self.liquidity_manager.to_string(),
&liquidity_manager::QueryMsg::SimulateWithdraw {
pair_addr: self.pair_addr.to_string(),
lp_tokens: lp_token.amount,
self.pair_addr.to_string(),
&astroport_v5::pair::QueryMsg::SimulateWithdraw {
lp_amount: lp_token.amount,
},
)?;

Expand Down Expand Up @@ -396,11 +392,11 @@ pub fn astroport_v5_assetinfo_to_assetinfo(asset: astroport_v5::asset::AssetInfo
}
}

pub fn asset_to_astroport_v5_asset(asset: Asset) -> astroport_v5::asset::Asset {
match asset.info {
pub fn asset_to_astroport_v5_asset(asset: &Asset) -> astroport_v5::asset::Asset {
match &asset.info {
AssetInfoBase::Native(denom) => astroport_v5::asset::Asset::native(denom, asset.amount),
AssetInfo::Cw20(contract_addr) => {
astroport_v5::asset::Asset::cw20(contract_addr, asset.amount)
astroport_v5::asset::Asset::cw20(contract_addr.clone(), asset.amount)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions test-contracts/astroport-test-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let pair_addr = deps.api.addr_validate(&msg.pair_addr)?;
let liquidity_manager = deps.api.addr_validate(&msg.liquidity_manager_addr)?;
let pool = AstroportPool::new(deps.as_ref(), pair_addr, liquidity_manager)?;
let pool = AstroportPool::new(deps.as_ref(), pair_addr, None)?;
POOL.save(deps.storage, &pool)?;

STAKING.save(
Expand Down
1 change: 0 additions & 1 deletion test-contracts/package/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub struct AstroportContractInstantiateMsg {
pub lp_token: String,
pub incentives_addr: String,
pub astro_token: AssetInfo,
pub liquidity_manager_addr: String,
}

#[cw_serde]
Expand Down
3 changes: 0 additions & 3 deletions test-helpers/src/astroport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ pub fn setup_pool_and_test_contract<'a>(
astroport_contracts.astro_token.address.clone(),
)),
lp_token_addr.clone(),
astroport_contracts.liquidity_manager.address.clone(),
&accs[0],
)?;

Expand All @@ -297,15 +296,13 @@ pub fn instantiate_test_astroport_contract<'a, R: Runner<'a>>(
incentives_addr: String,
astro_token: AssetInfo,
lp_token: String,
liquidity_manager_addr: String,
signer: &SigningAccount,
) -> RunnerResult<String> {
let init_msg = AstroportContractInstantiateMsg {
pair_addr,
lp_token,
incentives_addr,
astro_token,
liquidity_manager_addr,
};

let wasm = Wasm::new(runner);
Expand Down

0 comments on commit 647bb22

Please sign in to comment.