lev-stake-sol/components/TokenButton.tsx

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-09-12 17:37:41 -07:00
import Image from 'next/image'
2023-09-14 21:24:29 -07:00
import { formatTokenSymbol } from 'utils/tokens'
import useBankRates from 'hooks/useBankRates'
import useLeverageMax from 'hooks/useLeverageMax'
2024-03-10 04:18:19 -07:00
import mangoStore from '@store/mangoStore'
import SheenLoader from './shared/SheenLoader'
2024-03-20 03:26:49 -07:00
import { ChevronDownIcon } from '@heroicons/react/20/solid'
2023-09-12 17:37:41 -07:00
2023-09-25 06:48:39 -07:00
const TokenButton = ({
2024-03-20 03:26:49 -07:00
onClick,
2023-09-25 06:48:39 -07:00
tokenName,
}: {
tokenName: string
2024-03-20 03:26:49 -07:00
onClick: () => void
2023-09-25 06:48:39 -07:00
}) => {
const leverage = useLeverageMax(tokenName)
2024-03-10 04:18:19 -07:00
const groupLoaded = mangoStore((s) => s.groupLoaded)
2024-02-24 09:34:30 -08:00
2024-02-27 07:30:05 -08:00
const { stakeBankDepositRate, financialMetrics } = useBankRates(
2024-02-20 13:41:06 -08:00
tokenName,
leverage,
)
2024-02-22 08:02:05 -08:00
2024-02-27 07:30:05 -08:00
const { financialMetrics: estimatedNetAPYFor1xLev } = useBankRates(
2024-02-24 16:09:28 -08:00
tokenName,
1,
)
2024-03-10 04:18:19 -07:00
const APY_Daily_Compound =
Math.pow(1 + Number(stakeBankDepositRate) / 365, 365) - 1
2024-02-24 16:09:28 -08:00
const UiRate =
tokenName === 'USDC'
2024-02-28 03:04:28 -08:00
? APY_Daily_Compound * 100
2024-02-27 07:30:05 -08:00
: Math.max(estimatedNetAPYFor1xLev.APY, financialMetrics.APY)
2023-09-14 20:48:49 -07:00
2023-09-12 17:37:41 -07:00
return (
2023-09-14 06:18:39 -07:00
<button
2024-03-20 03:26:49 -07:00
className={`inner-shadow-bottom-sm w-full rounded-xl border border-th-bkg-3 bg-th-bkg-1 p-3 text-th-fgd-1 focus:outline-none focus-visible:border-th-fgd-4 md:hover:border-th-bkg-4 md:hover:focus-visible:border-th-fgd-4`}
onClick={onClick}
2023-09-14 06:18:39 -07:00
>
2024-03-20 03:26:49 -07:00
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<div
className={`inner-shadow-bottom-sm flex h-12 w-12 items-center justify-center rounded-full border border-th-bkg-2 bg-gradient-to-b from-th-bkg-1 to-th-bkg-2`}
>
<Image
src={`/icons/${tokenName.toLowerCase()}.svg`}
width={24}
height={24}
alt="Select a token"
/>
</div>
<div className="text-left">
<p className={`text-lg font-bold text-th-fgd-1`}>
{formatTokenSymbol(tokenName)}
</p>
<span className={`font-medium text-th-fgd-4`}>
{!groupLoaded ? (
<SheenLoader>
<div className={`h-5 w-10 bg-th-bkg-2`} />
</SheenLoader>
) : !UiRate || isNaN(UiRate) ? (
'Rate Unavailable'
) : tokenName === 'USDC' ? (
`${UiRate.toFixed(2)}% APY`
) : (
`Up to ${UiRate.toFixed(2)}% APY`
)}
</span>
</div>
2023-09-22 06:33:36 -07:00
</div>
2024-03-20 03:26:49 -07:00
<ChevronDownIcon className="h-6 w-6" />
2023-09-12 17:37:41 -07:00
</div>
</button>
)
}
export default TokenButton