import React, { useMemo, useState } from 'react' import Modal from './Modal' import MangoSrmAccountSelector from './MangoSrmAccountSelector' import useMangoStore from '../stores/useMangoStore' import useConnection from '../hooks/useConnection' import { withdrawSrm } from '../utils/mango' import Loading from './Loading' import Button from './Button' import { notify } from '../utils/notifications' import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions' import { PublicKey } from '@solana/web3.js' import { XIcon } from '@heroicons/react/outline' const WithdrawModal = ({ isOpen, onClose }) => { const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const { connection, programId } = useConnection() const walletAccounts = useMangoStore((s) => s.wallet.balances) const actions = useMangoStore((s) => s.actions) const srmMintAddress = useMangoStore((s) => s.connection.srmMint) const contributedSrm = useMangoStore((s) => s.wallet.contributedSrm) const mangoSrmAccountsForOwner = useMangoStore( (s) => s.wallet.srmAccountsForOwner ) const walletSrmAccount = useMemo( () => walletAccounts.find( (acc) => srmMintAddress === acc.account.mint.toString() ), [walletAccounts, srmMintAddress] ) const [selectedAccount, setSelectedAccount] = useState( mangoSrmAccountsForOwner[0] ) const withdrawDisabled = Number(inputAmount) <= 0 const setMaxForSelectedAccount = () => { setInputAmount(contributedSrm.toFixed(SRM_DECIMALS)) } 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) { withdrawSrm( connection, new PublicKey(programId), mangoGroup, selectedAccount, wallet, walletSrmAccount.publicKey, Number(inputAmount) ) .then((transSig: string) => { setSubmitting(false) notify({ message: `Withdrew ${inputAmount} SRM into your account`, txid: `${transSig}`, type: 'info', }) onClose() actions.fetchWalletBalances() actions.fetchMangoSrmAccounts() actions.fetchMangoGroup() }) .catch((err) => { setSubmitting(false) console.warn('Error withdrawing:', err) notify({ message: 'Could not perform withdraw operation', description: `${err}`, type: 'error', }) onClose() }) } } return (
X
Select:
setInputAmount(e.target.value)} >
) } export default React.memo(WithdrawModal)