From 6ead35fd8a88dd762ea432ac944734a2c61d7a0a Mon Sep 17 00:00:00 2001 From: tjs Date: Mon, 15 Aug 2022 18:18:23 -0400 Subject: [PATCH] wire up borrow modal; use maint health --- components/account/MangoAccount.tsx | 59 ----------- components/account/MangoAccountSummary.tsx | 2 +- components/modals/BorrowModal.tsx | 112 +++++++++++++++++---- components/modals/WithdrawModal.tsx | 38 ++++--- components/swap/LeverageSlider.tsx | 82 ++++++++++----- components/swap/Swap.tsx | 4 +- pages/index.tsx | 2 +- 7 files changed, 175 insertions(+), 124 deletions(-) delete mode 100644 components/account/MangoAccount.tsx diff --git a/components/account/MangoAccount.tsx b/components/account/MangoAccount.tsx deleted file mode 100644 index cf2ac00f..00000000 --- a/components/account/MangoAccount.tsx +++ /dev/null @@ -1,59 +0,0 @@ -import ContentBox from '../shared/ContentBox' -import mangoStore from '../../store/state' -import AccountActions from './AccountActions' -import { - HealthType, - toUiDecimalsForQuote, -} from '@blockworks-foundation/mango-v4' -import { formatDecimal } from '../../utils/numbers' - -const MangoAccount = () => { - const mangoAccount = mangoStore((s) => s.mangoAccount.current) - - return ( - -
-
-
Account Value
-
- $ - {mangoAccount - ? formatDecimal( - toUiDecimalsForQuote(mangoAccount.getEquity().toNumber()), - 2 - ) - : (0).toFixed(2)} -
-
-
-
Free Collateral
-
- $ - {mangoAccount - ? formatDecimal( - toUiDecimalsForQuote( - mangoAccount.getCollateralValue().toNumber() - ), - 2 - ) - : (0).toFixed(2)} -
-
-
-
Health
-
- {mangoAccount - ? mangoAccount.getHealthRatioUi(HealthType.init) - : 100} - % -
-
-
-
- -
-
- ) -} - -export default MangoAccount diff --git a/components/account/MangoAccountSummary.tsx b/components/account/MangoAccountSummary.tsx index 4e50702d..a54516ba 100644 --- a/components/account/MangoAccountSummary.tsx +++ b/components/account/MangoAccountSummary.tsx @@ -49,7 +49,7 @@ const MangoAccountSummary = () => {

{t('health')}

{mangoAccount - ? mangoAccount.getHealthRatioUi(HealthType.init) + ? mangoAccount.getHealthRatioUi(HealthType.maint) : 100} %

diff --git a/components/modals/BorrowModal.tsx b/components/modals/BorrowModal.tsx index 11b17abd..e417dc0d 100644 --- a/components/modals/BorrowModal.tsx +++ b/components/modals/BorrowModal.tsx @@ -1,9 +1,12 @@ +import { toUiDecimals } from '@blockworks-foundation/mango-v4' import { ChevronDownIcon } from '@heroicons/react/solid' import { useTranslation } from 'next-i18next' import Image from 'next/image' -import React, { ChangeEvent, useState } from 'react' -// import mangoStore from '../../store/state' +import React, { ChangeEvent, useCallback, useMemo, useState } from 'react' +import mangoStore from '../../store/state' import { ModalProps } from '../../types/modal' +import { notify } from '../../utils/notifications' +import { formatFixedDecimals } from '../../utils/numbers' import ButtonGroup from '../forms/ButtonGroup' // import { notify } from '../../utils/notifications' import Input from '../forms/Input' @@ -13,7 +16,7 @@ import DepositTokenList from '../shared/DepositTokenList' import Loading from '../shared/Loading' import Modal from '../shared/Modal' import { EnterBottomExitBottom, FadeInFadeOut } from '../shared/Transitions' -import LeverageSlider from '../swap/LeverageSlider' +import { BorrowLeverageSlider } from '../swap/LeverageSlider' interface BorrowModalProps { token?: string @@ -29,20 +32,87 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { const [showTokenList, setShowTokenList] = useState(false) const [sizePercentage, setSizePercentage] = useState('') - const handleSizePercentage = (percentage: string) => { - setSizePercentage(percentage) + const mangoAccount = mangoStore((s) => s.mangoAccount.current) - // TODO: calc max - const max = 100 - const amount = (Number(percentage) / 100) * max - setInputAmount(amount.toFixed()) - } + const bank = useMemo(() => { + const group = mangoStore.getState().group + return group?.banksMap.get(selectedToken) + }, [selectedToken]) + + const healthImpact = useMemo(() => { + const group = mangoStore.getState().group + if (!group || !bank || !mangoAccount) return 0 + + return mangoAccount + .simHealthRatioWithTokenPositionChanges(group, [ + { tokenName: bank.name, tokenAmount: parseFloat(inputAmount) * -1 }, + ]) + .toNumber() + }, [mangoAccount, bank, inputAmount]) + + const tokenMax = useMemo(() => { + const group = mangoStore.getState().group + if (!group || !bank) return 0 + const amount = mangoAccount + ?.getMaxWithdrawWithBorrowForToken(group, selectedToken) + .toNumber() + return amount ? toUiDecimals(amount, bank.mintDecimals) : 0 + }, [mangoAccount, bank, selectedToken]) + + const setMax = useCallback(() => { + setInputAmount(tokenMax.toString()) + }, [tokenMax]) + + const handleSizePercentage = useCallback( + (percentage: string) => { + setSizePercentage(percentage) + const amount = (Number(percentage) / 100) * (tokenMax || 0) + setInputAmount(amount.toString()) + }, + [tokenMax] + ) const handleSelectToken = (token: string) => { setSelectedToken(token) setShowTokenList(false) } + const handleWithdraw = async () => { + const client = mangoStore.getState().client + const group = mangoStore.getState().group + const mangoAccount = mangoStore.getState().mangoAccount.current + const actions = mangoStore.getState().actions + if (!mangoAccount || !group) return + setSubmitting(true) + try { + const tx = await client.tokenWithdraw( + group, + mangoAccount, + selectedToken, + parseFloat(inputAmount), + true + ) + notify({ + title: 'Transaction confirmed', + type: 'success', + txid: tx, + }) + actions.reloadAccount() + } catch (e: any) { + console.log(e) + + notify({ + title: 'Transaction failed', + description: e.message, + txid: e?.txid, + type: 'error', + }) + } finally { + setSubmitting(false) + onClose() + } + } + return (
@@ -116,22 +185,25 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) {

{t('leverage')}

0.00x

- setInputAmount(x)} /> + setInputAmount(x)} + />

{t('health-impact')}

-

-12%

+

{healthImpact}

-
+ {/*

{t('borrow-value')}

$1,000.00

-
+
*/}