From 357d82f5f2982468671f6cc3e372e9ca9a518895 Mon Sep 17 00:00:00 2001 From: Kerber0x Date: Mon, 29 Apr 2024 16:52:22 +0100 Subject: [PATCH] chore: add total LP share amount to the Pair query response --- .../pool-manager/src/contract.rs | 12 ++-- .../liquidity_hub/pool-manager/src/queries.rs | 17 ++++- .../src/tests/integration_tests.rs | 65 +++++++++++-------- .../pool-manager/src/tests/suite.rs | 17 ----- packages/white-whale-std/src/pool_manager.rs | 1 + 5 files changed, 58 insertions(+), 54 deletions(-) diff --git a/contracts/liquidity_hub/pool-manager/src/contract.rs b/contracts/liquidity_hub/pool-manager/src/contract.rs index 66d60308..b002893e 100644 --- a/contracts/liquidity_hub/pool-manager/src/contract.rs +++ b/contracts/liquidity_hub/pool-manager/src/contract.rs @@ -1,8 +1,8 @@ use crate::error::ContractError; use crate::helpers::{reverse_simulate_swap_operations, simulate_swap_operations}; -use crate::queries::{get_swap_route, get_swap_route_creator, get_swap_routes}; +use crate::queries::{get_pair, get_swap_route, get_swap_route_creator, get_swap_routes}; use crate::router::commands::{add_swap_routes, remove_swap_routes}; -use crate::state::{Config, MANAGER_CONFIG, PAIRS, PAIR_COUNTER}; +use crate::state::{Config, MANAGER_CONFIG, PAIR_COUNTER}; use crate::{liquidity, manager, queries, router, swap}; #[cfg(not(feature = "library"))] use cosmwasm_std::{ @@ -11,7 +11,7 @@ use cosmwasm_std::{ use cw2::set_contract_version; use semver::Version; use white_whale_std::pool_manager::{ - ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, PairInfoResponse, QueryMsg, + ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, QueryMsg, }; // version info for migration info @@ -244,9 +244,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result Ok(to_json_binary(&get_swap_routes(deps)?)?), QueryMsg::Ownership {} => Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?), - QueryMsg::Pair { pair_identifier } => Ok(to_json_binary(&PairInfoResponse { - pair_info: PAIRS.load(deps.storage, &pair_identifier)?, - })?), + QueryMsg::Pair { pair_identifier } => { + Ok(to_json_binary(&get_pair(deps, pair_identifier)?)?) + } QueryMsg::SwapRouteCreator { offer_asset_denom, ask_asset_denom, diff --git a/contracts/liquidity_hub/pool-manager/src/queries.rs b/contracts/liquidity_hub/pool-manager/src/queries.rs index dc45103f..39f5c3b4 100644 --- a/contracts/liquidity_hub/pool-manager/src/queries.rs +++ b/contracts/liquidity_hub/pool-manager/src/queries.rs @@ -3,8 +3,8 @@ use std::cmp::Ordering; use cosmwasm_std::{Coin, Decimal256, Deps, Env, Fraction, Order, StdResult, Uint128}; use white_whale_std::pool_manager::{ - AssetDecimalsResponse, Config, SwapRoute, SwapRouteCreatorResponse, SwapRouteResponse, - SwapRoutesResponse, + AssetDecimalsResponse, Config, PairInfoResponse, SwapRoute, SwapRouteCreatorResponse, + SwapRouteResponse, SwapRoutesResponse, }; use white_whale_std::pool_network::{ asset::PairType, @@ -12,7 +12,7 @@ use white_whale_std::pool_network::{ // router::SimulateSwapOperationsResponse, }; -use crate::state::MANAGER_CONFIG; +use crate::state::{MANAGER_CONFIG, PAIRS}; use crate::{ helpers::{self, calculate_stableswap_y, StableSwapDirection}, state::get_pair_by_identifier, @@ -307,6 +307,17 @@ pub fn get_swap_route_creator( }) } +/// Gets the pair info for a given pair identifier. Returns a [PairInfoResponse]. +pub fn get_pair(deps: Deps, pair_identifier: String) -> Result { + let pair = PAIRS.load(deps.storage, &pair_identifier)?; + let total_share = deps.querier.query_supply(pair.lp_denom)?; + + Ok(PairInfoResponse { + pair_info: PAIRS.load(deps.storage, &pair_identifier)?, + total_share, + }) +} + // TODO: May need to remove this for a new implementation, router swap operation queries // pub fn simulate_swap_operations( // deps: Deps, diff --git a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs index fc432a8d..a127d18e 100644 --- a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs +++ b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs @@ -1824,33 +1824,44 @@ mod swapping { }); // Lets try to add liquidity - suite.provide_liquidity( - creator.clone(), - "whale-uluna".to_string(), - None, - None, - vec![ - Coin { - denom: "uwhale".to_string(), - amount: Uint128::from(1000000u128), - }, - Coin { - denom: "uluna".to_string(), - amount: Uint128::from(1000000u128), + suite + .provide_liquidity( + creator.clone(), + "whale-uluna".to_string(), + None, + None, + vec![ + Coin { + denom: "uwhale".to_string(), + amount: Uint128::from(1000000u128), + }, + Coin { + denom: "uluna".to_string(), + amount: Uint128::from(1000000u128), + }, + ], + |result| { + // Ensure we got 999_000 in the response which is 1mil less the initial liquidity amount + assert!(result.unwrap().events.iter().any(|event| { + event.attributes.iter().any(|attr| { + attr.key == "share" + && attr.value + == (Uint128::from(1_000_000u128) - MINIMUM_LIQUIDITY_AMOUNT) + .to_string() + }) + })); }, - ], - |result| { - // Ensure we got 999_000 in the response which is 1mil less the initial liquidity amount - assert!(result.unwrap().events.iter().any(|event| { - event.attributes.iter().any(|attr| { - attr.key == "share" - && attr.value - == (Uint128::from(1_000_000u128) - MINIMUM_LIQUIDITY_AMOUNT) - .to_string() - }) - })); - }, - ); + ) + .query_pair_info("whale-uluna".to_string(), |result| { + let response = result.unwrap(); + assert_eq!( + response.total_share, + Coin { + denom: response.pair_info.lp_denom, + amount: Uint128::from(1_000_000u128) + } + ); + }); let simulated_return_amount = RefCell::new(Uint128::zero()); suite.query_simulation( @@ -1925,7 +1936,6 @@ mod swapping { amount: Uint128::from(1000u128), }, |result| { - println!("{:?}", result); *simulated_offer_amount.borrow_mut() = result.unwrap().offer_amount; }, ); @@ -1953,7 +1963,6 @@ mod swapping { let mut offer_amount = String::new(); for event in result.unwrap().events { - println!("{:?}", event); if event.ty == "wasm" { for attribute in event.attributes { match attribute.key.as_str() { diff --git a/contracts/liquidity_hub/pool-manager/src/tests/suite.rs b/contracts/liquidity_hub/pool-manager/src/tests/suite.rs index 4c1f177a..236167c4 100644 --- a/contracts/liquidity_hub/pool-manager/src/tests/suite.rs +++ b/contracts/liquidity_hub/pool-manager/src/tests/suite.rs @@ -681,23 +681,6 @@ impl TestingSuite { self } - pub(crate) fn _query_lp_token(&mut self, identifier: String, _sender: String) -> String { - // Get the LP token from Config - let lp_token_response: PairInfo = self - .app - .wrap() - .query_wasm_smart( - &self.pool_manager_addr, - &white_whale_std::pool_manager::QueryMsg::Pair { - pair_identifier: identifier, - }, - ) - .unwrap(); - - // Get balance of LP token, if native we can just query balance otherwise we need to go to cw20 - lp_token_response.lp_denom - } - /// Retrieves the current configuration of the pool manager contract. pub(crate) fn query_config(&mut self) -> Config { self.app diff --git a/packages/white-whale-std/src/pool_manager.rs b/packages/white-whale-std/src/pool_manager.rs index 9fc28f10..3c45b03e 100644 --- a/packages/white-whale-std/src/pool_manager.rs +++ b/packages/white-whale-std/src/pool_manager.rs @@ -285,6 +285,7 @@ pub struct SwapRoutesResponse { #[cw_serde] pub struct PairInfoResponse { pub pair_info: PairInfo, + pub total_share: Coin, } /// The response for the `AssetDecimals` query.