Skip to content

Commit

Permalink
feat: tweak pool query on pool manager to return a list of pools if n…
Browse files Browse the repository at this point in the history
…ecessary
  • Loading branch information
kerber0x committed Jul 2, 2024
1 parent be083ad commit 1a6462c
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@
]
},
"unbonding_period": {
"description": "Unbonding period in nanoseconds. The time that needs to pass before an unbonded position can be withdrawn",
"description": "Unbonding period in epochs. The time (in epochs) that needs to pass before an unbonded position can be withdrawn",
"type": "integer",
"format": "uint64",
"minimum": 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
]
},
"unbonding_period": {
"description": "Unbonding period in nanoseconds. The time that needs to pass before an unbonded position can be withdrawn",
"description": "Unbonding period in epochs. The time (in epochs) that needs to pass before an unbonded position can be withdrawn",
"type": "integer",
"format": "uint64",
"minimum": 0.0
Expand Down
48 changes: 29 additions & 19 deletions contracts/liquidity_hub/bonding-manager/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use white_whale_std::bonding_manager::{
use white_whale_std::constants::LP_SYMBOL;
use white_whale_std::epoch_manager::epoch_manager::EpochResponse;
use white_whale_std::pool_manager::{
PoolInfoResponse, SimulateSwapOperationsResponse, SwapRouteResponse,
PoolsResponse, SimulateSwapOperationsResponse, SwapRouteResponse,
};
use white_whale_std::pool_network::asset;
use white_whale_std::pool_network::asset::aggregate_coins;
Expand Down Expand Up @@ -128,14 +128,16 @@ pub fn handle_lp_tokens_rewards(
extract_pool_identifier(&lp_token.denom).ok_or(ContractError::AssetMismatch)?;

// make sure a pool with the given identifier exists
let pool: StdResult<PoolInfoResponse> = deps.querier.query_wasm_smart(
let pools_response: StdResult<PoolsResponse> = deps.querier.query_wasm_smart(
config.pool_manager_addr.to_string(),
&white_whale_std::pool_manager::QueryMsg::Pool {
pool_identifier: pool_identifier.to_string(),
&white_whale_std::pool_manager::QueryMsg::Pools {
pool_identifier: Some(pool_identifier.to_string()),
start_after: None,
limit: None,
},
);

if pool.is_err() {
if pools_response.is_err() || pools_response?.pools.is_empty() {
continue;
}

Expand Down Expand Up @@ -221,25 +223,33 @@ pub fn swap_coins_to_main_token(
// check if the pool has any assets, if not skip the swap
// Note we are only checking the first operation here.
// Might be better to another loop to check all operations
let pool_query = white_whale_std::pool_manager::QueryMsg::Pool {
pool_identifier: swap_routes
.swap_route
.swap_operations
.first()
.unwrap()
.get_pool_identifer(),
let pools_query = white_whale_std::pool_manager::QueryMsg::Pools {
pool_identifier: Some(
swap_routes
.swap_route
.swap_operations
.first()
.unwrap()
.get_pool_identifer(),
),
start_after: None,
limit: None,
};
let mut skip_swap = false;
// Query for the pool to check if it has any assets
let resp: PoolInfoResponse = deps
let pools_response: PoolsResponse = deps
.querier
.query_wasm_smart(config.pool_manager_addr.to_string(), &pool_query)?;
.query_wasm_smart(config.pool_manager_addr.to_string(), &pools_query)?;
// Check pair 'assets' and if either one has 0 amount then don't do swaps
resp.pool_info.assets.iter().for_each(|asset| {
if asset.amount.is_zero() {
skip_swap = true;
}
});
pools_response.pools[0]
.pool_info
.assets
.iter()
.for_each(|asset| {
if asset.amount.is_zero() {
skip_swap = true;
}
});

let simulate_swap_operations_response: SimulateSwapOperationsResponse =
deps.querier.query_wasm_smart(
Expand Down
15 changes: 5 additions & 10 deletions contracts/liquidity_hub/pool-manager/schema/pool-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@
"additionalProperties": false
},
{
"description": "Retrieves the pools information.",
"description": "Retrieves the pool information for the given pool identifier.",
"type": "object",
"required": [
"pools"
Expand All @@ -975,7 +975,7 @@
"type": "object",
"properties": {
"limit": {
"description": "An optional parameter specifying the maximum number of pools to return.",
"description": "The amount of pools to return. If unspecified, will default to a value specified by the contract.",
"type": [
"integer",
"null"
Expand All @@ -984,14 +984,14 @@
"minimum": 0.0
},
"pool_identifier": {
"description": "If provided, the query will return the pool with the given identifier, otherwise, it will return all pools.",
"description": "An optional parameter specifying the pool identifier to do the query for. If not provided, it will return all pools based on the pagination parameters.",
"type": [
"string",
"null"
]
},
"start_after": {
"description": "An optional parameter specifying what pool identifier to start searching after.",
"description": "An optional parameter specifying what pool (identifier) to start searching after.",
"type": [
"string",
"null"
Expand Down Expand Up @@ -1356,7 +1356,7 @@
],
"properties": {
"pools": {
"description": "The pools on the contract.",
"description": "The pools information responses.",
"type": "array",
"items": {
"$ref": "#/definitions/PoolInfoResponse"
Expand Down Expand Up @@ -1447,7 +1447,6 @@
"asset_decimals",
"asset_denoms",
"assets",
"identifier",
"lp_denom",
"pool_fees",
"pool_type"
Expand Down Expand Up @@ -1476,9 +1475,6 @@
"$ref": "#/definitions/Coin"
}
},
"identifier": {
"type": "string"
},
"lp_denom": {
"description": "The LP denom of the pool.",
"type": "string"
Expand All @@ -1503,7 +1499,6 @@
"additionalProperties": false
},
"PoolInfoResponse": {
"description": "The response for the `Pool` query.",
"type": "object",
"required": [
"pool_info",
Expand Down
8 changes: 4 additions & 4 deletions contracts/liquidity_hub/pool-manager/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"additionalProperties": false
},
{
"description": "Retrieves the pools information.",
"description": "Retrieves the pool information for the given pool identifier.",
"type": "object",
"required": [
"pools"
Expand All @@ -227,7 +227,7 @@
"type": "object",
"properties": {
"limit": {
"description": "An optional parameter specifying the maximum number of pools to return.",
"description": "The amount of pools to return. If unspecified, will default to a value specified by the contract.",
"type": [
"integer",
"null"
Expand All @@ -236,14 +236,14 @@
"minimum": 0.0
},
"pool_identifier": {
"description": "If provided, the query will return the pool with the given identifier, otherwise, it will return all pools.",
"description": "An optional parameter specifying the pool identifier to do the query for. If not provided, it will return all pools based on the pagination parameters.",
"type": [
"string",
"null"
]
},
"start_after": {
"description": "An optional parameter specifying what pool identifier to start searching after.",
"description": "An optional parameter specifying what pool (identifier) to start searching after.",
"type": [
"string",
"null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"properties": {
"pools": {
"description": "The pools on the contract.",
"description": "The pools information responses.",
"type": "array",
"items": {
"$ref": "#/definitions/PoolInfoResponse"
Expand Down Expand Up @@ -99,7 +99,6 @@
"asset_decimals",
"asset_denoms",
"assets",
"identifier",
"lp_denom",
"pool_fees",
"pool_type"
Expand Down Expand Up @@ -128,9 +127,6 @@
"$ref": "#/definitions/Coin"
}
},
"identifier": {
"type": "string"
},
"lp_denom": {
"description": "The LP denom of the pool.",
"type": "string"
Expand All @@ -155,7 +151,6 @@
"additionalProperties": false
},
"PoolInfoResponse": {
"description": "The response for the `Pool` query.",
"type": "object",
"required": [
"pool_info",
Expand Down
13 changes: 10 additions & 3 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,16 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
)?)?),
QueryMsg::SwapRoutes => Ok(to_json_binary(&queries::get_swap_routes(deps)?)?),
QueryMsg::Ownership {} => Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?),
QueryMsg::Pool { pool_identifier } => {
Ok(to_json_binary(&queries::get_pool(deps, pool_identifier)?)?)
}
QueryMsg::Pools {
pool_identifier,
start_after,
limit,
} => Ok(to_json_binary(&queries::get_pools(
deps,
pool_identifier,
start_after,
limit,
)?)?),
QueryMsg::SwapRouteCreator {
offer_asset_denom,
ask_asset_denom,
Expand Down
64 changes: 57 additions & 7 deletions contracts/liquidity_hub/pool-manager/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::cmp::Ordering;
use cosmwasm_std::{
coin, ensure, Coin, Decimal256, Deps, Fraction, Order, StdResult, Uint128, Uint256,
};
use cw_storage_plus::Bound;

use white_whale_std::pool_manager::{
AssetDecimalsResponse, Config, PoolInfoResponse, PoolType, ReverseSimulationResponse,
SimulateSwapOperationsResponse, SimulationResponse, SwapOperation, SwapRoute,
SwapRouteCreatorResponse, SwapRouteResponse, SwapRoutesResponse,
AssetDecimalsResponse, Config, PoolInfoResponse, PoolType, PoolsResponse,
ReverseSimulationResponse, SimulateSwapOperationsResponse, SimulationResponse, SwapOperation,
SwapRoute, SwapRouteCreatorResponse, SwapRouteResponse, SwapRoutesResponse,
};

use crate::helpers::get_asset_indexes_in_pool;
Expand Down Expand Up @@ -287,13 +288,62 @@ pub fn get_swap_route_creator(
})
}

// settings for pagination
pub(crate) const MAX_LIMIT: u32 = 100;
const DEFAULT_LIMIT: u32 = 10;

/// Gets the pools in the contract. Returns a [PoolsResponse].
pub fn get_pools(
deps: Deps,
pool_identifier: Option<String>,
start_after: Option<String>,
limit: Option<u32>,
) -> Result<PoolsResponse, ContractError> {
let pools = if let Some(pool_identifier) = pool_identifier {
vec![get_pool(deps, pool_identifier)?]
} else {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = cw_utils::calc_range_start_string(start_after).map(Bound::ExclusiveRaw);

POOLS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (_, pool) = item?;
let total_share = deps.querier.query_supply(&pool.lp_denom)?;

Ok(PoolInfoResponse {
pool_info: pool,
total_share,
})
})
.collect::<StdResult<Vec<PoolInfoResponse>>>()?

// let pool_infos = POOLS
// .range(deps.storage, start, None, Order::Ascending)
// .take(limit)
// .map(|item| {
// let (_, pool) = item?;
//
// Ok(pool)
// })
// .collect::<StdResult<Vec<PoolInfo>>>()?;
//
// for pool in pool_infos.iter() {
// let total_share = deps.querier.query_supply(&pool.lp_denom)?;
// }
};

Ok(PoolsResponse { pools })
}

/// Gets the pool info for a given pool identifier. Returns a [PoolInfoResponse].
pub fn get_pool(deps: Deps, pool_identifier: String) -> Result<PoolInfoResponse, ContractError> {
let pool = POOLS.load(deps.storage, &pool_identifier)?;
let total_share = deps.querier.query_supply(pool.lp_denom)?;
fn get_pool(deps: Deps, pool_identifier: String) -> Result<PoolInfoResponse, ContractError> {
let pool_info = POOLS.load(deps.storage, &pool_identifier)?;
let total_share = deps.querier.query_supply(&pool_info.lp_denom)?;

Ok(PoolInfoResponse {
pool_info: POOLS.load(deps.storage, &pool_identifier)?,
pool_info,
total_share,
})
}
Expand Down
Loading

0 comments on commit 1a6462c

Please sign in to comment.