From 5a22d9b117a071718f297bfa29b2e9a5ee14ece4 Mon Sep 17 00:00:00 2001 From: viet-nv Date: Tue, 1 Oct 2024 14:17:34 +0700 Subject: [PATCH] feat: update PI response call from api --- src/components/SwapForm/hooks/useGetRoute.ts | 3 ++ src/services/route/index.ts | 47 ++++++++++++++++++++ src/services/route/types/getRoute.ts | 4 ++ 3 files changed, 54 insertions(+) diff --git a/src/components/SwapForm/hooks/useGetRoute.ts b/src/components/SwapForm/hooks/useGetRoute.ts index 2905ae6ff6..54978c1977 100644 --- a/src/components/SwapForm/hooks/useGetRoute.ts +++ b/src/components/SwapForm/hooks/useGetRoute.ts @@ -146,10 +146,13 @@ const useGetRoute = (args: ArgsGetRoute) => { const params: GetRouteParams = { tokenIn: tokenInAddress, tokenOut: tokenOutAddress, + tokenInDecimals: currencyIn.decimals, + tokenOutDecimals: currencyOut.decimals, amountIn, includedSources: dexes, gasInclude: 'true', // default gasPrice: '', // default + chainId, ...feeConfigParams, } diff --git a/src/services/route/index.ts b/src/services/route/index.ts index 8984d75c5b..9c9ce94b43 100644 --- a/src/services/route/index.ts +++ b/src/services/route/index.ts @@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query/react' import { baseQueryOauthDynamic } from 'services/baseQueryOauth' import { BuildRoutePayload, BuildRouteResponse } from 'services/route/types/buildRoute' +import { BFF_API } from 'constants/env' + import { GetRouteParams, GetRouteResponse } from './types/getRoute' const routeApi = createApi({ @@ -27,6 +29,51 @@ const routeApi = createApi({ 'x-client-id': clientId || 'kyberswap', }, }), + async transformResponse(baseResponse: GetRouteResponse, _meta, { params }): Promise { + const { routeSummary } = baseResponse?.data || {} + const { chainId, tokenInDecimals, tokenOutDecimals, tokenIn, tokenOut } = params || {} + + // Ensure all necessary data is available + if (baseResponse?.data?.routeSummary && routeSummary && chainId && tokenInDecimals && tokenOutDecimals) { + const { amountIn, amountOut } = routeSummary + + // Build the URL for the price impact API request + const priceImpactUrl = new URL(`${BFF_API}/v1/price-impact`) + priceImpactUrl.searchParams.append('tokenIn', tokenIn) + priceImpactUrl.searchParams.append('tokenInDecimal', tokenInDecimals.toString()) + priceImpactUrl.searchParams.append('tokenOut', tokenOut) + priceImpactUrl.searchParams.append('tokenOutDecimal', tokenOutDecimals.toString()) + priceImpactUrl.searchParams.append('amountIn', amountIn) + priceImpactUrl.searchParams.append('amountOut', amountOut) + priceImpactUrl.searchParams.append('chainId', chainId.toString()) + + try { + // Fetch price impact data + const priceImpactResponse = await fetch(priceImpactUrl.toString()).then(res => res.json()) + const { amountInUSD, amountOutUSD } = priceImpactResponse?.data || {} + + // Update routeSummary with USD values if available + if (amountInUSD && amountOutUSD) { + return { + ...baseResponse, + data: { + ...baseResponse.data, + routeSummary: { + ...routeSummary, + amountInUsd: amountInUSD, + amountOutUsd: amountOutUSD, + }, + }, + } + } + } catch (error) { + console.error('Failed to fetch price impact:', error) + } + } + + // Return original response if conditions are not met or request fails + return baseResponse + }, }), buildRoute: builder.mutation< BuildRouteResponse, diff --git a/src/services/route/types/getRoute.ts b/src/services/route/types/getRoute.ts index 0f8867b4d0..9dc4958abb 100644 --- a/src/services/route/types/getRoute.ts +++ b/src/services/route/types/getRoute.ts @@ -14,6 +14,10 @@ export type GetRouteParams = { isInBps?: string feeReceiver?: string debug?: string + // for calculating price impact only + chainId?: number + tokenInDecimals?: number + tokenOutDecimals?: number } export type RouteSummary = {