2024-03-28 07:52:55 -07:00
|
|
|
|
import Image from 'next/image'
|
|
|
|
|
import { formatTokenSymbol } from 'utils/tokens'
|
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
|
import SheenLoader from './shared/SheenLoader'
|
|
|
|
|
import { useMemo } from 'react'
|
|
|
|
|
import FormatNumericValue from './shared/FormatNumericValue'
|
|
|
|
|
import { walletBalanceForToken } from './StakeForm'
|
|
|
|
|
import usePositions from 'hooks/usePositions'
|
|
|
|
|
import { ClientContextKeys } from 'utils/constants'
|
2024-06-23 23:29:28 -07:00
|
|
|
|
import { StakeableToken } from 'hooks/useStakeableTokens'
|
|
|
|
|
import { SOL_YIELD } from './Stake'
|
2024-03-28 07:52:55 -07:00
|
|
|
|
|
|
|
|
|
const TokenSelect = ({
|
|
|
|
|
onClick,
|
2024-06-23 23:29:28 -07:00
|
|
|
|
tokenInfo,
|
2024-03-28 07:52:55 -07:00
|
|
|
|
clientContext,
|
|
|
|
|
showPositionSize,
|
|
|
|
|
}: {
|
2024-06-23 23:29:28 -07:00
|
|
|
|
tokenInfo: StakeableToken
|
2024-03-28 07:52:55 -07:00
|
|
|
|
onClick: () => void
|
|
|
|
|
clientContext: ClientContextKeys
|
|
|
|
|
showPositionSize?: boolean
|
|
|
|
|
}) => {
|
2024-06-23 23:29:28 -07:00
|
|
|
|
const { symbol } = tokenInfo.token
|
2024-06-24 21:05:34 -07:00
|
|
|
|
const { estNetApy } = tokenInfo
|
2024-03-28 07:52:55 -07:00
|
|
|
|
const groupLoaded = mangoStore((s) => s.groupLoaded)
|
|
|
|
|
const walletTokens = mangoStore((s) => s.wallet.tokens)
|
|
|
|
|
const { positions } = usePositions()
|
|
|
|
|
|
|
|
|
|
const walletBalance = useMemo(() => {
|
2024-06-23 23:29:28 -07:00
|
|
|
|
return walletBalanceForToken(walletTokens, symbol, clientContext)
|
|
|
|
|
}, [walletTokens, symbol, clientContext])
|
2024-03-28 07:52:55 -07:00
|
|
|
|
|
|
|
|
|
const position = useMemo(() => {
|
|
|
|
|
if (!positions || !positions?.length) return
|
2024-06-23 23:29:28 -07:00
|
|
|
|
return positions.find((position) => position.bank.name === symbol)
|
|
|
|
|
}, [positions, symbol])
|
2024-03-28 07:52:55 -07:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
className="default-transition w-full rounded-lg p-3 md:hover:bg-th-bkg-2"
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
>
|
|
|
|
|
<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 shrink-0 items-center justify-center rounded-full border border-th-bkg-2 bg-gradient-to-b from-th-bkg-1 to-th-bkg-2`}
|
|
|
|
|
>
|
|
|
|
|
<Image
|
2024-06-23 23:29:28 -07:00
|
|
|
|
src={`/icons/${symbol.toLowerCase()}.svg`}
|
2024-03-28 07:52:55 -07:00
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
alt="Select a token"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-left">
|
2024-04-02 20:17:19 -07:00
|
|
|
|
<p className={`text-sm text-th-fgd-1 lg:text-base`}>
|
2024-06-23 23:29:28 -07:00
|
|
|
|
{formatTokenSymbol(symbol)}
|
2024-03-28 07:52:55 -07:00
|
|
|
|
</p>
|
2024-04-02 20:17:19 -07:00
|
|
|
|
<span
|
|
|
|
|
className={`text-sm font-bold leading-none text-th-fgd-1 sm:text-lg`}
|
|
|
|
|
>
|
2024-03-28 07:52:55 -07:00
|
|
|
|
{!groupLoaded ? (
|
|
|
|
|
<SheenLoader>
|
|
|
|
|
<div className={`h-5 w-10 bg-th-bkg-2`} />
|
|
|
|
|
</SheenLoader>
|
2024-06-24 21:05:34 -07:00
|
|
|
|
) : !estNetApy || isNaN(estNetApy) ? (
|
2024-03-28 07:52:55 -07:00
|
|
|
|
'Rate Unavailable'
|
2024-06-23 23:29:28 -07:00
|
|
|
|
) : symbol === 'USDC' ? (
|
2024-04-02 20:17:19 -07:00
|
|
|
|
<>
|
2024-06-24 21:05:34 -07:00
|
|
|
|
{`${estNetApy.toFixed(2)}%`}
|
2024-06-23 23:29:28 -07:00
|
|
|
|
<div className="mt-1 flex items-center">
|
|
|
|
|
<Image
|
|
|
|
|
className="mr-1"
|
|
|
|
|
src={`/icons/usdc.svg`}
|
|
|
|
|
width={14}
|
|
|
|
|
height={14}
|
|
|
|
|
alt="USDC Logo"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm font-normal text-th-fgd-4">
|
|
|
|
|
Earn USDC
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2024-04-02 20:17:19 -07:00
|
|
|
|
</>
|
2024-03-28 07:52:55 -07:00
|
|
|
|
) : (
|
2024-04-02 20:17:19 -07:00
|
|
|
|
<>
|
2024-06-24 21:05:34 -07:00
|
|
|
|
{`${estNetApy.toFixed(2)}%`}
|
2024-06-23 23:29:28 -07:00
|
|
|
|
<div className="mt-1">
|
|
|
|
|
{SOL_YIELD.includes(symbol) ? (
|
|
|
|
|
<div className="flex items-center">
|
|
|
|
|
<Image
|
|
|
|
|
className="mr-1"
|
|
|
|
|
src={`/icons/sol.svg`}
|
|
|
|
|
width={14}
|
|
|
|
|
height={14}
|
|
|
|
|
alt="SOL Logo"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm font-normal text-th-fgd-4">
|
|
|
|
|
Earn SOL
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex items-center">
|
|
|
|
|
<Image
|
|
|
|
|
className="mr-1"
|
|
|
|
|
src={`/icons/usdc.svg`}
|
|
|
|
|
width={14}
|
|
|
|
|
height={14}
|
|
|
|
|
alt="USDC Logo"
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm font-normal text-th-fgd-4">
|
|
|
|
|
Earn USDC
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2024-04-02 20:17:19 -07:00
|
|
|
|
</>
|
2024-03-28 07:52:55 -07:00
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="pl-3 text-right">
|
|
|
|
|
{showPositionSize ? (
|
|
|
|
|
position ? (
|
|
|
|
|
<>
|
|
|
|
|
<span className="text-sm font-bold text-th-fgd-1 sm:text-lg">
|
|
|
|
|
<FormatNumericValue
|
|
|
|
|
value={
|
|
|
|
|
position.stakeBalance *
|
|
|
|
|
(position.bank.name != 'USDC'
|
|
|
|
|
? position.bank?.uiPrice
|
|
|
|
|
: 1)
|
|
|
|
|
}
|
|
|
|
|
decimals={2}
|
|
|
|
|
/>{' '}
|
|
|
|
|
{'USDC'}
|
|
|
|
|
</span>
|
|
|
|
|
{position.bank.name !== 'USDC' ? (
|
|
|
|
|
<p className="text-sm text-th-fgd-4">
|
|
|
|
|
<FormatNumericValue
|
|
|
|
|
roundUp={true}
|
|
|
|
|
value={position.stakeBalance}
|
|
|
|
|
decimals={3}
|
|
|
|
|
/>{' '}
|
|
|
|
|
{formatTokenSymbol(position.bank.name)}
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
'–'
|
|
|
|
|
)
|
|
|
|
|
) : (
|
|
|
|
|
<FormatNumericValue
|
|
|
|
|
value={walletBalance.maxAmount}
|
|
|
|
|
decimals={walletBalance.maxDecimals}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TokenSelect
|