@@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query/react'
2
2
import { baseQueryOauthDynamic } from 'services/baseQueryOauth'
3
3
import { BuildRoutePayload , BuildRouteResponse } from 'services/route/types/buildRoute'
4
4
5
+ import { BFF_API } from 'constants/env'
6
+
5
7
import { GetRouteParams , GetRouteResponse } from './types/getRoute'
6
8
7
9
const routeApi = createApi ( {
@@ -27,6 +29,51 @@ const routeApi = createApi({
27
29
'x-client-id' : clientId || 'kyberswap' ,
28
30
} ,
29
31
} ) ,
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
+ } ,
30
77
} ) ,
31
78
buildRoute : builder . mutation <
32
79
BuildRouteResponse ,
0 commit comments