import React, { useMemo, useState } from 'react' import xw from 'xwind' // import { nativeToUi } from '@blockworks-foundation/mango-client/lib/utils' import Modal from './Modal' import AccountSelect from './AccountSelect' import useMangoStore from '../stores/useMangoStore' import useMarketList from '../hooks/useMarketList' import { getSymbolForTokenMintAddress, tokenPrecision } from '../utils/index' import useConnection from '../hooks/useConnection' import { withdraw } from '../utils/mango' import Loading from './Loading' import Button from './Button' const WithdrawModal = ({ isOpen, onClose }) => { const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const { symbols } = useMarketList() const { getTokenIndex } = useMarketList() const { connection, programId } = useConnection() const walletAccounts = useMangoStore((s) => s.wallet.balances) const actions = useMangoStore((s) => s.actions) const depositAccounts = useMemo( () => walletAccounts.filter((acc) => Object.values(symbols).includes(acc.account.mint.toString()) ), [symbols, walletAccounts] ) const [selectedAccount, setSelectedAccount] = useState(depositAccounts[0]) const withdrawDisabled = Number(inputAmount) <= 0 const setMaxForSelectedAccount = () => { const marginAccount = useMangoStore.getState().selectedMarginAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const mintAddress = selectedAccount?.account.mint.toString() const tokenIndex = getTokenIndex(mintAddress) const symbol = getSymbolForTokenMintAddress(mintAddress) const max = marginAccount.getUiDeposit(mangoGroup, tokenIndex) setInputAmount(max.toFixed(tokenPrecision[symbol])) } 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) { console.log('withdrawing') withdraw( connection, programId, mangoGroup, marginAccount, wallet, selectedAccount.account.mint, selectedAccount.publicKey, Number(inputAmount) ) .then((transSig: string) => { actions.fetchWalletBalances() setSubmitting(false) console.log('Successfull withrawal:', transSig) onClose() }) .catch((err) => { setSubmitting(false) console.warn('Error withdrawing:', err) onClose() }) } } return (
X
Select:
setInputAmount(e.target.value)} >
) } export default React.memo(WithdrawModal)