import React, { FunctionComponent, useEffect, useMemo, useState } from 'react' import Modal from './Modal' import Input from './Input' import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import { DECIMALS, tokenPrecision } from '../utils/index' import Loading from './Loading' import Slider from './Slider' import Button, { LinkButton } from './Button' import Switch from './Switch' import Tooltip from './Tooltip' import { ExclamationCircleIcon, InformationCircleIcon, } from '@heroicons/react/outline' import { ChevronLeftIcon, ChevronDownIcon, ChevronUpIcon, } from '@heroicons/react/solid' import { Disclosure } from '@headlessui/react' import Select from './Select' import { withdraw } from '../utils/mango' import { ZERO_I80F48, I80F48, MangoAccount, ONE_I80F48, } from '@blockworks-foundation/mango-client' import { notify } from '../utils/notifications' // const trimDecimals = (n, digits) => { // const step = Math.pow(10, digits || 0) // const temp = Math.trunc(step * n) // return temp / step // } interface WithdrawModalProps { onClose: () => void isOpen: boolean title?: string tokenSymbol?: string borrow?: boolean } const WithdrawModal: FunctionComponent = ({ isOpen, onClose, tokenSymbol = '', borrow = false, title, }) => { const [withdrawTokenSymbol, setWithdrawTokenSymbol] = useState( tokenSymbol || 'USDC' ) const [inputAmount, setInputAmount] = useState('') const [invalidAmountMessage, setInvalidAmountMessage] = useState('') const [maxAmount, setMaxAmount] = useState(0) const [submitting, setSubmitting] = useState(false) const [includeBorrow, setIncludeBorrow] = useState(borrow) const [simulation, setSimulation] = useState(null) const [showSimulation, setShowSimulation] = useState(false) const [sliderPercentage, setSliderPercentage] = useState(0) const [maxButtonTransition, setMaxButtonTransition] = useState(false) const actions = useMangoStore((s) => s.actions) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const selectedMangoAccount = useMangoStore( (s) => s.selectedMangoAccount.current ) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const tokens = useMemo(() => mangoGroupConfig.tokens, [mangoGroupConfig]) const token = useMemo( () => tokens.find((t) => t.symbol === withdrawTokenSymbol), [withdrawTokenSymbol, tokens] ) const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) useEffect(() => { if (!mangoGroup || !selectedMangoAccount || !withdrawTokenSymbol) return const mintDecimals = mangoGroup.tokens[tokenIndex].decimals const deposits = selectedMangoAccount.getUiDeposit( mangoCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) const borrows = selectedMangoAccount.getUiBorrow( mangoCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) const maxValForSelectedAsset = getDepositsForSelectedAsset().mul( mangoGroup.getPrice(tokenIndex, mangoCache) || ONE_I80F48 ) const currentAssetsVal = selectedMangoAccount .getAssetsVal(mangoGroup, mangoCache, 'Init') .sub(maxValForSelectedAsset) const currentLiabsVal = selectedMangoAccount.getLiabsVal( mangoGroup, mangoCache, 'Init' ) const liabsAvail = currentAssetsVal .sub(currentLiabsVal) .sub(I80F48.fromNumber(0.01)) // calculate max withdraw amount const amountToWithdraw = includeBorrow ? liabsAvail .div( (mangoGroup.getPrice(tokenIndex, mangoCache) || ONE_I80F48).mul( mangoGroup.spotMarkets[tokenIndex].initLiabWeight ) ) .add(getDepositsForSelectedAsset()) : getDepositsForSelectedAsset() if (amountToWithdraw.gt(I80F48.fromNumber(0))) { setMaxAmount(amountToWithdraw.toNumber()) } else { setMaxAmount(0) } // simulate change to deposits & borrow based on input amount const parsedInputAmount = inputAmount ? I80F48.fromString(inputAmount) : ZERO_I80F48 let newDeposit = deposits.sub(parsedInputAmount) newDeposit = newDeposit.gt(ZERO_I80F48) ? newDeposit : ZERO_I80F48 let newBorrows = parsedInputAmount.sub(deposits) newBorrows = newBorrows.gt(ZERO_I80F48) ? newBorrows : ZERO_I80F48 newBorrows = newBorrows.add(borrows) // clone MangoAccount and arrays to not modify selectedMangoAccount const simulation = new MangoAccount(null, selectedMangoAccount) simulation.deposits = [...selectedMangoAccount.deposits] simulation.borrows = [...selectedMangoAccount.borrows] // update with simulated values simulation.deposits[tokenIndex] = newDeposit .div(I80F48.fromNumber(Math.pow(10, mintDecimals))) .div(mangoCache.rootBankCache[tokenIndex].depositIndex) simulation.borrows[tokenIndex] = newBorrows .div(I80F48.fromNumber(Math.pow(10, mintDecimals))) .div(mangoCache.rootBankCache[tokenIndex].borrowIndex) const assetsVal = simulation.getAssetsVal(mangoGroup, mangoCache, 'Init') const liabsVal = simulation.getLiabsVal(mangoGroup, mangoCache, 'Init') // const collateralRatio = simulation.getCollateralRatio( // mangoGroup, // prices // ) // const leverage = 1 / Math.max(0, collateralRatio - 1) setSimulation({ assetsVal, liabsVal, }) }, [ includeBorrow, inputAmount, tokenIndex, selectedMangoAccount, mangoGroup, mangoCache, ]) const handleWithdraw = () => { setSubmitting(true) withdraw({ amount: Number(inputAmount), token: mangoGroup.tokens[tokenIndex].mint, allowBorrow: includeBorrow, }) .then((txid: string) => { setSubmitting(false) actions.fetchMangoGroup() actions.fetchMangoAccounts() actions.fetchWalletTokens() notify({ title: 'Withdraw successful', type: 'success', txid, }) onClose() }) .catch((err) => { setSubmitting(false) console.error('Error withdrawing:', err) notify({ title: 'Could not perform withdraw', description: err.message, txid: err.txid, type: 'error', }) onClose() }) } const handleSetSelectedAsset = (symbol) => { setInputAmount('') setSliderPercentage(0) setWithdrawTokenSymbol(symbol) } const getDepositsForSelectedAsset = (): I80F48 => { return selectedMangoAccount.getUiDeposit( mangoCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) } const getBorrowAmount = () => { const tokenBalance = getDepositsForSelectedAsset() const borrowAmount = I80F48.fromString(inputAmount).sub(tokenBalance) return borrowAmount.gt(ZERO_I80F48) ? borrowAmount : 0 } const getAccountStatusColor = ( collateralRatio: number, isRisk?: boolean, isStatus?: boolean ) => { if (collateralRatio < 1.25) { return isRisk ? (
High
) : isStatus ? ( 'bg-th-red' ) : ( 'border-th-red text-th-red' ) } else if (collateralRatio > 1.25 && collateralRatio < 1.5) { return isRisk ? (
Moderate
) : isStatus ? ( 'bg-th-orange' ) : ( 'border-th-orange text-th-orange' ) } else { return isRisk ? (
Low
) : isStatus ? ( 'bg-th-green' ) : ( 'border-th-green text-th-green' ) } } const handleIncludeBorrowSwitch = (checked) => { setIncludeBorrow(checked) setInputAmount('') setSliderPercentage(0) setInvalidAmountMessage('') } const setMaxForSelectedAsset = () => { setInputAmount(getDepositsForSelectedAsset().toFixed()) setSliderPercentage(100) setInvalidAmountMessage('') setMaxButtonTransition(true) } const setMaxBorrowForSelectedAsset = async () => { console.log('setting max borrow for selected', maxAmount) setInputAmount(maxAmount.toString()) setSliderPercentage(100) setInvalidAmountMessage('') setMaxButtonTransition(true) } const onChangeAmountInput = (amount: string) => { setInputAmount(amount) setSliderPercentage((Number(amount) / maxAmount) * 100) setInvalidAmountMessage('') } const onChangeSlider = async (percentage) => { const amount = (percentage / 100) * maxAmount if (percentage === 100) { setInputAmount(maxAmount.toString()) } else { setInputAmount(amount.toString()) } setSliderPercentage(percentage) setInvalidAmountMessage('') validateAmountInput(amount) } const validateAmountInput = (amount) => { const parsedAmount = Number(amount) if ( (parsedAmount <= 0 && getDepositsForSelectedAsset().gt(ZERO_I80F48)) || (parsedAmount <= 0 && includeBorrow) ) { setInvalidAmountMessage('Enter an amount to withdraw') } if ( (getDepositsForSelectedAsset() === ZERO_I80F48 || getDepositsForSelectedAsset().lt(I80F48.fromNumber(parsedAmount))) && !includeBorrow ) { setInvalidAmountMessage('Insufficient balance. Borrow funds to withdraw') } } useEffect(() => { if (simulation && simulation.collateralRatio < 1.2 && includeBorrow) { setInvalidAmountMessage( 'Leverage too high. Reduce the amount to withdraw' ) } }, [simulation]) const getTokenBalances = () => { const mangoCache = useMangoStore.getState().selectedMangoGroup.cache const mangoGroup = useMangoStore.getState().selectedMangoGroup.current return tokens.map((token) => { const tokenIndex = mangoGroup.getTokenIndex(token.mintKey) return { symbol: token.symbol, balance: selectedMangoAccount .getUiDeposit( mangoCache.rootBankCache[tokenIndex], mangoGroup, tokenIndex ) .toFixed(tokenPrecision[token.symbol]), } }) } // turn off slider transition for dragging slider handle interaction useEffect(() => { if (maxButtonTransition) { setMaxButtonTransition(false) } }, [maxButtonTransition]) // turn on borrow toggle when asset balance is zero // useEffect(() => { // if (withdrawTokenSymbol && getDepositsForSelectedAsset() === 0) { // setIncludeBorrow(true) // } // }, [withdrawTokenSymbol]) if (!withdrawTokenSymbol) return null return ( <> {!showSimulation ? ( <> {title ? title : 'Withdraw Funds'}
Asset
Borrow Funds
handleIncludeBorrowSwitch(checked)} />
Amount
validateAmountInput(e.target.value)} onChange={(e) => onChangeAmountInput(e.target.value)} suffix={withdrawTokenSymbol} /> {/* {simulation ? ( {simulation.leverage < 5 ? simulation.leverage.toFixed(2) : '>5'} x ) : null} */}
{invalidAmountMessage ? (
{invalidAmountMessage}
) : null}
onChangeSlider(v)} step={1} maxButtonTransition={maxButtonTransition} />
) : null} {showSimulation && simulation ? ( <> Confirm Withdraw {simulation.collateralRatio < 1.2 ? (
Prices have changed and increased your leverage. Reduce the withdrawal amount.
) : null}
{`You're about to withdraw`}
{inputAmount} {withdrawTokenSymbol}
{getBorrowAmount() > 0 ? (
{`Includes borrow of ~${getBorrowAmount().toFixed( DECIMALS[withdrawTokenSymbol] )} ${withdrawTokenSymbol}`}
) : null}
{({ open }) => ( <>
{/* */} {/* */} Account Health Check
{open ? ( ) : ( )}
Account Value
${simulation.assetsVal.toFixed(2)}
Account Risk
{getAccountStatusColor( simulation.collateralRatio, true )}
Leverage
{/* {simulation.leverage.toFixed(2)}x */} X.XXx
{simulation.liabsVal > 0.05 ? (
Borrow Value
${simulation.liabsVal.toFixed(2)}
) : null}
)}
setShowSimulation(false)} > Back ) : null}
) } export default React.memo(WithdrawModal)