Skip to content

Commit

Permalink
support cl pool just query swap
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongsu-hong committed Oct 5, 2023
1 parent 0ba753e commit 879b34e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 36 deletions.
24 changes: 0 additions & 24 deletions packages/pool/src/cl.rs

This file was deleted.

79 changes: 79 additions & 0 deletions packages/pool/src/concentrated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, Decimal, Deps, Uint256};
use ibcx_interface::types::SwapRoute;
use {Decimal, StdResult};

use crate::{OsmosisPool, PoolError};

#[cw_serde]
pub struct Pool {
#[serde(rename = "@type")]
pub type_url: String,
pub address: String,
pub id: String,

pub incentives_address: String,
pub spread_rewards_address: String,

pub token0: String,
pub token1: String,

pub current_tick_liquidity: String,
pub current_sqrt_price: String,
pub current_tick: String,
pub tick_spacing: String,

pub spread_factor: String,
pub exponent_at_price_one: String,
pub last_liquidity_update: String,
}

impl OsmosisPool for Pool {
fn get_id(&self) -> u64 {
self.id.parse().unwrap()
}

fn get_type(&self) -> &str {
"concentrated_liquidity_pool"
}

fn get_spread_factor(&self) -> StdResult<Decimal> {
self.spread_factor.parse()
}

fn clone_box(&self) -> Box<dyn OsmosisPool> {
Box::new(self.clone())
}

fn swap_exact_amount_in(
&mut self,
deps: &Deps,
input_amount: Coin,
output_denom: String,
min_output_amount: Uint256,
spread_factor: Decimal,
) -> Result<Uint256, PoolError> {
Ok(SwapRoutes(vec![SwapRoute {
pool_id: self.get_id(),
token_denom: output_denom,
}])
.sim_swap_exact_in(&deps.querier, &self.address, input_amount)?
.into())
}

fn swap_exact_amount_out(
&mut self,
deps: &Deps,
input_denom: String,
max_input_amount: Uint256,
output_amount: Coin,
spread_factor: Decimal,
) -> Result<Uint256, PoolError> {
Ok(SwapRoutes(vec![SwapRoute {
pool_id: self.get_id(),
token_denom: input_denom,
}])
.sim_swap_exact_out(&deps.querier, &self.address, output_amount)?
.into())
}
}
9 changes: 4 additions & 5 deletions packages/pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod cl;
mod concentrated;
mod error;
mod sim;
mod stable;
Expand All @@ -13,7 +13,7 @@ use osmosis_std::types::osmosis::poolmanager::v1beta1::PoolRequest;
pub use error::PoolError;
pub use sim::Simulator;

pub use cl::Pool as CLPool;
pub use concentrated::Pool as ConcentratedPool;
pub use stable::Pool as StablePool;
pub use weighted::Pool as WeightedPool;

Expand All @@ -24,8 +24,6 @@ pub trait OsmosisPool {

fn get_spread_factor(&self) -> StdResult<Decimal>;

fn get_exit_fee(&self) -> StdResult<Decimal>;

fn clone_box(&self) -> Box<dyn OsmosisPool>;

fn swap_exact_amount_in(
Expand Down Expand Up @@ -55,7 +53,6 @@ impl Clone for Box<dyn OsmosisPool> {
#[cw_serde]
#[serde(untagged)]
pub enum Pool {
CL(CLPool),
CW {
#[serde(rename = "@type")]
type_url: String,
Expand All @@ -66,6 +63,7 @@ pub enum Pool {
},
Stable(StablePool),
Weighted(WeightedPool),
Concentrated(ConcentratedPool),
}

impl Pool {
Expand Down Expand Up @@ -99,6 +97,7 @@ pub fn query_pools(
match v.pool {
Pool::Stable(p) => Ok(Box::new(p)),
Pool::Weighted(p) => Ok(Box::new(p)),
Pool::Concentrated(p) => Ok(Box::new(p)),
_ => Err(PoolError::UnsupportedPoolType),
}
})
Expand Down
4 changes: 0 additions & 4 deletions packages/pool/src/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,6 @@ impl OsmosisPool for Pool {
Ok(self.pool_params.swap_fee)
}

fn get_exit_fee(&self) -> StdResult<Decimal> {
Ok(self.pool_params.exit_fee)
}

fn clone_box(&self) -> Box<dyn OsmosisPool> {
Box::new(self.clone())
}
Expand Down
5 changes: 2 additions & 3 deletions packages/pool/src/weighted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,14 @@ impl OsmosisPool for Pool {
fn get_id(&self) -> u64 {
self.id.parse::<u64>().unwrap()
}

fn get_type(&self) -> &str {
"weighted_pool"
}

fn get_spread_factor(&self) -> StdResult<Decimal> {
Ok(self.pool_params.swap_fee)
}
fn get_exit_fee(&self) -> StdResult<Decimal> {
Ok(self.pool_params.exit_fee)
}

fn clone_box(&self) -> Box<dyn OsmosisPool> {
Box::new(self.clone())
Expand Down

0 comments on commit 879b34e

Please sign in to comment.