From 199baddb84f9715da19af10d26549cc77e611c59 Mon Sep 17 00:00:00 2001 From: piobab Date: Wed, 23 Aug 2023 12:11:23 +0200 Subject: [PATCH] Add CL pool support for price sources. (#301) --- Cargo.lock | 8 ++-- Cargo.toml | 2 +- contracts/oracle/osmosis/src/helpers.rs | 8 +++- contracts/oracle/osmosis/src/price_source.rs | 8 ++++ .../oracle/osmosis/tests/tests/helpers/mod.rs | 29 +++++++++++- .../tests/tests/test_set_price_source.rs | 11 ++++- packages/chains/osmosis/src/helpers.rs | 45 ++++++++++++++++++- packages/chains/osmosis/src/lib.rs | 7 ++- 8 files changed, 107 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2309a7203..af441b202 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2276,9 +2276,9 @@ checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" [[package]] name = "osmosis-std" -version = "0.16.1" +version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fa46d2ad5ae738572887974e000934374ce3546b820505c0ee19ca708e49622" +checksum = "75895e4db1a81ca29118e366365744f64314938327e4eedba8e6e462fb15e94f" dependencies = [ "chrono", "cosmwasm-std", @@ -2292,9 +2292,9 @@ dependencies = [ [[package]] name = "osmosis-std-derive" -version = "0.16.1" +version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11c2ba5535743617d6f44ae8d572d064fabab6d06ffcf403512f89c58954dbe9" +checksum = "f47f0b2f22adb341bb59e5a3a1b464dde033181954bd055b9ae86d6511ba465b" dependencies = [ "itertools", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 814aeb606..71ea9291f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ cw-storage-plus = "1.1.0" cw-utils = "1.0.1" mars-owner = { version = "2.0.0", features = ["emergency-owner"] } neutron-sdk = "0.6.1" -osmosis-std = "0.16.1" +osmosis-std = "0.16.2" prost = { version = "0.11.9", default-features = false } pyth-sdk-cw = "1.2.0" schemars = "0.8.12" diff --git a/contracts/oracle/osmosis/src/helpers.rs b/contracts/oracle/osmosis/src/helpers.rs index 3d6d9a67e..e3b612ca2 100644 --- a/contracts/oracle/osmosis/src/helpers.rs +++ b/contracts/oracle/osmosis/src/helpers.rs @@ -9,7 +9,7 @@ use crate::DowntimeDetector; /// 48 hours in seconds const TWO_DAYS_IN_SECONDS: u64 = 172800u64; -/// Assert the Osmosis pool indicated by `pool_id` is of Balancer XYK or StableSwap and assets are OSMO and `denom` +/// Assert the Osmosis pool indicated by `pool_id` is of Balancer XYK, StableSwap or ConcentratedLiquidity and assets are OSMO and `denom` pub fn assert_osmosis_pool_assets( pool: &Pool, denom: &str, @@ -23,6 +23,7 @@ pub fn assert_osmosis_pool_assets( assert_equal_asset_weights(balancer_pool)?; } Pool::StableSwap(_) => {} + Pool::ConcentratedLiquidity(_) => {} }; Ok(()) @@ -39,6 +40,11 @@ pub fn assert_osmosis_xyk_lp_pool(pool: &Pool) -> ContractResult<()> { reason: format!("StableSwap pool not supported. Pool id {}", stable_swap_pool.id), }); } + Pool::ConcentratedLiquidity(cl_pool) => { + return Err(ContractError::InvalidPriceSource { + reason: format!("ConcentratedLiquidity pool not supported. Pool id {}", cl_pool.id), + }); + } }; Ok(()) diff --git a/contracts/oracle/osmosis/src/price_source.rs b/contracts/oracle/osmosis/src/price_source.rs index 0b2e5b4f9..1a0e9f95e 100644 --- a/contracts/oracle/osmosis/src/price_source.rs +++ b/contracts/oracle/osmosis/src/price_source.rs @@ -612,6 +612,14 @@ impl OsmosisPriceSourceChecked { reason: format!("StableSwap pool not supported. Pool id {}", pool.id), }) } + Pool::ConcentratedLiquidity(pool) => { + return Err(ContractError::InvalidPrice { + reason: format!( + "ConcentratedLiquidity pool not supported. Pool id {}", + pool.id + ), + }) + } }; let coin0 = Pool::unwrap_coin(&pool.pool_assets[0].token)?; diff --git a/contracts/oracle/osmosis/tests/tests/helpers/mod.rs b/contracts/oracle/osmosis/tests/tests/helpers/mod.rs index a022146e9..ba18c9670 100644 --- a/contracts/oracle/osmosis/tests/tests/helpers/mod.rs +++ b/contracts/oracle/osmosis/tests/tests/helpers/mod.rs @@ -9,7 +9,7 @@ use cosmwasm_std::{ }; use mars_oracle_base::ContractError; use mars_oracle_osmosis::{contract::entry, msg::ExecuteMsg, OsmosisPriceSourceUnchecked}; -use mars_osmosis::{BalancerPool, StableSwapPool}; +use mars_osmosis::{BalancerPool, ConcentratedLiquidityPool, StableSwapPool}; use mars_red_bank_types::oracle::msg::{InstantiateMsg, QueryMsg}; use mars_testing::{mock_info, MarsMockQuerier}; use osmosis_std::types::osmosis::{gamm::v1beta1::PoolAsset, poolmanager::v1beta1::PoolResponse}; @@ -96,6 +96,10 @@ pub fn setup_test_with_pools() -> OwnedDeps } } +pub fn prepare_query_cl_pool_response(pool_id: u64, token0: &str, token1: &str) -> PoolResponse { + let pool = ConcentratedLiquidityPool { + address: "osmo126pr9qp44aft4juw7x4ev4s2qdtnwe38jzwunec9pxt5cpzaaphqyagqpu".to_string(), + incentives_address: "osmo1h2mhtj3wmsdt3uacev9pgpg38hkcxhsmyyn9ums0ya6eddrsafjsxs9j03" + .to_string(), + spread_rewards_address: "osmo16j5sssw32xuk8a0kjj8n54g25ye6kr339nz5axf8lzyeajk0k22stsm36c" + .to_string(), + id: pool_id, + current_tick_liquidity: "3820025893854099618.699762490947860933".to_string(), + token0: token0.to_string(), + token1: token1.to_string(), + current_sqrt_price: "656651.537483144215151633465586753226461989".to_string(), + current_tick: 102311912, + tick_spacing: 100, + exponent_at_price_one: -6, + spread_factor: "0.002000000000000000".to_string(), + last_liquidity_update: None, + }; + PoolResponse { + pool: Some(pool.to_any()), + } +} + pub fn set_pyth_price_source(deps: DepsMut, denom: &str, price_id: PriceIdentifier) { set_price_source( deps, diff --git a/contracts/oracle/osmosis/tests/tests/test_set_price_source.rs b/contracts/oracle/osmosis/tests/tests/test_set_price_source.rs index f0f38ee5a..11613758d 100644 --- a/contracts/oracle/osmosis/tests/tests/test_set_price_source.rs +++ b/contracts/oracle/osmosis/tests/tests/test_set_price_source.rs @@ -1036,7 +1036,7 @@ fn setting_price_source_xyk_lp() { ); // attempting to use StableSwap pool - let err = set_price_source_xyk_lp("atom_mars_lp", 5555).unwrap_err(); + let err = set_price_source_xyk_lp("atom_uosmo_lp", 5555).unwrap_err(); assert_eq!( err, ContractError::InvalidPriceSource { @@ -1044,6 +1044,15 @@ fn setting_price_source_xyk_lp() { } ); + // attempting to use ConcentratedLiquid pool + let err = set_price_source_xyk_lp("ujuno_uosmo_lp", 7777).unwrap_err(); + assert_eq!( + err, + ContractError::InvalidPriceSource { + reason: "ConcentratedLiquidity pool not supported. Pool id 7777".to_string() + } + ); + // properly set xyk lp price source let res = set_price_source_xyk_lp("uosmo_umars_lp", 89).unwrap(); assert_eq!(res.messages.len(), 0); diff --git a/packages/chains/osmosis/src/helpers.rs b/packages/chains/osmosis/src/helpers.rs index 6cb9e204d..875d38a13 100644 --- a/packages/chains/osmosis/src/helpers.rs +++ b/packages/chains/osmosis/src/helpers.rs @@ -8,6 +8,7 @@ use osmosis_std::{ types::{ cosmos::base::v1beta1::Coin, osmosis::{ + concentratedliquidity::v1beta1::Pool as ConcentratedLiquidityPool, downtimedetector::v1beta1::DowntimedetectorQuerier, gamm::{ poolmodels::stableswap::v1beta1::Pool as StableSwapPool, @@ -30,6 +31,7 @@ pub trait CommonPoolData { pub enum Pool { Balancer(BalancerPool), StableSwap(StableSwapPool), + ConcentratedLiquidity(ConcentratedLiquidityPool), } impl CommonPoolData for Pool { @@ -37,6 +39,7 @@ impl CommonPoolData for Pool { match self { Pool::Balancer(pool) => pool.id, Pool::StableSwap(pool) => pool.id, + Pool::ConcentratedLiquidity(pool) => pool.id, } } @@ -51,6 +54,9 @@ impl CommonPoolData for Pool { Pool::StableSwap(pool) => { pool.pool_liquidity.iter().map(|pl| pl.denom.clone()).collect() } + Pool::ConcentratedLiquidity(pool) => { + vec![pool.token0.clone(), pool.token1.clone()] + } } } } @@ -62,13 +68,18 @@ impl TryFrom for Pool { if let Ok(pool) = BalancerPool::decode(value.value.as_slice()) { return Ok(Pool::Balancer(pool)); } + if let Ok(pool) = StableSwapPool::decode(value.value.as_slice()) { return Ok(Pool::StableSwap(pool)); } + if let Ok(pool) = ConcentratedLiquidityPool::decode(value.value.as_slice()) { + return Ok(Pool::ConcentratedLiquidity(pool)); + } + Err(StdError::parse_err( "Pool", - "Unsupported pool: must be either `Balancer` or `StableSwap`.", + "Unsupported pool: must be either `Balancer`, `StableSwap` or `ConcentratedLiquidity`.", )) } } @@ -279,4 +290,36 @@ mod tests { assert_eq!(stable_swap_pool.id, pool.get_pool_id()); assert_eq!(vec!["denom_1".to_string(), "denom_2".to_string()], pool.get_pool_denoms()) } + + #[test] + fn common_data_for_concentrated_liquidity_pool() { + let concentrated_liquidity_pool = ConcentratedLiquidityPool { + address: "pool_address".to_string(), + incentives_address: "incentives_address".to_string(), + spread_rewards_address: "spread_rewards_address".to_string(), + id: 1066, + current_tick_liquidity: "3820025893854099618.699762490947860933".to_string(), + token0: "uosmo".to_string(), + token1: "ibc/0CD3A0285E1341859B5E86B6AB7682F023D03E97607CCC1DC95706411D866DF7" + .to_string(), + current_sqrt_price: "656651.537483144215151633465586753226461989".to_string(), + current_tick: 102311912, + tick_spacing: 100, + exponent_at_price_one: -6, + spread_factor: "0.002000000000000000".to_string(), + last_liquidity_update: None, + }; + + let any_pool = concentrated_liquidity_pool.to_any(); + let pool: Pool = any_pool.try_into().unwrap(); + + assert_eq!(concentrated_liquidity_pool.id, pool.get_pool_id()); + assert_eq!( + vec![ + "uosmo".to_string(), + "ibc/0CD3A0285E1341859B5E86B6AB7682F023D03E97607CCC1DC95706411D866DF7".to_string() + ], + pool.get_pool_denoms() + ); + } } diff --git a/packages/chains/osmosis/src/lib.rs b/packages/chains/osmosis/src/lib.rs index cfa498093..8d7ed0ea0 100644 --- a/packages/chains/osmosis/src/lib.rs +++ b/packages/chains/osmosis/src/lib.rs @@ -1,5 +1,8 @@ pub mod helpers; -pub use osmosis_std::types::osmosis::gamm::{ - poolmodels::stableswap::v1beta1::Pool as StableSwapPool, v1beta1::Pool as BalancerPool, +pub use osmosis_std::types::osmosis::{ + concentratedliquidity::v1beta1::Pool as ConcentratedLiquidityPool, + gamm::{ + poolmodels::stableswap::v1beta1::Pool as StableSwapPool, v1beta1::Pool as BalancerPool, + }, };