From da3b68d9d7c4a492fd4e81c6e40a6fface710cb4 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van <54303398+haunv3@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:21:56 +0700 Subject: [PATCH] fix response func handleSimulateSwap (#250) --- packages/universal-swap/package.json | 2 +- packages/universal-swap/src/helper.ts | 40 +++++++++++++++------------ packages/universal-swap/src/types.ts | 1 + 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/universal-swap/package.json b/packages/universal-swap/package.json index 839cd8b3..a51988e0 100644 --- a/packages/universal-swap/package.json +++ b/packages/universal-swap/package.json @@ -1,6 +1,6 @@ { "name": "@oraichain/oraidex-universal-swap", - "version": "1.0.73", + "version": "1.0.74", "main": "build/index.js", "files": [ "build/" diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index 75cbae40..07230402 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -653,25 +653,31 @@ export class UniversalSwapHelper { throw new Error( `Cannot find token on Oraichain for token ${query.originalFromInfo.coinGeckoId} and ${query.originalToInfo.coinGeckoId}` ); - const amount = query.useSmartRoute - ? ( - await UniversalSwapHelper.simulateSwapUsingSmartRoute({ - fromInfo, - toInfo, - amount: toAmount(query.originalAmount, fromInfo.decimals).toString() - }) - ).returnAmount - : ( - await UniversalSwapHelper.simulateSwap({ - fromInfo, - toInfo, - amount: toAmount(query.originalAmount, fromInfo.decimals).toString(), - routerClient: query.routerClient - }) - ).amount; + let amount; + let routes = []; + if (query.useSmartRoute) { + const { returnAmount, routes: routesSwap }: SmartRouterResponse = + await UniversalSwapHelper.simulateSwapUsingSmartRoute({ + fromInfo, + toInfo, + amount: toAmount(query.originalAmount, fromInfo.decimals).toString() + }); + routes = routesSwap; + amount = returnAmount; + } else { + amount = ( + await UniversalSwapHelper.simulateSwap({ + fromInfo, + toInfo, + amount: toAmount(query.originalAmount, fromInfo.decimals).toString(), + routerClient: query.routerClient + }) + ).amount; + } return { amount, - displayAmount: toDisplay(amount, getTokenOnOraichain(toInfo.coinGeckoId)?.decimals) + displayAmount: toDisplay(amount, getTokenOnOraichain(toInfo.coinGeckoId)?.decimals), + routes }; }; diff --git a/packages/universal-swap/src/types.ts b/packages/universal-swap/src/types.ts index 67e0ffdd..12c4af66 100644 --- a/packages/universal-swap/src/types.ts +++ b/packages/universal-swap/src/types.ts @@ -16,6 +16,7 @@ export enum SwapDirection { export interface SimulateResponse { amount: Uint128; displayAmount: number; + routes?: SmartRouteSwapOperations[]; } export interface SwapData {