2023-09-14 18:48:02 -07:00
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
|
|
|
import { useMemo } from 'react'
|
2023-10-10 20:59:23 -07:00
|
|
|
import { BORROW_TOKEN, SHOW_INACTIVE_POSITIONS_KEY } from 'utils/constants'
|
2023-09-14 21:24:29 -07:00
|
|
|
import TokenLogo from './shared/TokenLogo'
|
|
|
|
import Button from './shared/Button'
|
|
|
|
import { formatTokenSymbol } from 'utils/tokens'
|
2023-11-10 09:58:04 -08:00
|
|
|
import mangoStore, { ActiveTab } from '@store/mangoStore'
|
2023-09-14 21:58:09 -07:00
|
|
|
import Switch from './forms/Switch'
|
|
|
|
import useLocalStorageState from 'hooks/useLocalStorageState'
|
2023-09-20 14:41:45 -07:00
|
|
|
import FormatNumericValue from './shared/FormatNumericValue'
|
2023-09-28 15:43:28 -07:00
|
|
|
import {
|
2023-09-29 16:20:05 -07:00
|
|
|
Bank,
|
2023-09-28 15:43:28 -07:00
|
|
|
MangoAccount,
|
|
|
|
toUiDecimalsForQuote,
|
|
|
|
} from '@blockworks-foundation/mango-v4'
|
2023-10-01 18:36:50 -07:00
|
|
|
import useBankRates from 'hooks/useBankRates'
|
2023-10-10 20:59:23 -07:00
|
|
|
import usePositions from 'hooks/usePositions'
|
2023-11-21 03:39:26 -08:00
|
|
|
import Tooltip from './shared/Tooltip'
|
2023-09-14 18:48:02 -07:00
|
|
|
|
2023-09-14 21:24:29 -07:00
|
|
|
const set = mangoStore.getState().set
|
2023-09-20 18:54:55 -07:00
|
|
|
|
2023-10-01 18:36:50 -07:00
|
|
|
type Position = {
|
|
|
|
borrowBalance: number
|
|
|
|
stakeBalance: number
|
|
|
|
bank: Bank
|
|
|
|
acct: MangoAccount | undefined
|
2023-09-20 18:54:55 -07:00
|
|
|
}
|
|
|
|
|
2023-09-29 16:20:05 -07:00
|
|
|
const getLiquidationRatio = (
|
|
|
|
borrowBalance: number,
|
|
|
|
stakeBalance: number,
|
|
|
|
stakeBank: Bank,
|
|
|
|
borrowBank: Bank,
|
|
|
|
) => {
|
|
|
|
return (
|
|
|
|
(Math.abs(borrowBalance) * borrowBank.maintLiabWeight.toNumber()) /
|
|
|
|
(stakeBalance * stakeBank.maintAssetWeight.toNumber())
|
|
|
|
).toFixed(3)
|
|
|
|
}
|
|
|
|
|
2023-09-14 21:24:29 -07:00
|
|
|
const Positions = ({
|
|
|
|
setActiveTab,
|
|
|
|
}: {
|
2023-11-10 09:58:04 -08:00
|
|
|
setActiveTab: (tab: ActiveTab) => void
|
2023-09-14 21:24:29 -07:00
|
|
|
}) => {
|
2023-09-14 21:58:09 -07:00
|
|
|
const [showInactivePositions, setShowInactivePositions] =
|
|
|
|
useLocalStorageState(SHOW_INACTIVE_POSITIONS_KEY, true)
|
2023-10-10 20:59:23 -07:00
|
|
|
const { borrowBank, positions } = usePositions(showInactivePositions)
|
2023-09-20 14:41:45 -07:00
|
|
|
|
2023-09-14 21:58:09 -07:00
|
|
|
const numberOfPositions = useMemo(() => {
|
|
|
|
if (!positions.length) return 0
|
2023-09-20 18:54:55 -07:00
|
|
|
return positions.filter((pos) => pos.stakeBalance > 0).length
|
2023-09-14 21:58:09 -07:00
|
|
|
}, [positions])
|
2023-09-14 18:48:02 -07:00
|
|
|
|
2023-09-24 05:55:00 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="mb-2 flex items-center justify-between rounded-lg border-2 border-th-fgd-1 bg-th-bkg-1 px-6 py-3.5">
|
|
|
|
<p className="font-medium">{`You have ${numberOfPositions} active position${
|
|
|
|
numberOfPositions !== 1 ? 's' : ''
|
2023-09-14 21:58:09 -07:00
|
|
|
}`}</p>
|
|
|
|
<Switch
|
|
|
|
checked={showInactivePositions}
|
|
|
|
onChange={(checked) => setShowInactivePositions(checked)}
|
|
|
|
>
|
|
|
|
Show Inactive
|
|
|
|
</Switch>
|
|
|
|
</div>
|
2023-09-24 05:55:00 -07:00
|
|
|
<div className="grid grid-cols-1 gap-2">
|
|
|
|
{positions.length ? (
|
2023-10-01 18:36:50 -07:00
|
|
|
positions.map((position) => {
|
|
|
|
return position.bank ? (
|
|
|
|
<PositionItem
|
|
|
|
key={position.bank.name}
|
|
|
|
position={position}
|
|
|
|
setActiveTab={setActiveTab}
|
|
|
|
borrowBank={borrowBank}
|
|
|
|
/>
|
2023-09-24 05:55:00 -07:00
|
|
|
) : null
|
|
|
|
})
|
|
|
|
) : (
|
|
|
|
<div className="flex justify-center rounded-2xl border-2 border-th-fgd-1 bg-th-bkg-1 p-6">
|
|
|
|
<span>Nothing to see here...</span>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-09-14 22:57:39 -07:00
|
|
|
</div>
|
2023-09-24 05:55:00 -07:00
|
|
|
</>
|
|
|
|
)
|
2023-09-14 18:48:02 -07:00
|
|
|
}
|
|
|
|
|
2023-10-01 18:36:50 -07:00
|
|
|
const PositionItem = ({
|
|
|
|
position,
|
|
|
|
setActiveTab,
|
|
|
|
borrowBank,
|
|
|
|
}: {
|
|
|
|
position: Position
|
2023-11-10 09:58:04 -08:00
|
|
|
setActiveTab: (v: ActiveTab) => void
|
2023-10-01 18:36:50 -07:00
|
|
|
borrowBank: Bank | undefined
|
|
|
|
}) => {
|
|
|
|
const { group } = useMangoGroup()
|
|
|
|
const { stakeBalance, borrowBalance, bank, acct } = position
|
|
|
|
|
|
|
|
const handleAddOrManagePosition = (token: string) => {
|
2023-10-02 05:30:58 -07:00
|
|
|
setActiveTab('Boost!')
|
2023-10-01 18:36:50 -07:00
|
|
|
set((state) => {
|
|
|
|
state.selectedToken = token
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const leverage = useMemo(() => {
|
2023-10-11 16:04:35 -07:00
|
|
|
if (!group || !acct) return 1
|
2023-10-01 18:36:50 -07:00
|
|
|
const accountValue = toUiDecimalsForQuote(acct.getEquity(group).toNumber())
|
|
|
|
|
|
|
|
const assetsValue = toUiDecimalsForQuote(
|
|
|
|
acct.getAssetsValue(group).toNumber(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if (isNaN(assetsValue / accountValue)) {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return Math.abs(1 - assetsValue / accountValue) + 1
|
|
|
|
}
|
|
|
|
}, [group, acct])
|
|
|
|
|
2023-10-01 19:23:46 -07:00
|
|
|
const [liqRatio, liqPriceChangePercentage] = useMemo(() => {
|
|
|
|
if (!borrowBalance || !borrowBank) return ['0.00', '']
|
|
|
|
const liqRatio = getLiquidationRatio(
|
|
|
|
borrowBalance,
|
|
|
|
stakeBalance,
|
|
|
|
bank,
|
|
|
|
borrowBank,
|
|
|
|
)
|
|
|
|
const currentPriceRatio = bank.uiPrice / borrowBank.uiPrice
|
|
|
|
const liqPriceChangePercentage =
|
|
|
|
((parseFloat(liqRatio) - currentPriceRatio) / currentPriceRatio) * 100
|
|
|
|
return [liqRatio, liqPriceChangePercentage.toFixed(2)]
|
|
|
|
}, [bank, borrowBalance, borrowBank, stakeBalance])
|
|
|
|
|
2023-10-01 18:36:50 -07:00
|
|
|
const { estimatedNetAPY } = useBankRates(bank.name, leverage)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="rounded-2xl border-2 border-th-fgd-1 bg-th-bkg-1 p-6">
|
|
|
|
<div className="mb-4 flex flex-col border-b border-th-bkg-3 pb-4 md:flex-row md:items-center md:justify-between">
|
|
|
|
<div className="mb-4 flex items-center space-x-3 md:mb-0">
|
|
|
|
<div
|
2023-10-01 19:27:51 -07:00
|
|
|
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`}
|
2023-10-01 18:36:50 -07:00
|
|
|
>
|
2023-10-01 19:27:51 -07:00
|
|
|
<TokenLogo bank={bank} size={28} />
|
2023-10-01 18:36:50 -07:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<h3>{formatTokenSymbol(bank.name)}</h3>
|
2023-10-01 19:27:51 -07:00
|
|
|
{/* <span
|
2023-10-01 18:36:50 -07:00
|
|
|
className={`text-sm ${
|
|
|
|
stakeBalance ? 'text-th-fgd-1' : 'text-th-fgd-4'
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{stakeBalance ? 'Opened 2 weeks ago' : 'No Position'}
|
2023-10-01 19:27:51 -07:00
|
|
|
</span> */}
|
2023-10-01 18:36:50 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Button onClick={() => handleAddOrManagePosition(bank.name)}>
|
2023-10-02 05:30:58 -07:00
|
|
|
<p className="mb-1 text-base tracking-wider text-th-bkg-1">
|
2023-10-01 18:36:50 -07:00
|
|
|
{stakeBalance ? 'Manage' : 'Add Position'}
|
2023-10-02 05:30:58 -07:00
|
|
|
</p>
|
2023-10-01 18:36:50 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
2023-10-09 16:10:26 -07:00
|
|
|
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
2023-10-01 18:36:50 -07:00
|
|
|
<div>
|
|
|
|
<p className="mb-1 text-th-fgd-4">Position Size</p>
|
|
|
|
<span className="text-xl font-bold text-th-fgd-1">
|
|
|
|
<FormatNumericValue value={stakeBalance} decimals={6} />{' '}
|
|
|
|
{formatTokenSymbol(bank.name)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p className="mb-1 text-th-fgd-4">Est. APY</p>
|
|
|
|
<span className="text-xl font-bold text-th-fgd-1">
|
|
|
|
<FormatNumericValue value={estimatedNetAPY} decimals={2} />%
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p className="mb-1 text-th-fgd-4">Leverage</p>
|
|
|
|
<span className="text-xl font-bold text-th-fgd-1">
|
|
|
|
{leverage ? leverage.toFixed(2) : 0.0}x
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{/* <div>
|
|
|
|
<p className="mb-1 text-th-fgd-4">Earned</p>
|
|
|
|
<span className="text-xl font-bold text-th-fgd-1">
|
|
|
|
{stakeBalance
|
|
|
|
? `X.XX ${formatTokenSymbol(bank.name)}`
|
|
|
|
: `0 ${formatTokenSymbol(bank.name)}`}
|
|
|
|
</span>
|
|
|
|
</div> */}
|
|
|
|
<div>
|
2023-11-21 03:39:26 -08:00
|
|
|
<p className="mb-1 text-th-fgd-4">Est. Liquidation Price</p>
|
2023-10-01 19:23:46 -07:00
|
|
|
<div className="flex flex-wrap items-end">
|
|
|
|
<span className="mr-2 whitespace-nowrap text-xl font-bold text-th-fgd-1">
|
|
|
|
{liqRatio} {`${formatTokenSymbol(bank.name)}/${BORROW_TOKEN}`}
|
|
|
|
</span>
|
|
|
|
{liqPriceChangePercentage ? (
|
2023-11-21 03:39:26 -08:00
|
|
|
<Tooltip content="Estimated price change required for liquidation.">
|
|
|
|
<p className="tooltip-underline mb-0.5 text-th-fgd-4">
|
|
|
|
{liqPriceChangePercentage}%
|
|
|
|
</p>
|
|
|
|
</Tooltip>
|
2023-10-01 19:23:46 -07:00
|
|
|
) : null}
|
|
|
|
</div>
|
2023-10-01 18:36:50 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-14 18:48:02 -07:00
|
|
|
export default Positions
|