Skip to content

Commit

Permalink
feat(pool_router): add router to pool-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x authored Mar 25, 2024
2 parents f03af56 + e59999f commit 06b1037
Show file tree
Hide file tree
Showing 19 changed files with 1,586 additions and 782 deletions.
55 changes: 55 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/pool-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,61 @@
},
"additionalProperties": false
},
{
"description": "Execute multiple [`SwapOperations`] to allow for multi-hop swaps.",
"type": "object",
"required": [
"execute_swap_operations"
],
"properties": {
"execute_swap_operations": {
"type": "object",
"required": [
"operations"
],
"properties": {
"max_spread": {
"description": "The (optional) maximum spread to incur when performing any swap.\n\nIf left unspecified, there is no limit to what spread the transaction can incur.",
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"minimum_receive": {
"description": "The minimum amount of the output (i.e., final swap operation token) required for the message to succeed.",
"anyOf": [
{
"$ref": "#/definitions/Uint128"
},
{
"type": "null"
}
]
},
"operations": {
"description": "The operations that should be performed in sequence.\n\nThe amount in each swap will be the output from the previous swap.\n\nThe first swap will use whatever funds are sent in the [`MessageInfo`].",
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
},
"to": {
"description": "The (optional) recipient of the output tokens.\n\nIf left unspecified, tokens will be sent to the sender of the message.",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Adds swap routes to the router.",
"type": "object",
Expand Down
55 changes: 55 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,61 @@
},
"additionalProperties": false
},
{
"description": "Execute multiple [`SwapOperations`] to allow for multi-hop swaps.",
"type": "object",
"required": [
"execute_swap_operations"
],
"properties": {
"execute_swap_operations": {
"type": "object",
"required": [
"operations"
],
"properties": {
"max_spread": {
"description": "The (optional) maximum spread to incur when performing any swap.\n\nIf left unspecified, there is no limit to what spread the transaction can incur.",
"anyOf": [
{
"$ref": "#/definitions/Decimal"
},
{
"type": "null"
}
]
},
"minimum_receive": {
"description": "The minimum amount of the output (i.e., final swap operation token) required for the message to succeed.",
"anyOf": [
{
"$ref": "#/definitions/Uint128"
},
{
"type": "null"
}
]
},
"operations": {
"description": "The operations that should be performed in sequence.\n\nThe amount in each swap will be the output from the previous swap.\n\nThe first swap will use whatever funds are sent in the [`MessageInfo`].",
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
},
"to": {
"description": "The (optional) recipient of the output tokens.\n\nIf left unspecified, tokens will be sent to the sender of the message.",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Adds swap routes to the router.",
"type": "object",
Expand Down
46 changes: 18 additions & 28 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::ContractError;
use crate::queries::{get_swap_route, get_swap_routes};
use crate::state::{Config, MANAGER_CONFIG, PAIRS, PAIR_COUNTER};
use crate::{liquidity, manager, queries, swap};
use crate::{liquidity, manager, queries, router, swap};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
Expand Down Expand Up @@ -94,21 +94,12 @@ pub fn execute(
to,
pair_identifier,
} => {
// check if the swap feature is enabled
let feature_toggle: FeatureToggle = MANAGER_CONFIG.load(deps.storage)?.feature_toggle;
if !feature_toggle.swaps_enabled {
return Err(ContractError::OperationDisabled("swap".to_string()));
}

if !offer_asset.is_native_token() {
return Err(ContractError::Unauthorized {});
}

let to_addr = if let Some(to_addr) = to {
Some(deps.api.addr_validate(&to_addr)?)
} else {
None
};

swap::commands::swap(
deps,
env,
Expand Down Expand Up @@ -157,23 +148,22 @@ pub fn execute(
// )

// },
// ExecuteMsg::ExecuteSwapOperations {
// operations,
// minimum_receive,
// to,
// max_spread,
// } => {
// let api = deps.api;
// router::commands::execute_swap_operations(
// deps,
// env,
// info.sender,
// operations,
// minimum_receive,
// optional_addr_validate(api, to)?,
// max_spread,
// )
// }
ExecuteMsg::ExecuteSwapOperations {
operations,
minimum_receive,
to,
max_spread,
} => {
let api = deps.api;
router::commands::execute_swap_operations(
deps,
info,
operations,
minimum_receive,
optional_addr_validate(api, to)?,
max_spread,
)
}
// ExecuteMsg::ExecuteSwapOperation {
// operation,
// to,
Expand Down
14 changes: 11 additions & 3 deletions contracts/liquidity_hub/pool-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cw_ownable::OwnershipError;
use cw_utils::PaymentError;
use semver::Version;
use thiserror::Error;
use white_whale_std::pool_network::asset::AssetInfo;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
Expand Down Expand Up @@ -36,9 +37,6 @@ pub enum ContractError {
#[error("The provided assets are both the same")]
SameAsset {},

#[error("Invalid operations; multiple output token")]
MultipleOutputToken {},

#[error("Attempt to migrate to version {new_version}, but contract is on a higher version {current_version}")]
MigrateInvalidVersion {
new_version: Version,
Expand Down Expand Up @@ -135,11 +133,21 @@ pub enum ContractError {

#[error("Must provide swap operations to execute")]
NoSwapOperationsProvided {},

#[error("Attempt to perform non-consecutive swap operation from previous output of {previous_output} to next input of {next_input}")]
NonConsecutiveSwapOperations {
previous_output: AssetInfo,
next_input: AssetInfo,
},

#[error("Invalid pair creation fee, expected {expected} got {amount}")]
InvalidPairCreationFee {
amount: cosmwasm_std::Uint128,
expected: cosmwasm_std::Uint128,
},

#[error("Funds for {denom} were missing when performing swap")]
MissingNativeSwapFunds { denom: String },
}

impl From<semver::Error> for ContractError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ pub fn withdraw_liquidity(
pair_identifier: String,
) -> Result<Response, ContractError> {
let config = MANAGER_CONFIG.load(deps.storage)?;
// check if the deposit feature is enabled
if !config.feature_toggle.deposits_enabled {
// check if the withdraw feature is enabled
if !config.feature_toggle.withdrawals_enabled {
return Err(ContractError::OperationDisabled(
"provide_liquidity".to_string(),
"withdraw_liquidity".to_string(),
));
}
// Get the pair by the pair_identifier
Expand Down
Loading

0 comments on commit 06b1037

Please sign in to comment.