Skip to content

Commit 1fe49fc

Browse files
authored
feat: update PI response call from api (#2542)
1 parent c7d6f17 commit 1fe49fc

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/components/SwapForm/hooks/useGetRoute.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ const useGetRoute = (args: ArgsGetRoute) => {
146146
const params: GetRouteParams = {
147147
tokenIn: tokenInAddress,
148148
tokenOut: tokenOutAddress,
149+
tokenInDecimals: currencyIn.decimals,
150+
tokenOutDecimals: currencyOut.decimals,
149151
amountIn,
150152
includedSources: dexes,
151153
gasInclude: 'true', // default
152154
gasPrice: '', // default
155+
chainId,
153156
...feeConfigParams,
154157
}
155158

src/services/route/index.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query/react'
22
import { baseQueryOauthDynamic } from 'services/baseQueryOauth'
33
import { BuildRoutePayload, BuildRouteResponse } from 'services/route/types/buildRoute'
44

5+
import { BFF_API } from 'constants/env'
6+
57
import { GetRouteParams, GetRouteResponse } from './types/getRoute'
68

79
const routeApi = createApi({
@@ -27,6 +29,51 @@ const routeApi = createApi({
2729
'x-client-id': clientId || 'kyberswap',
2830
},
2931
}),
32+
async transformResponse(baseResponse: GetRouteResponse, _meta, { params }): Promise<GetRouteResponse> {
33+
const { routeSummary } = baseResponse?.data || {}
34+
const { chainId, tokenInDecimals, tokenOutDecimals, tokenIn, tokenOut } = params || {}
35+
36+
// Ensure all necessary data is available
37+
if (baseResponse?.data?.routeSummary && routeSummary && chainId && tokenInDecimals && tokenOutDecimals) {
38+
const { amountIn, amountOut } = routeSummary
39+
40+
// Build the URL for the price impact API request
41+
const priceImpactUrl = new URL(`${BFF_API}/v1/price-impact`)
42+
priceImpactUrl.searchParams.append('tokenIn', tokenIn)
43+
priceImpactUrl.searchParams.append('tokenInDecimal', tokenInDecimals.toString())
44+
priceImpactUrl.searchParams.append('tokenOut', tokenOut)
45+
priceImpactUrl.searchParams.append('tokenOutDecimal', tokenOutDecimals.toString())
46+
priceImpactUrl.searchParams.append('amountIn', amountIn)
47+
priceImpactUrl.searchParams.append('amountOut', amountOut)
48+
priceImpactUrl.searchParams.append('chainId', chainId.toString())
49+
50+
try {
51+
// Fetch price impact data
52+
const priceImpactResponse = await fetch(priceImpactUrl.toString()).then(res => res.json())
53+
const { amountInUSD, amountOutUSD } = priceImpactResponse?.data || {}
54+
55+
// Update routeSummary with USD values if available
56+
if (amountInUSD && amountOutUSD) {
57+
return {
58+
...baseResponse,
59+
data: {
60+
...baseResponse.data,
61+
routeSummary: {
62+
...routeSummary,
63+
amountInUsd: amountInUSD,
64+
amountOutUsd: amountOutUSD,
65+
},
66+
},
67+
}
68+
}
69+
} catch (error) {
70+
console.error('Failed to fetch price impact:', error)
71+
}
72+
}
73+
74+
// Return original response if conditions are not met or request fails
75+
return baseResponse
76+
},
3077
}),
3178
buildRoute: builder.mutation<
3279
BuildRouteResponse,

src/services/route/types/getRoute.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export type GetRouteParams = {
1414
isInBps?: string
1515
feeReceiver?: string
1616
debug?: string
17+
// for calculating price impact only
18+
chainId?: number
19+
tokenInDecimals?: number
20+
tokenOutDecimals?: number
1721
}
1822

1923
export type RouteSummary = {

0 commit comments

Comments
 (0)