asi-calculator/components/Strategies/Swap/Results.tsx

143 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-03-31 16:48:31 +02:00
import { Result } from '@/components/ResultRow'
2024-03-31 23:50:43 +02:00
import { ratioOceanToAsi, ratioAgixToAsi, ratioFetToAsi } from '@/constants'
2024-03-31 16:48:31 +02:00
import { usePrices } from '@/hooks'
2024-03-31 23:50:43 +02:00
import { fetcher, getTokenAddressBySymbol, getTokenBySymbol } from '@/utils'
2024-03-30 19:11:24 +01:00
import useSWR from 'swr'
2024-03-31 16:48:31 +02:00
import { TokenSymbol } from '@/types'
2024-03-30 19:11:24 +01:00
2024-04-01 02:15:51 +02:00
const options = {
keepPreviousData: true // so loading UI can kick in properly
}
2024-03-31 16:48:31 +02:00
export function SwapResults({
2024-03-31 23:50:43 +02:00
tokenSymbol,
2024-03-31 16:48:31 +02:00
amount
}: {
2024-03-31 23:50:43 +02:00
tokenSymbol: TokenSymbol
2024-03-31 16:48:31 +02:00
amount: number
}) {
2024-04-01 02:15:51 +02:00
const {
prices,
isValidating: isValidatingPrices,
isLoading: isLoadingPrices
} = usePrices()
2024-03-30 19:11:24 +01:00
2024-03-31 23:50:43 +02:00
// -> AGIX
2024-04-01 02:15:51 +02:00
const {
data: dataSwapToAgix,
isValidating: isValidatingToAgix,
isLoading: isLoadingToAgix
} = useSWR(
2024-03-31 23:50:43 +02:00
`/api/quote/?tokenIn=${getTokenAddressBySymbol(
tokenSymbol
)}&tokenOut=${getTokenAddressBySymbol('AGIX')}&amountIn=${amount}`,
2024-04-01 02:15:51 +02:00
fetcher,
options
2024-03-31 23:50:43 +02:00
)
// -> FET
2024-04-01 02:15:51 +02:00
const {
data: dataSwapToFet,
isValidating: isValidatingToFet,
isLoading: isLoadingToFet
} = useSWR(
2024-03-31 23:50:43 +02:00
`/api/quote/?tokenIn=${getTokenAddressBySymbol(
tokenSymbol
)}&tokenOut=${getTokenAddressBySymbol('FET')}&amountIn=${amount}`,
2024-04-01 02:15:51 +02:00
fetcher,
options
2024-03-31 23:50:43 +02:00
)
// -> OCEAN
2024-04-01 02:15:51 +02:00
const {
data: dataSwapToOcean,
isValidating: isValidatingToOcean,
isLoading: isLoadingToOcean
} = useSWR(
2024-03-31 23:50:43 +02:00
`/api/quote/?tokenIn=${getTokenAddressBySymbol(
tokenSymbol
)}&tokenOut=${getTokenAddressBySymbol('OCEAN')}&amountIn=${amount}`,
2024-04-01 02:15:51 +02:00
fetcher,
options
2024-03-31 23:50:43 +02:00
)
2024-03-30 19:11:24 +01:00
return (
2024-03-31 16:48:31 +02:00
<>
2024-03-30 19:11:24 +01:00
<Result
2024-03-31 16:58:26 +02:00
token={getTokenBySymbol('OCEAN')}
2024-03-31 23:50:43 +02:00
amount={
dataSwapToOcean?.amountOut / Number(`1e${dataSwapToOcean?.decimals}`)
}
amountAsi={
(dataSwapToOcean?.amountOut /
Number(`1e${dataSwapToOcean?.decimals}`)) *
ratioOceanToAsi
}
amountFiat={
(dataSwapToOcean?.amountOut /
Number(`1e${dataSwapToOcean?.decimals}`)) *
ratioOceanToAsi *
prices.asi
}
2024-03-31 16:48:31 +02:00
amountOriginalFiat={
2024-03-31 23:50:43 +02:00
tokenSymbol
? (dataSwapToOcean?.amountOut /
Number(`1e${dataSwapToOcean?.decimals}`)) *
prices[
tokenSymbol.toLowerCase() as 'ocean' | 'agix' | 'fet' | 'asi'
]
2024-03-31 16:48:31 +02:00
: undefined
}
2024-03-31 23:50:43 +02:00
isValidating={isValidatingToOcean || isValidatingPrices}
2024-04-01 02:15:51 +02:00
isLoading={isLoadingToOcean || isLoadingPrices}
2024-03-30 19:11:24 +01:00
/>
<Result
2024-03-31 16:58:26 +02:00
token={getTokenBySymbol('AGIX')}
2024-03-30 19:11:24 +01:00
amount={
2024-03-31 23:50:43 +02:00
dataSwapToAgix?.amountOut / Number(`1e${dataSwapToAgix?.decimals}`)
2024-03-30 19:11:24 +01:00
}
amountAsi={
2024-03-31 23:50:43 +02:00
(dataSwapToAgix?.amountOut /
Number(`1e${dataSwapToAgix?.decimals}`)) *
ratioAgixToAsi
2024-03-30 19:11:24 +01:00
}
amountFiat={
2024-03-31 23:50:43 +02:00
(dataSwapToAgix?.amountOut /
Number(`1e${dataSwapToAgix?.decimals}`)) *
2024-03-30 19:11:24 +01:00
ratioAgixToAsi *
prices.asi
}
2024-03-31 05:03:37 +02:00
amountOriginalFiat={
2024-03-31 23:50:43 +02:00
(dataSwapToAgix?.amountOut /
Number(`1e${dataSwapToAgix?.decimals}`)) *
prices.agix
2024-03-31 05:03:37 +02:00
}
2024-03-31 23:50:43 +02:00
isValidating={isValidatingToAgix || isValidatingPrices}
2024-04-01 02:15:51 +02:00
isLoading={isLoadingToAgix || isLoadingPrices}
2024-03-30 19:11:24 +01:00
/>
<Result
2024-03-31 16:58:26 +02:00
token={getTokenBySymbol('FET')}
2024-03-30 19:11:24 +01:00
amount={
2024-03-31 23:50:43 +02:00
dataSwapToFet?.amountOut / Number(`1e${dataSwapToFet?.decimals}`)
2024-03-30 19:11:24 +01:00
}
amountAsi={
2024-03-31 23:50:43 +02:00
(dataSwapToFet?.amountOut / Number(`1e${dataSwapToFet?.decimals}`)) *
ratioFetToAsi
2024-03-30 19:11:24 +01:00
}
amountFiat={
2024-03-31 23:50:43 +02:00
(dataSwapToFet?.amountOut / Number(`1e${dataSwapToFet?.decimals}`)) *
prices.asi
2024-03-30 19:11:24 +01:00
}
2024-03-31 05:03:37 +02:00
amountOriginalFiat={
2024-03-31 23:50:43 +02:00
(dataSwapToFet?.amountOut / Number(`1e${dataSwapToFet?.decimals}`)) *
prices.asi
2024-03-31 05:03:37 +02:00
}
2024-03-31 23:50:43 +02:00
isValidating={isValidatingToFet || isValidatingPrices}
2024-04-01 02:15:51 +02:00
isLoading={isLoadingToFet || isLoadingPrices}
2024-03-30 19:11:24 +01:00
/>
2024-03-31 16:48:31 +02:00
</>
2024-03-30 19:11:24 +01:00
)
}