Skip to content

Commit

Permalink
chore: add total LP share amount to the Pair query response
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed Apr 29, 2024
1 parent 7764c16 commit 357d82f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 54 deletions.
12 changes: 6 additions & 6 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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
Expand Down Expand Up @@ -244,9 +244,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
)?)?),
QueryMsg::SwapRoutes {} => 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,
Expand Down
17 changes: 14 additions & 3 deletions contracts/liquidity_hub/pool-manager/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ 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,
pair::{ReverseSimulationResponse, SimulationResponse},
// 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,
Expand Down Expand Up @@ -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<PairInfoResponse, ContractError> {
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,
Expand Down
65 changes: 37 additions & 28 deletions contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1925,7 +1936,6 @@ mod swapping {
amount: Uint128::from(1000u128),
},
|result| {
println!("{:?}", result);
*simulated_offer_amount.borrow_mut() = result.unwrap().offer_amount;
},
);
Expand Down Expand Up @@ -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() {
Expand Down
17 changes: 0 additions & 17 deletions contracts/liquidity_hub/pool-manager/src/tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/white-whale-std/src/pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 357d82f

Please sign in to comment.