asi-calculator/components/Strategies/Swap.tsx

111 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-03-30 19:11:24 +01:00
'use client'
import {
ratioOceanToAsi,
ratioAgixToAsi,
ratioFetToAsi,
tokens
} from '@/constants'
2024-03-31 05:03:37 +02:00
import { fetcher } from '@/utils'
2024-03-30 20:50:51 +01:00
import { Result } from '@/components/ResultRow'
import stylesShared from './styles.module.css'
2024-03-30 19:11:24 +01:00
import { useState } from 'react'
import useSWR from 'swr'
import { useDebounce } from 'use-debounce'
import { usePrices } from '@/hooks'
2024-03-30 20:50:51 +01:00
import { FormAmount, type Token } from '@/components/FormAmount'
2024-03-30 19:11:24 +01:00
export function Swap() {
2024-03-31 05:32:11 +02:00
const { prices, isValidating: isValidatingPrices } = usePrices()
2024-03-30 20:50:51 +01:00
const [amount, setAmount] = useState(100)
const [debouncedAmount] = useDebounce(amount, 500)
const [token, setToken] = useState<Token>('ocean')
2024-03-30 19:11:24 +01:00
2024-03-31 05:32:11 +02:00
const { data: dataSwapOceanToAgix, isValidating: isValidatingOceanToAgix } =
useSWR(
`/api/quote/?tokenIn=${tokens[0]}&tokenOut=${tokens[2]}&amountIn=${debouncedAmount}`,
fetcher
)
2024-03-30 19:11:24 +01:00
2024-03-31 05:32:11 +02:00
const { data: dataSwapOceanToFet, isValidating: isValidatingOceanToFet } =
useSWR(
`/api/quote/?tokenIn=${tokens[0]}&tokenOut=${tokens[1]}&amountIn=${debouncedAmount}`,
fetcher
)
2024-03-30 19:11:24 +01:00
return (
2024-03-30 20:50:51 +01:00
<div className={stylesShared.results}>
<h3 className={stylesShared.title}>
Swapping{' '}
<FormAmount
amount={amount}
token={token}
setAmount={setAmount}
// setToken={setToken}
/>{' '}
2024-03-31 05:03:37 +02:00
right now gets you:
2024-03-30 19:11:24 +01:00
</h3>
<Result
tokenSymbol="OCEAN"
tokenAddress="0x967da4048cd07ab37855c090aaf366e4ce1b9f48"
2024-03-30 20:50:51 +01:00
amount={debouncedAmount}
amountAsi={debouncedAmount * ratioOceanToAsi}
amountFiat={debouncedAmount * ratioOceanToAsi * prices.asi}
2024-03-31 05:03:37 +02:00
amountOriginalFiat={token ? debouncedAmount * prices[token] : undefined}
2024-03-31 05:32:11 +02:00
isValidating={
isValidatingOceanToAgix ||
isValidatingOceanToFet ||
isValidatingPrices
}
2024-03-30 19:11:24 +01:00
/>
<Result
tokenSymbol="AGIX"
tokenAddress="0x5b7533812759b45c2b44c19e320ba2cd2681b542"
amount={
dataSwapOceanToAgix?.amountOut /
Number(`1e${dataSwapOceanToAgix?.decimals}`) || 0
}
amountAsi={
(dataSwapOceanToAgix?.amountOut /
Number(`1e${dataSwapOceanToAgix?.decimals}`) || 0) * ratioAgixToAsi
}
amountFiat={
(dataSwapOceanToAgix?.amountOut /
Number(`1e${dataSwapOceanToAgix?.decimals}`) || 0) *
ratioAgixToAsi *
prices.asi
}
2024-03-31 05:03:37 +02:00
amountOriginalFiat={
(dataSwapOceanToAgix?.amountOut /
Number(`1e${dataSwapOceanToAgix?.decimals}`) || 0) * prices.agix
}
2024-03-31 05:32:11 +02:00
isValidating={isValidatingOceanToAgix || isValidatingPrices}
2024-03-30 19:11:24 +01:00
/>
<Result
tokenSymbol="FET"
tokenAddress="0xaea46a60368a7bd060eec7df8cba43b7ef41ad85"
amount={
dataSwapOceanToFet?.amountOut /
Number(`1e${dataSwapOceanToFet?.decimals}`) || 0
}
amountAsi={
(dataSwapOceanToFet?.amountOut /
Number(`1e${dataSwapOceanToFet?.decimals}`) || 0) * ratioFetToAsi
}
amountFiat={
(dataSwapOceanToFet?.amountOut /
Number(`1e${dataSwapOceanToFet?.decimals}`) || 0) * prices.asi
}
2024-03-31 05:03:37 +02:00
amountOriginalFiat={
(dataSwapOceanToFet?.amountOut /
Number(`1e${dataSwapOceanToFet?.decimals}`) || 0) * prices.asi
}
2024-03-31 05:32:11 +02:00
isValidating={isValidatingOceanToFet || isValidatingPrices}
2024-03-30 19:11:24 +01:00
/>
</div>
)
}