From 879b34e4114ad03e6994407e6db9e47cf9ae4c77 Mon Sep 17 00:00:00 2001 From: byeongsu-hong Date: Thu, 5 Oct 2023 00:50:31 +0000 Subject: [PATCH] support cl pool just query swap --- packages/pool/src/cl.rs | 24 ---------- packages/pool/src/concentrated.rs | 79 +++++++++++++++++++++++++++++++ packages/pool/src/lib.rs | 9 ++-- packages/pool/src/stable.rs | 4 -- packages/pool/src/weighted.rs | 5 +- 5 files changed, 85 insertions(+), 36 deletions(-) delete mode 100644 packages/pool/src/cl.rs create mode 100644 packages/pool/src/concentrated.rs diff --git a/packages/pool/src/cl.rs b/packages/pool/src/cl.rs deleted file mode 100644 index 877e533..0000000 --- a/packages/pool/src/cl.rs +++ /dev/null @@ -1,24 +0,0 @@ -use cosmwasm_schema::cw_serde; - -#[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, -} diff --git a/packages/pool/src/concentrated.rs b/packages/pool/src/concentrated.rs new file mode 100644 index 0000000..e7678a1 --- /dev/null +++ b/packages/pool/src/concentrated.rs @@ -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 { + self.spread_factor.parse() + } + + fn clone_box(&self) -> Box { + 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 { + 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 { + Ok(SwapRoutes(vec![SwapRoute { + pool_id: self.get_id(), + token_denom: input_denom, + }]) + .sim_swap_exact_out(&deps.querier, &self.address, output_amount)? + .into()) + } +} diff --git a/packages/pool/src/lib.rs b/packages/pool/src/lib.rs index 0fdce1b..cc716ba 100644 --- a/packages/pool/src/lib.rs +++ b/packages/pool/src/lib.rs @@ -1,4 +1,4 @@ -mod cl; +mod concentrated; mod error; mod sim; mod stable; @@ -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; @@ -24,8 +24,6 @@ pub trait OsmosisPool { fn get_spread_factor(&self) -> StdResult; - fn get_exit_fee(&self) -> StdResult; - fn clone_box(&self) -> Box; fn swap_exact_amount_in( @@ -55,7 +53,6 @@ impl Clone for Box { #[cw_serde] #[serde(untagged)] pub enum Pool { - CL(CLPool), CW { #[serde(rename = "@type")] type_url: String, @@ -66,6 +63,7 @@ pub enum Pool { }, Stable(StablePool), Weighted(WeightedPool), + Concentrated(ConcentratedPool), } impl Pool { @@ -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), } }) diff --git a/packages/pool/src/stable.rs b/packages/pool/src/stable.rs index eaa703c..0d83934 100644 --- a/packages/pool/src/stable.rs +++ b/packages/pool/src/stable.rs @@ -295,10 +295,6 @@ impl OsmosisPool for Pool { Ok(self.pool_params.swap_fee) } - fn get_exit_fee(&self) -> StdResult { - Ok(self.pool_params.exit_fee) - } - fn clone_box(&self) -> Box { Box::new(self.clone()) } diff --git a/packages/pool/src/weighted.rs b/packages/pool/src/weighted.rs index b16162e..18c3e3b 100644 --- a/packages/pool/src/weighted.rs +++ b/packages/pool/src/weighted.rs @@ -213,15 +213,14 @@ impl OsmosisPool for Pool { fn get_id(&self) -> u64 { self.id.parse::().unwrap() } + fn get_type(&self) -> &str { "weighted_pool" } + fn get_spread_factor(&self) -> StdResult { Ok(self.pool_params.swap_fee) } - fn get_exit_fee(&self) -> StdResult { - Ok(self.pool_params.exit_fee) - } fn clone_box(&self) -> Box { Box::new(self.clone())