From ef911a47b52cc43a592c45ae36b56880305bf4f9 Mon Sep 17 00:00:00 2001 From: tjs Date: Wed, 20 Sep 2023 21:54:55 -0400 Subject: [PATCH] add lev --- components/Positions.tsx | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/components/Positions.tsx b/components/Positions.tsx index 6f41e9f..82aadd8 100644 --- a/components/Positions.tsx +++ b/components/Positions.tsx @@ -14,6 +14,13 @@ import FormatNumericValue from './shared/FormatNumericValue' const set = mangoStore.getState().set +const BORROW_TOKEN = 'SOL' + +const getLeverage = (stakeBalance: number, borrowBalance: number) => { + if (stakeBalance === 0) return 0 + return borrowBalance / stakeBalance +} + const Positions = ({ setActiveTab, }: { @@ -25,6 +32,10 @@ const Positions = ({ useLocalStorageState(SHOW_INACTIVE_POSITIONS_KEY, true) const { stakeAccounts } = useStakeAccounts() + const borrowBank = useMemo(() => { + return group?.banksMapByMint.get(BORROW_TOKEN)?.[0] + }, [group]) + const banks = useMemo(() => { if (!group) return [] const positionBanks = [] @@ -41,20 +52,22 @@ const Positions = ({ for (const bank of banks) { if (!bank) continue const acct = stakeAccounts.find((acc) => acc.getTokenBalanceUi(bank) > 0) - const balance = acct ? acct.getTokenBalanceUi(bank) : 0 - positions.push({ balance, bank }) + const stakeBalance = acct ? acct.getTokenBalanceUi(bank) : 0 + const borrowBalance = + acct && borrowBank ? acct.getTokenBalanceUi(borrowBank) : 0 + positions.push({ borrowBalance, stakeBalance, bank }) } - const sortedPositions = positions.sort((a, b) => b.balance - a.balance) + const sortedPositions = positions.sort( + (a, b) => b.stakeBalance - a.stakeBalance, + ) return showInactivePositions ? sortedPositions - : sortedPositions.filter((pos) => pos.balance > 0) + : sortedPositions.filter((pos) => pos.stakeBalance > 0) }, [banks, showInactivePositions, stakeAccounts]) - console.log('positions', positions) - const numberOfPositions = useMemo(() => { if (!positions.length) return 0 - return positions.filter((pos) => pos.balance > 0).length + return positions.filter((pos) => pos.stakeBalance > 0).length }, [positions]) const handleAddOrManagePosition = (token: string) => { @@ -79,11 +92,11 @@ const Positions = ({
{positions.map((position, i) => { - const { balance, bank } = position + const { stakeBalance, borrowBalance, bank } = position return bank ? (
@@ -92,10 +105,10 @@ const Positions = ({

{formatTokenSymbol(bank.name)}

- {balance ? 'Opened 2 weeks ago' : 'No Position'} + {stakeBalance ? 'Opened 2 weeks ago' : 'No Position'}
@@ -103,14 +116,14 @@ const Positions = ({ onClick={() => handleAddOrManagePosition(bank.name)} secondary > - {balance ? 'Manage' : 'Add Position'} + {stakeBalance ? 'Manage' : 'Add Position'}

Position Size

- {' '} + {' '} {formatTokenSymbol(bank.name)}
@@ -131,19 +144,17 @@ const Positions = ({

Leverage

- {balance ? '3x' : '0x'} + {getLeverage(stakeBalance, borrowBalance)}x

Earned

- - {balance ? '3.321 SOL' : '0 SOL'} - + 0 {bank.name}

Liquidation Price

- {balance ? '1.234' : '0'}{' '} + {stakeBalance ? '1.234' : '0'}{' '} {`${formatTokenSymbol(bank.name)}/SOL`}