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 2ca8eee
Show file tree
Hide file tree
Showing 3 changed files with 30 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
23 changes: 23 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,27 @@ const routeApi = createApi({
'x-client-id': clientId || 'kyberswap',
},
}),
async transformResponse(baseResponse: GetRouteResponse, _meta, { params }): Promise<GetRouteResponse> {
if (baseResponse?.data?.routeSummary && params.chainId && params.tokenInDecimals && params.tokenOutDecimals) {
const priceImpactResponse = await fetch(
`${BFF_API}/v1/price-impact?tokenIn=${params.tokenIn}&tokenInDecimal=${params.tokenInDecimals}&tokenOut=${params.tokenOut}&tokenOutDecimal=${params.tokenOutDecimals}&amountIn=${baseResponse.data.routeSummary.amountIn}&amountOut=${baseResponse.data.routeSummary.amountOut}&chainId=${params.chainId}`,
).then(res => res.json())
if (priceImpactResponse?.data?.amountInUSD && priceImpactResponse?.data?.amountOutUSD)
return {
...baseResponse,
data: {
...baseResponse.data,
routeSummary: {
...baseResponse.data.routeSummary,
amountInUsd: priceImpactResponse.data.amountInUSD,
amountOutUsd: priceImpactResponse.data.amountOutUSD,
},
},
}
}

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 2ca8eee

Please sign in to comment.