Skip to content

Commit

Permalink
feat(pool_manager): pools query improvement (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x authored Jul 2, 2024
1 parent be083ad commit 00f346b
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
4 changes: 2 additions & 2 deletions contracts/liquidity_hub/bonding-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CONTRACT_NAME: &str = "white_whale-bonding_manager";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

pub const LP_WITHDRAWAL_REPLY_ID: u64 = 0;
pub const NEW_EPOCH_CREATION_REPLY_ID: u64 = 1;
pub const TEMPORAL_BOND_ACTION_REPLY_ID: u64 = 1;

#[entry_point]
pub fn instantiate(
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn instantiate(
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
LP_WITHDRAWAL_REPLY_ID => rewards::commands::handle_lp_withdrawal_reply(deps, msg),
NEW_EPOCH_CREATION_REPLY_ID => {
TEMPORAL_BOND_ACTION_REPLY_ID => {
let TemporalBondAction {
sender,
coin,
Expand Down
52 changes: 31 additions & 21 deletions contracts/liquidity_hub/bonding-manager/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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;

use crate::contract::{LP_WITHDRAWAL_REPLY_ID, NEW_EPOCH_CREATION_REPLY_ID};
use crate::contract::{LP_WITHDRAWAL_REPLY_ID, TEMPORAL_BOND_ACTION_REPLY_ID};
use crate::error::ContractError;
use crate::queries::query_claimable;
use crate::state::{
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 Expand Up @@ -537,6 +547,6 @@ fn create_temporal_bond_action_submsg(
) -> Result<SubMsg, ContractError> {
Ok(SubMsg::reply_on_success(
wasm_execute(contract_addr, msg, vec![])?,
NEW_EPOCH_CREATION_REPLY_ID,
TEMPORAL_BOND_ACTION_REPLY_ID,
))
}
20 changes: 10 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,9 +1447,9 @@
"asset_decimals",
"asset_denoms",
"assets",
"identifier",
"lp_denom",
"pool_fees",
"pool_identifier",
"pool_type"
],
"properties": {
Expand All @@ -1476,9 +1476,6 @@
"$ref": "#/definitions/Coin"
}
},
"identifier": {
"type": "string"
},
"lp_denom": {
"description": "The LP denom of the pool.",
"type": "string"
Expand All @@ -1491,6 +1488,10 @@
}
]
},
"pool_identifier": {
"description": "The identifier for the pool.",
"type": "string"
},
"pool_type": {
"description": "The type of pool to create.",
"allOf": [
Expand All @@ -1503,7 +1504,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,9 +99,9 @@
"asset_decimals",
"asset_denoms",
"assets",
"identifier",
"lp_denom",
"pool_fees",
"pool_identifier",
"pool_type"
],
"properties": {
Expand All @@ -128,9 +128,6 @@
"$ref": "#/definitions/Coin"
}
},
"identifier": {
"type": "string"
},
"lp_denom": {
"description": "The LP denom of the pool.",
"type": "string"
Expand All @@ -143,6 +140,10 @@
}
]
},
"pool_identifier": {
"description": "The identifier for the pool.",
"type": "string"
},
"pool_type": {
"description": "The type of pool to create.",
"allOf": [
Expand All @@ -155,7 +156,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
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub fn create_pool(
deps.storage,
&identifier,
&PoolInfo {
pool_identifier: identifier.clone(),
asset_denoms,
pool_type: pool_type.clone(),
lp_denom: lp_asset.clone(),
Expand Down
Loading

0 comments on commit 00f346b

Please sign in to comment.