From e394946137c166fa712d34509f6c7ea35f8ced09 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Thu, 25 Apr 2024 18:02:40 +0700 Subject: [PATCH 1/2] fix response func handleSimulateSwap --- 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 { From e1a53a501e7672d5ceb8ceb6dc7130fc616b6a14 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Fri, 26 Apr 2024 14:10:08 +0700 Subject: [PATCH 2/2] update response handleSimulate --- packages/universal-swap/package.json | 2 +- packages/universal-swap/src/helper.ts | 18 +++++++++--------- packages/universal-swap/src/types.ts | 21 ++++++++++++--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/universal-swap/package.json b/packages/universal-swap/package.json index a51988e0..f9338443 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.74", + "version": "1.0.75", "main": "build/index.js", "files": [ "build/" diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index 07230402..c45e2fcb 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -471,7 +471,7 @@ export class UniversalSwapHelper { askChainId: string, offerAmount: string ): Promise => { - const { returnAmount, routes } = await UniversalSwapHelper.querySmartRoute( + const { returnAmount, routes: routesSwap } = await UniversalSwapHelper.querySmartRoute( offerInfo, offerChainId, askInfo, @@ -479,7 +479,7 @@ export class UniversalSwapHelper { offerAmount ); - const routesSwap = routes.map((route) => { + const routes = routesSwap.map((route) => { let ops = []; let currTokenIn = offerInfo; for (let path of route.paths) { @@ -503,7 +503,8 @@ export class UniversalSwapHelper { return { swapAmount: offerAmount, returnAmount, - routes: routesSwap + routes, + routesSwap }; }; @@ -656,12 +657,11 @@ export class UniversalSwapHelper { let amount; let routes = []; if (query.useSmartRoute) { - const { returnAmount, routes: routesSwap }: SmartRouterResponse = - await UniversalSwapHelper.simulateSwapUsingSmartRoute({ - fromInfo, - toInfo, - amount: toAmount(query.originalAmount, fromInfo.decimals).toString() - }); + const { returnAmount, routesSwap }: SmartRouterResponse = await UniversalSwapHelper.simulateSwapUsingSmartRoute({ + fromInfo, + toInfo, + amount: toAmount(query.originalAmount, fromInfo.decimals).toString() + }); routes = routesSwap; amount = returnAmount; } else { diff --git a/packages/universal-swap/src/types.ts b/packages/universal-swap/src/types.ts index 12c4af66..18a1f37c 100644 --- a/packages/universal-swap/src/types.ts +++ b/packages/universal-swap/src/types.ts @@ -16,7 +16,7 @@ export enum SwapDirection { export interface SimulateResponse { amount: Uint128; displayAmount: number; - routes?: SmartRouteSwapOperations[]; + routes?: SmartRouteSwapAPIOperations[]; } export interface SwapData { @@ -116,19 +116,22 @@ export type SmartRouterResponse = { swapAmount: string; returnAmount: string; routes: SmartRouteSwapOperations[]; + routesSwap?: SmartRouteSwapAPIOperations[]; }; -export type SmartRouterResponseAPI = { +export type SmartRouteSwapAPIOperations = { swapAmount: string; returnAmount: string; - routes: { - swapAmount: string; - returnAmount: string; - paths: { - poolId: string; - tokenOut: string; - }[]; + paths: { + poolId: string; + tokenOut: string; }[]; }; +export type SmartRouterResponseAPI = { + swapAmount: string; + returnAmount: string; + routes: SmartRouteSwapAPIOperations[]; +}; + export type ConvertType = Convert | ConvertReverse;