Skip to content

Commit

Permalink
feat(smart-contracts): use response structs on all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
nseguias committed Apr 16, 2024
1 parent 3a605e4 commit f55194e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
8 changes: 4 additions & 4 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmwasm_std::{
use cw2::set_contract_version;
use semver::Version;
use white_whale_std::pool_manager::{
ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, QueryMsg,
ExecuteMsg, FeatureToggle, InstantiateMsg, MigrateMsg, PairInfoResponse, QueryMsg,
};

// version info for migration info
Expand Down Expand Up @@ -236,9 +236,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(
&PAIRS.load(deps.storage, &pair_identifier)?,
)?),
QueryMsg::Pair { pair_identifier } => Ok(to_json_binary(&PairInfoResponse {
pair_info: PAIRS.load(deps.storage, &pair_identifier)?,
})?),
}
}

Expand Down
38 changes: 23 additions & 15 deletions contracts/liquidity_hub/pool-manager/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::Ordering;
use cosmwasm_std::{Coin, Decimal256, Deps, Env, Fraction, Order, StdResult, Uint128};

use white_whale_std::pool_manager::{
AssetDecimalsResponse, Config, SwapOperation, SwapRouteResponse,
AssetDecimalsResponse, Config, SwapRoute, SwapRouteResponse, SwapRoutesResponse,
};
use white_whale_std::pool_network::{
asset::PairType,
Expand Down Expand Up @@ -245,40 +245,48 @@ pub fn query_reverse_simulation(
// Router related queries, swap routes and SwapOperations
// get_swap_routes which only takes deps: Deps as input
// the function will read from SWAP_ROUTES and return all swpa routes in a vec
pub fn get_swap_routes(deps: Deps) -> Result<Vec<SwapRouteResponse>, ContractError> {
let swap_routes: Vec<SwapRouteResponse> = SWAP_ROUTES
pub fn get_swap_routes(deps: Deps) -> Result<SwapRoutesResponse, ContractError> {
let swap_routes: Vec<SwapRoute> = SWAP_ROUTES
.range(deps.storage, None, None, Order::Ascending)
.map(|item| {
let swap_info = item?;
// Destructure key into (offer_asset, ask_asset)
let (offer_asset_denom, ask_asset_denom) = swap_info.0;
// Destructure value into vec of SwapOperation
let swap_route = swap_info.1;
let swap_operations = swap_info.1;

Ok(SwapRouteResponse {
Ok(SwapRoute {
offer_asset_denom,
ask_asset_denom,
swap_route,
swap_operations,
})
})
.collect::<StdResult<Vec<SwapRouteResponse>>>()?;
.collect::<StdResult<Vec<SwapRoute>>>()?;

Ok(swap_routes)
Ok(SwapRoutesResponse { swap_routes })
}

pub fn get_swap_route(
deps: Deps,
offer_asset_denom: String,
ask_asset_denom: String,
) -> Result<Vec<SwapOperation>, ContractError> {
) -> Result<SwapRouteResponse, ContractError> {
let swap_route_key = SWAP_ROUTES.key((&offer_asset_denom, &ask_asset_denom));

swap_route_key
.load(deps.storage)
.map_err(|_| ContractError::NoSwapRouteForAssets {
offer_asset: offer_asset_denom,
ask_asset: ask_asset_denom,
})
let swap_operations =
swap_route_key
.load(deps.storage)
.map_err(|_| ContractError::NoSwapRouteForAssets {
offer_asset: offer_asset_denom.clone(),
ask_asset: ask_asset_denom.clone(),
})?;
Ok(SwapRouteResponse {
swap_route: SwapRoute {
offer_asset_denom,
ask_asset_denom,
swap_operations,
},
})
}

// TODO: May need to remove this for a new implementation, router swap operation queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ mod swapping {
}

mod ownership {
use white_whale_std::pool_network::pair::FeatureToggle;
use white_whale_std::pool_manager::FeatureToggle;

use super::*;

Expand Down
6 changes: 2 additions & 4 deletions contracts/liquidity_hub/pool-manager/src/tests/suite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::testing::MockStorage;
use white_whale_std::pool_manager::{Config, SwapOperation};
use white_whale_std::pool_manager::{Config, FeatureToggle, SwapOperation};
use white_whale_std::pool_manager::{InstantiateMsg, PairInfo};

use cosmwasm_std::{coin, Addr, Coin, Decimal, Empty, StdResult, Timestamp, Uint128, Uint64};
Expand All @@ -9,9 +9,7 @@ use cw_multi_test::{
};
use white_whale_std::fee::PoolFee;
use white_whale_std::pool_network::asset::{AssetInfo, PairType};
use white_whale_std::pool_network::pair::{
FeatureToggle, ReverseSimulationResponse, SimulationResponse,
};
use white_whale_std::pool_network::pair::{ReverseSimulationResponse, SimulationResponse};
use white_whale_testing::multi_test::stargate_mock::StargateMock;

use cw_multi_test::addons::{MockAddressGenerator, MockApiBech32};
Expand Down
27 changes: 20 additions & 7 deletions packages/white-whale-std/src/pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ pub struct SwapRoute {
// Used for all swap routes
#[cw_serde]
pub struct SwapRouteResponse {
pub offer_asset_denom: String,
pub ask_asset_denom: String,
pub swap_route: Vec<SwapOperation>,
pub swap_route: SwapRoute,
}

impl fmt::Display for SwapRoute {
Expand Down Expand Up @@ -204,7 +202,7 @@ pub enum ExecuteMsg {
#[derive(QueryResponses)]
pub enum QueryMsg {
/// Retrieves the contract's config.
#[returns(Config)]
#[returns(ConfigResponse)]
Config {},

/// Retrieves the decimals for the given asset.
Expand All @@ -231,13 +229,13 @@ pub enum QueryMsg {
},

/// Gets the swap route for the given offer and ask assets.
#[returns(Vec<SwapOperation>)]
#[returns(SwapRouteResponse)]
SwapRoute {
offer_asset_denom: String,
ask_asset_denom: String,
},
/// Gets all swap routes registered
#[returns(Vec<SwapRouteResponse>)]
#[returns(SwapRoutesResponse)]
SwapRoutes {},

// /// Simulates swap operations.
Expand All @@ -253,10 +251,25 @@ pub enum QueryMsg {
// ask_amount: Uint128,
// operations: Vec<SwapOperation>,
// },
#[returns(PairInfo)]
#[returns(PairInfoResponse)]
Pair { pair_identifier: String },
}

#[cw_serde]
pub struct ConfigResponse {
pub config: Config,
}

#[cw_serde]
pub struct SwapRoutesResponse {
pub swap_routes: Vec<SwapRoute>,
}

#[cw_serde]
pub struct PairInfoResponse {
pub pair_info: PairInfo,
}

/// The response for the `AssetDecimals` query.
#[cw_serde]
pub struct AssetDecimalsResponse {
Expand Down

0 comments on commit f55194e

Please sign in to comment.