Skip to content

Commit

Permalink
feat: update PI response call from api
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Oct 1, 2024
1 parent c7d6f17 commit 5a22d9b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/SwapForm/hooks/useGetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
47 changes: 47 additions & 0 deletions src/services/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -27,6 +29,51 @@ const routeApi = createApi({
'x-client-id': clientId || 'kyberswap',
},
}),
async transformResponse(baseResponse: GetRouteResponse, _meta, { params }): Promise<GetRouteResponse> {
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,
Expand Down
4 changes: 4 additions & 0 deletions src/services/route/types/getRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 5a22d9b

Please sign in to comment.