From 65b36c67c09bc8d6c264320d28f8986806dc851c Mon Sep 17 00:00:00 2001 From: jinoosss Date: Fri, 1 Dec 2023 11:47:24 +0900 Subject: [PATCH] [GSW-593] feat: Change swap router key generation --- .../swap/swap-router-repository-impl.ts | 4 +- .../web/src/utils/swap-route-utils.spec.ts | 149 ++++++++++++++++++ packages/web/src/utils/swap-route-utils.ts | 16 +- 3 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 packages/web/src/utils/swap-route-utils.spec.ts diff --git a/packages/web/src/repositories/swap/swap-router-repository-impl.ts b/packages/web/src/repositories/swap/swap-router-repository-impl.ts index 2d81fa119..0424df9f6 100644 --- a/packages/web/src/repositories/swap/swap-router-repository-impl.ts +++ b/packages/web/src/repositories/swap/swap-router-repository-impl.ts @@ -44,7 +44,7 @@ export class SwapRouterRepositoryImpl implements SwapRouterRepository { const swapRouter = new SwapRouter(pools); const estimatedRoutes = swapRouter.estimateSwapRoute(inputTokenPath, outputTokenPath, BigInt(tokenAmount), exactType); - const routesQuery = makeRoutesQuery(estimatedRoutes); + const routesQuery = makeRoutesQuery(estimatedRoutes, inputToken.path); const quotes = estimatedRoutes.map(route => route.quote).join(","); const param = makeABCIParams("DrySwapRoute", [ inputTokenPath, @@ -86,7 +86,7 @@ export class SwapRouterRepositoryImpl implements SwapRouterRepository { estimatedRoutes, tokenAmountLimit } = request; - const routesQuery = makeRoutesQuery(estimatedRoutes); + const routesQuery = makeRoutesQuery(estimatedRoutes, inputToken.path); const quotes = estimatedRoutes.map(route => route.quote).join(","); const response = await this.walletClient.sendTransaction({ messages: [ diff --git a/packages/web/src/utils/swap-route-utils.spec.ts b/packages/web/src/utils/swap-route-utils.spec.ts new file mode 100644 index 000000000..7228dd205 --- /dev/null +++ b/packages/web/src/utils/swap-route-utils.spec.ts @@ -0,0 +1,149 @@ +import { EstimatedRoute } from "@gnoswap-labs/swap-router"; +import { makeRoutesQuery } from "./swap-route-utils"; + +const estimatedRoutes: EstimatedRoute[] = [ + { + routeKey: + "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500", + pools: [ + { + poolPath: "gno.land/r/bar:gno.land/r/baz:500", + tokenAPath: "gno.land/r/bar", + tokenBPath: "gno.land/r/baz", + fee: 500, + tokenABalance: 49660n, + tokenBBalance: 99999n, + tickSpacing: 10, + maxLiquidityPerTick: 103951672670308, + price: 1.1912357939484004, + sqrtPriceX96: 112428146980036105194050682880n, + tick: 6999, + feeProtocol: 0, + tokenAProtocolFee: 0, + tokenBProtocolFee: 0, + liquidity: 740557n, + ticks: [5000, 9000], + tickBitmaps: {}, + positions: [ + { + liquidity: 740557n, + owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx", + tickLower: 5000, + tickUpper: 9000, + tokenAOwed: 0n, + tokenBOwed: 0n, + }, + ], + }, + { + poolPath: "gno.land/r/baz:gno.land/r/qux:500", + tokenAPath: "gno.land/r/baz", + tokenBPath: "gno.land/r/qux", + fee: 500, + tokenABalance: 49660n, + tokenBBalance: 99999n, + tickSpacing: 10, + maxLiquidityPerTick: 103951672670308, + price: 1.1912357939484004, + sqrtPriceX96: 112428146980036105194050682880n, + tick: 6999, + feeProtocol: 0, + tokenAProtocolFee: 0, + tokenBProtocolFee: 0, + liquidity: 740557n, + ticks: [5000, 9000], + tickBitmaps: {}, + positions: [ + { + liquidity: 740557n, + owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx", + tickLower: 5000, + tickUpper: 9000, + tokenAOwed: 0n, + tokenBOwed: 0n, + }, + ], + }, + ], + quote: 95, + amountIn: 9500n, + amountOut: 36480n, + }, + { + routeKey: + "gno.land/r/bar:gno.land/r/foo:500*POOL*gno.land/r/foo:gno.land/r/qux:500", + pools: [ + { + poolPath: "gno.land/r/bar:gno.land/r/foo:500", + tokenAPath: "gno.land/r/bar", + tokenBPath: "gno.land/r/foo", + fee: 500, + tokenABalance: 1839n, + tokenBBalance: 4999n, + tickSpacing: 10, + maxLiquidityPerTick: 103951672670308, + price: 1.2840093675402746, + sqrtPriceX96: 130621891405341616875390369792n, + tick: 9999, + feeProtocol: 0, + tokenAProtocolFee: 0, + tokenBProtocolFee: 0, + liquidity: 31870n, + ticks: [8000, 12000], + tickBitmaps: {}, + positions: [ + { + liquidity: 31870n, + owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx", + tickLower: 8000, + tickUpper: 12000, + tokenAOwed: 0n, + tokenBOwed: 0n, + }, + ], + }, + { + poolPath: "gno.land/r/foo:gno.land/r/qux:500", + tokenAPath: "gno.land/r/foo", + tokenBPath: "gno.land/r/qux", + fee: 500, + tokenABalance: 1839n, + tokenBBalance: 4999n, + tickSpacing: 10, + maxLiquidityPerTick: 103951672670308, + price: 1.2840093675402746, + sqrtPriceX96: 130621891405341616875390369792n, + tick: 9999, + feeProtocol: 0, + tokenAProtocolFee: 0, + tokenBProtocolFee: 0, + liquidity: 31870n, + ticks: [8000, 12000], + tickBitmaps: {}, + positions: [ + { + liquidity: 31870n, + owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx", + tickLower: 8000, + tickUpper: 12000, + tokenAOwed: 0n, + tokenBOwed: 0n, + }, + ], + }, + ], + quote: 5, + amountIn: 500n, + amountOut: 3360n, + }, +]; + +describe("make swap route query", () => { + test("2 routes and 2 pools", async () => { + const routeKey = makeRoutesQuery(estimatedRoutes, "gno.land/r/bar"); + + expect(routeKey).toBe( + "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500,gno.land/r/bar:gno.land/r/foo:500*POOL*gno.land/r/foo:gno.land/r/qux:500", + ); + }); +}); diff --git a/packages/web/src/utils/swap-route-utils.ts b/packages/web/src/utils/swap-route-utils.ts index 9a867771c..4c6792a68 100644 --- a/packages/web/src/utils/swap-route-utils.ts +++ b/packages/web/src/utils/swap-route-utils.ts @@ -1,8 +1,20 @@ import { EstimatedRoute } from "@gnoswap-labs/swap-router"; -export function makeRoutesQuery(routes: EstimatedRoute[]) { +export function makeRoutesQuery(routes: EstimatedRoute[], fromPath: string) { const POOL_DIVIDER = "*POOL*"; return routes - .map(route => route.pools.map(pool => pool.poolPath).join(POOL_DIVIDER)) + .map(route => { + let curreentFromPath = fromPath; + return route.pools + .map(pool => { + const { tokenAPath, tokenBPath, fee } = pool; + const ordered = curreentFromPath === tokenAPath; + const inputTokenPath = ordered ? tokenAPath : tokenBPath; + const outputTokenPath = ordered ? tokenBPath : tokenAPath; + curreentFromPath = outputTokenPath; + return `${inputTokenPath}:${outputTokenPath}:${fee}`; + }) + .join(POOL_DIVIDER); + }) .join(","); }