Skip to content

Commit

Permalink
Merge pull request #346 from White-Whale-Defi-Platform/feat/enable-qu…
Browse files Browse the repository at this point in the history
…ery-reverse-simulate-swap-operations
  • Loading branch information
nseguias authored Apr 29, 2024
2 parents 75794fe + deebeee commit 7764c16
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 23 deletions.
50 changes: 50 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/pool-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@
"additionalProperties": false
},
{
"description": "Simulates swap operations.",
"type": "object",
"required": [
"simulate_swap_operations"
Expand Down Expand Up @@ -863,6 +864,35 @@
},
"additionalProperties": false
},
{
"description": "Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset is needed to perform the swap.",
"type": "object",
"required": [
"reverse_simulate_swap_operations"
],
"properties": {
"reverse_simulate_swap_operations": {
"type": "object",
"required": [
"ask_amount",
"operations"
],
"properties": {
"ask_amount": {
"$ref": "#/definitions/Uint128"
},
"operations": {
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down Expand Up @@ -1378,6 +1408,26 @@
}
}
},
"reverse_simulate_swap_operations": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulateSwapOperationsResponse",
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false,
"definitions": {
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
},
"reverse_simulation": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulationResponse",
Expand Down
30 changes: 30 additions & 0 deletions contracts/liquidity_hub/pool-manager/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"additionalProperties": false
},
{
"description": "Simulates swap operations.",
"type": "object",
"required": [
"simulate_swap_operations"
Expand Down Expand Up @@ -166,6 +167,35 @@
},
"additionalProperties": false
},
{
"description": "Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset is needed to perform the swap.",
"type": "object",
"required": [
"reverse_simulate_swap_operations"
],
"properties": {
"reverse_simulate_swap_operations": {
"type": "object",
"required": [
"ask_amount",
"operations"
],
"properties": {
"ask_amount": {
"$ref": "#/definitions/Uint128"
},
"operations": {
"type": "array",
"items": {
"$ref": "#/definitions/SwapOperation"
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ReverseSimulateSwapOperationsResponse",
"type": "object",
"required": [
"amount"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
}
},
"additionalProperties": false,
"definitions": {
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
}
14 changes: 7 additions & 7 deletions contracts/liquidity_hub/pool-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::ContractError;
use crate::helpers::simulate_swap_operations;
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::router::commands::{add_swap_routes, remove_swap_routes};
use crate::state::{Config, MANAGER_CONFIG, PAIRS, PAIR_COUNTER};
Expand Down Expand Up @@ -228,12 +228,12 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
offer_amount,
operations,
)?)?),
// QueryMsg::ReverseSimulateSwapOperations {
// ask_amount,
// operations,
// } => Ok(to_binary(&queries::reverse_simulate_swap_operations(
// deps, env, ask_amount, operations,
// )?)?),
QueryMsg::ReverseSimulateSwapOperations {
ask_amount,
operations,
} => Ok(to_json_binary(&reverse_simulate_swap_operations(
deps, ask_amount, operations,
)?)?),
QueryMsg::SwapRoute {
offer_asset_denom,
ask_asset_denom,
Expand Down
38 changes: 33 additions & 5 deletions contracts/liquidity_hub/pool-manager/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,39 @@ pub fn simulate_swap_operations(
token_out_denom: _,
pool_identifier,
} => {
let res = query_simulation(
deps,
coin(offer_amount.u128(), token_in_denom),
pool_identifier,
)?;
let res =
query_simulation(deps, coin(amount.u128(), token_in_denom), pool_identifier)?;
amount = res.return_amount;
}
}
}

Ok(SimulateSwapOperationsResponse { amount })
}

/// This function iterates over the swap operations in the reverse order,
/// simulates each swap to get the final amount after all the swaps.
pub fn reverse_simulate_swap_operations(
deps: Deps,
ask_amount: Uint128,
operations: Vec<SwapOperation>,
) -> Result<SimulateSwapOperationsResponse, ContractError> {
let operations_len = operations.len();
if operations_len == 0 {
return Err(ContractError::NoSwapOperationsProvided {});
}

let mut amount = ask_amount;

for operation in operations.into_iter().rev() {
match operation {
SwapOperation::WhaleSwap {
token_in_denom: _,
token_out_denom,
pool_identifier,
} => {
let res =
query_simulation(deps, coin(amount.u128(), token_out_denom), pool_identifier)?;
amount = res.return_amount;
}
}
Expand Down
Loading

0 comments on commit 7764c16

Please sign in to comment.