From 6157e0568eaa5c35f9a9c47b3636812e2ac61ed6 Mon Sep 17 00:00:00 2001 From: SamuelQZQ Date: Wed, 7 Aug 2024 12:26:28 +0800 Subject: [PATCH] Add max allowed swap percentage to filter out some small pools --- .changeset/lazy-pigs-greet.md | 5 +++++ packages/thalaswap-router/src/PoolDataClient.ts | 2 +- packages/thalaswap-router/src/ThalaswapRouter.ts | 15 ++++++++++++++- packages/thalaswap-router/src/router.ts | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .changeset/lazy-pigs-greet.md diff --git a/.changeset/lazy-pigs-greet.md b/.changeset/lazy-pigs-greet.md new file mode 100644 index 0000000..e633a22 --- /dev/null +++ b/.changeset/lazy-pigs-greet.md @@ -0,0 +1,5 @@ +--- +"@thalalabs/router-sdk": minor +--- + +Add max allowed swap percentage to filter out some small pools diff --git a/packages/thalaswap-router/src/PoolDataClient.ts b/packages/thalaswap-router/src/PoolDataClient.ts index 341588d..274fd86 100644 --- a/packages/thalaswap-router/src/PoolDataClient.ts +++ b/packages/thalaswap-router/src/PoolDataClient.ts @@ -4,7 +4,7 @@ import { uniq } from "lodash"; import { fp64ToFloat, parsePoolMetadata, scaleDown } from "./utils"; class PoolDataClient { - private poolData: PoolData | null = null; + public poolData: PoolData | null = null; private lastUpdated: number = 0; private expiry = 10000; // 10 seconds private retryLimit = 3; diff --git a/packages/thalaswap-router/src/ThalaswapRouter.ts b/packages/thalaswap-router/src/ThalaswapRouter.ts index e6ee300..d03afb0 100644 --- a/packages/thalaswap-router/src/ThalaswapRouter.ts +++ b/packages/thalaswap-router/src/ThalaswapRouter.ts @@ -19,6 +19,8 @@ const encodeWeight = (weight: number, resourceAddress: string): string => { return `${resourceAddress}::weighted_pool::Weight_${Math.floor(weight * 100).toString()}`; }; +const DEFAULT_MAX_ALLOWED_SWAP_PERCENTAGE = 0.5; + // Encode the pool type arguments for a given pool // If extendStableArgs is true, then the stable pool type arguments will be extended to 8 arguments (filled with additional 4 nulls) const encodePoolType = ( @@ -59,22 +61,29 @@ const scaleUp = (amount: number, decimals: number): number => { return Math.floor(amount * Math.pow(10, decimals)); }; +type Options = { + maxAllowedSwapPercentage?: number; +}; + class ThalaswapRouter { - private client: PoolDataClient; + public client: PoolDataClient; private graph: Graph | null = null; private coins: Coin[] | null = null; private resourceAddress: string; private multirouterAddress: string; + private options: Options; constructor( network: Network, fullnode: string, resourceAddress: string, multirouterAddress: string, + options?: Options, ) { this.resourceAddress = resourceAddress; this.multirouterAddress = multirouterAddress; this.client = new PoolDataClient(network, fullnode, resourceAddress); + this.options = options ?? {}; } setPoolDataClient(client: PoolDataClient) { @@ -154,6 +163,8 @@ class ThalaswapRouter { endToken, amountIn, maxHops, + this.options.maxAllowedSwapPercentage ?? + DEFAULT_MAX_ALLOWED_SWAP_PERCENTAGE, ); } @@ -176,6 +187,8 @@ class ThalaswapRouter { endToken, amountOut, maxHops, + this.options.maxAllowedSwapPercentage ?? + DEFAULT_MAX_ALLOWED_SWAP_PERCENTAGE, ); } diff --git a/packages/thalaswap-router/src/router.ts b/packages/thalaswap-router/src/router.ts index a8e4ee2..25a485a 100644 --- a/packages/thalaswap-router/src/router.ts +++ b/packages/thalaswap-router/src/router.ts @@ -119,6 +119,7 @@ export function findRouteGivenExactInput( endToken: string, amountIn: number, maxHops: number, + maxAllowedSwapPercentage: number, ): Route | null { const tokens = Object.keys(graph); // distances[token][hop] is the maximum amount of token that can be received given hop number @@ -142,6 +143,12 @@ export function findRouteGivenExactInput( if (distances[fromToken][i] === undefined) continue; // Skip unvisited nodes + if ( + distances[fromToken][i] / edge.pool.balances[edge.fromIndex] > + maxAllowedSwapPercentage + ) + continue; + const newDistance = calcOutGivenIn( distances[fromToken][i]!, edge.pool, @@ -228,6 +235,7 @@ export function findRouteGivenExactOutput( endToken: string, amountOut: number, maxHops: number, + maxAllowedSwapPercentage: number, ): Route | null { const tokens = Object.keys(graph); const distances: Distances = {}; @@ -249,6 +257,12 @@ export function findRouteGivenExactOutput( if (distances[toToken][i] === undefined) continue; // Skip unvisited nodes + if ( + distances[toToken][i] / edge.pool.balances[edge.toIndex] > + maxAllowedSwapPercentage + ) + continue; + try { const newDistance = calcInGivenOut( distances[toToken][i]!,