Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update PI response call from api #2542

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
tienkane marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading