2024-04-01 15:23:01 +02:00
|
|
|
'use client'
|
|
|
|
|
2024-07-26 14:16:59 +02:00
|
|
|
import { fetcher, getTokenAddressBySymbol } from '@/lib'
|
|
|
|
import type { TokenSymbol } from '@/types'
|
2024-04-01 14:42:08 +02:00
|
|
|
import useSWR from 'swr'
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
keepPreviousData: true // so loading UI can kick in properly
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useQuote(
|
|
|
|
tokenSymbol: TokenSymbol,
|
|
|
|
amount: number,
|
|
|
|
shouldFetch: boolean
|
|
|
|
) {
|
|
|
|
// -> AGIX
|
|
|
|
const {
|
|
|
|
data: dataSwapToAgix,
|
|
|
|
isValidating: isValidatingToAgix,
|
|
|
|
isLoading: isLoadingToAgix
|
|
|
|
} = useSWR(
|
|
|
|
shouldFetch
|
|
|
|
? `/api/quote/?tokenIn=${getTokenAddressBySymbol(
|
|
|
|
tokenSymbol
|
|
|
|
)}&tokenOut=${getTokenAddressBySymbol('AGIX')}&amountIn=${amount}`
|
|
|
|
: null,
|
|
|
|
fetcher,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
// -> FET
|
|
|
|
const {
|
|
|
|
data: dataSwapToFet,
|
|
|
|
isValidating: isValidatingToFet,
|
|
|
|
isLoading: isLoadingToFet
|
|
|
|
} = useSWR(
|
|
|
|
shouldFetch
|
|
|
|
? `/api/quote/?tokenIn=${getTokenAddressBySymbol(
|
|
|
|
tokenSymbol
|
|
|
|
)}&tokenOut=${getTokenAddressBySymbol('FET')}&amountIn=${amount}`
|
|
|
|
: null,
|
|
|
|
fetcher,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
// -> OCEAN
|
|
|
|
const {
|
|
|
|
data: dataSwapToOcean,
|
|
|
|
isValidating: isValidatingToOcean,
|
|
|
|
isLoading: isLoadingToOcean
|
|
|
|
} = useSWR(
|
|
|
|
shouldFetch
|
|
|
|
? `/api/quote/?tokenIn=${getTokenAddressBySymbol(
|
|
|
|
tokenSymbol
|
|
|
|
)}&tokenOut=${getTokenAddressBySymbol('OCEAN')}&amountIn=${amount}`
|
|
|
|
: null,
|
|
|
|
fetcher,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
2024-09-19 22:01:39 +02:00
|
|
|
// -> CUDOS
|
|
|
|
const {
|
|
|
|
data: dataSwapToCudos,
|
|
|
|
isValidating: isValidatingToCudos,
|
|
|
|
isLoading: isLoadingToCudos
|
|
|
|
} = useSWR(
|
|
|
|
shouldFetch
|
|
|
|
? `/api/quote/?tokenIn=${getTokenAddressBySymbol(
|
|
|
|
tokenSymbol
|
|
|
|
)}&tokenOut=${getTokenAddressBySymbol('CUDOS')}&amountIn=${amount}`
|
|
|
|
: null,
|
|
|
|
fetcher,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
2024-04-01 14:42:08 +02:00
|
|
|
const amountToOcean =
|
|
|
|
dataSwapToOcean?.amountOut / Number(`1e${dataSwapToOcean?.decimals}`)
|
|
|
|
const amountToAgix =
|
|
|
|
dataSwapToAgix?.amountOut / Number(`1e${dataSwapToAgix?.decimals}`)
|
2024-09-19 22:01:39 +02:00
|
|
|
const amountToCudos =
|
|
|
|
dataSwapToCudos?.amountOut / Number(`1e${dataSwapToCudos?.decimals}`)
|
2024-04-01 14:42:08 +02:00
|
|
|
const amountToFet =
|
|
|
|
dataSwapToFet?.amountOut / Number(`1e${dataSwapToFet?.decimals}`)
|
|
|
|
|
|
|
|
return shouldFetch
|
|
|
|
? {
|
|
|
|
amountToOcean,
|
|
|
|
amountToAgix,
|
|
|
|
amountToFet,
|
2024-09-19 22:01:39 +02:00
|
|
|
amountToCudos,
|
2024-04-01 14:42:08 +02:00
|
|
|
isValidatingToAgix,
|
|
|
|
isLoadingToAgix,
|
|
|
|
isValidatingToFet,
|
|
|
|
isLoadingToFet,
|
|
|
|
isValidatingToOcean,
|
2024-09-19 22:01:39 +02:00
|
|
|
isLoadingToOcean,
|
|
|
|
isValidatingToCudos,
|
|
|
|
isLoadingToCudos
|
2024-04-01 14:42:08 +02:00
|
|
|
}
|
|
|
|
: {
|
|
|
|
amountToOcean: undefined,
|
|
|
|
amountToAgix: undefined,
|
2024-09-19 22:01:39 +02:00
|
|
|
amountToCudos: undefined,
|
2024-04-01 14:42:08 +02:00
|
|
|
amountToFet: undefined,
|
|
|
|
isValidatingToAgix: false,
|
|
|
|
isLoadingToAgix: false,
|
|
|
|
isValidatingToFet: false,
|
|
|
|
isLoadingToFet: false,
|
|
|
|
isValidatingToOcean: false,
|
2024-09-19 22:01:39 +02:00
|
|
|
isLoadingToOcean: false,
|
|
|
|
isValidatingToCudos: false,
|
|
|
|
isLoadingToCudos: false
|
2024-04-01 14:42:08 +02:00
|
|
|
}
|
|
|
|
}
|