Skip to content

Commit

Permalink
Add max allowed swap percentage to filter out some small pools
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQZQ committed Aug 7, 2024
1 parent a6e1e2d commit 6157e05
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-pigs-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thalalabs/router-sdk": minor
---

Add max allowed swap percentage to filter out some small pools
2 changes: 1 addition & 1 deletion packages/thalaswap-router/src/PoolDataClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion packages/thalaswap-router/src/ThalaswapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -154,6 +163,8 @@ class ThalaswapRouter {
endToken,
amountIn,
maxHops,
this.options.maxAllowedSwapPercentage ??
DEFAULT_MAX_ALLOWED_SWAP_PERCENTAGE,
);
}

Expand All @@ -176,6 +187,8 @@ class ThalaswapRouter {
endToken,
amountOut,
maxHops,
this.options.maxAllowedSwapPercentage ??
DEFAULT_MAX_ALLOWED_SWAP_PERCENTAGE,
);
}

Expand Down
14 changes: 14 additions & 0 deletions packages/thalaswap-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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]!,
Expand Down

0 comments on commit 6157e05

Please sign in to comment.