import React, { useMemo, useState } from 'react' import Modal from './Modal' import Input from './Input' import AccountSelect from './AccountSelect' import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import useMarketList from '../hooks/useMarketList' import { getSymbolForTokenMintAddress, displayDepositsForMarginAccount, floorToDecimal, } from '../utils/index' import useConnection from '../hooks/useConnection' import { borrowAndWithdraw, withdraw } from '../utils/mango' import Loading from './Loading' import Button from './Button' import { notify } from '../utils/notifications' import Switch from './Switch' import Tooltip from './Tooltip' import { InformationCircleIcon } from '@heroicons/react/outline' import { Transition } from '@headlessui/react' import { PublicKey } from '@solana/web3.js' const WithdrawModal = ({ isOpen, onClose }) => { const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const [includeBorrow, setIncludeBorrow] = useState(false) const { getTokenIndex, symbols } = useMarketList() const { connection, programId } = useConnection() const walletAccounts = useMangoStore((s) => s.wallet.balances) const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mintDecimals = useMangoStore((s) => s.selectedMangoGroup.mintDecimals) const selectedMarginAccount = useMangoStore( (s) => s.selectedMarginAccount.current ) const actions = useMangoStore((s) => s.actions) const withdrawAccounts = useMemo( () => walletAccounts.filter((acc) => Object.values(symbols).includes(acc.account.mint.toString()) ), [symbols, walletAccounts] ) const [selectedAccount, setSelectedAccount] = useState(withdrawAccounts[0]) const mintAddress = useMemo(() => selectedAccount?.account.mint.toString(), [ selectedAccount, ]) const tokenIndex = useMemo(() => getTokenIndex(mintAddress), [ mintAddress, getTokenIndex, ]) const handleSetSelectedAccount = (val) => { setInputAmount('') setSelectedAccount(val) } const withdrawDisabled = Number(inputAmount) <= 0 const getMaxForSelectedAccount = () => { const marginAccount = useMangoStore.getState().selectedMarginAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current return displayDepositsForMarginAccount( marginAccount, mangoGroup, tokenIndex ) } const setMaxForSelectedAccount = () => { setInputAmount(getMaxForSelectedAccount().toString()) } const handleIncludeBorrowSwitch = (checked) => { setIncludeBorrow(checked) setInputAmount('') } const setMaxBorrowForSelectedAccount = async () => { // get index prices const prices = await selectedMangoGroup.getPrices(connection) // get value of margin account assets minus the selected token const assetsVal = selectedMarginAccount.getAssetsVal(selectedMangoGroup, prices) - getMaxForSelectedAccount() * prices[tokenIndex] const currentLiabs = selectedMarginAccount.getLiabsVal( selectedMangoGroup, prices ) // multiply by 0.99 and subtract 0.01 to account for rounding issues const liabsAvail = (assetsVal / 1.2 - currentLiabs) * 0.99 - 0.01 const amountToWithdraw = liabsAvail / prices[tokenIndex] + getMaxForSelectedAccount() if (amountToWithdraw > 0) { setInputAmount( floorToDecimal( amountToWithdraw, mintDecimals[getTokenIndex(mintAddress)] ).toString() ) } else { setInputAmount('0') } } const handleWithdraw = () => { setSubmitting(true) const marginAccount = useMangoStore.getState().selectedMarginAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current if (!marginAccount || !mangoGroup) return if (!includeBorrow) { withdraw( connection, new PublicKey(programId), mangoGroup, marginAccount, wallet, selectedAccount.account.mint, selectedAccount.publicKey, Number(inputAmount) ) .then((_transSig: string) => { setSubmitting(false) actions.fetchMangoGroup() actions.fetchMarginAccounts() actions.fetchWalletBalances() onClose() }) .catch((err) => { setSubmitting(false) console.warn('Error withdrawing:', err) notify({ message: 'Could not perform withdraw', description: `${err}`, type: 'error', }) onClose() }) } else { borrowAndWithdraw( connection, new PublicKey(programId), mangoGroup, marginAccount, wallet, selectedAccount.account.mint, selectedAccount.publicKey, Number(inputAmount) ) .then((_transSig: string) => { setSubmitting(false) actions.fetchMangoGroup() actions.fetchMarginAccounts() actions.fetchWalletBalances() onClose() }) .catch((err) => { setSubmitting(false) console.warn('Error borrowing and withdrawing:', err) notify({ message: 'Could not perform borrow and withdraw', description: `${err}`, type: 'error', }) onClose() }) } } const getBorrowAmount = () => { const tokenBalance = getMaxForSelectedAccount() const borrowAmount = parseFloat(inputAmount) - tokenBalance return borrowAmount > 0 ? borrowAmount : 0 } if (!selectedAccount) return null return ( Withdraw Funds <>
Borrow Funds
handleIncludeBorrowSwitch(checked)} />
Amount
{includeBorrow ? 'Max with Borrow' : 'Max'}
setInputAmount(e.target.value)} suffix={getSymbolForTokenMintAddress( selectedAccount?.account?.mint.toString() )} />
{includeBorrow ? (
Borrow Amount
{`${getBorrowAmount()} ${getSymbolForTokenMintAddress( selectedAccount?.account?.mint.toString() )}`}
Current APR
{(selectedMangoGroup.getBorrowRate(tokenIndex) * 100).toFixed( 2 )} %
) : null}
) } export default React.memo(WithdrawModal)