import React, { useMemo, useState } from 'react' import { nativeToUi } from '@blockworks-foundation/mango-client/lib/utils' 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 { floorToDecimal, getSymbolForTokenMintAddress, tokenPrecision, } from '../utils/index' import useConnection from '../hooks/useConnection' import { deposit, initMarginAccountAndDeposit } from '../utils/mango' import { PublicKey } from '@solana/web3.js' import Loading from './Loading' import Button from './Button' import { notify } from '../utils/notifications' const DepositModal = ({ isOpen, onClose }) => { const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const { getTokenIndex, symbols } = useMarketList() const { connection, programId } = useConnection() const mintDecimals = useMangoStore((s) => s.selectedMangoGroup.mintDecimals) 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 handleAccountSelect = (account) => { setSelectedAccount(account) setInputAmount('') } // TODO: remove duplication in AccountSelect const getBalanceForAccount = (account) => { const mintAddress = account?.account.mint.toString() const balance = nativeToUi( account?.account?.amount, mintDecimals[getTokenIndex(mintAddress)] ) const symbol = getSymbolForTokenMintAddress(mintAddress) return floorToDecimal(balance, tokenPrecision[symbol]).toString() } const setMaxForSelectedAccount = () => { const max = getBalanceForAccount(selectedAccount) setInputAmount(max) } const handleDeposit = () => { setSubmitting(true) const marginAccount = useMangoStore.getState().selectedMarginAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current console.log('handleDeposit', wallet, walletAccounts) if (!marginAccount && mangoGroup) { initMarginAccountAndDeposit( connection, new PublicKey(programId), mangoGroup, wallet, selectedAccount.account.mint, selectedAccount.publicKey, Number(inputAmount) ) .then((_response: Array) => { actions.fetchWalletBalances() actions.fetchMangoGroup() actions.fetchMarginAccounts() setSubmitting(false) onClose() }) .catch((err) => { setSubmitting(false) console.error(err) notify({ message: 'Could not perform init margin account and deposit operation', type: 'error', }) onClose() }) } else { deposit( connection, programId, mangoGroup, marginAccount, wallet, selectedAccount.account.mint, selectedAccount.publicKey, Number(inputAmount) ) .then((_response: string) => { actions.fetchWalletBalances() actions.fetchMangoGroup() actions.fetchMarginAccounts() setSubmitting(false) onClose() }) .catch((err) => { setSubmitting(false) console.error(err) notify({ message: 'Could not perform deposit operation', type: 'error', }) onClose() }) } } return ( {/* not sure what the below div os for? */}
X
Deposit Funds
Token Account
Amount
Max
setInputAmount(e.target.value)} suffix={getSymbolForTokenMintAddress( selectedAccount?.account?.mint.toString() )} />
) } export default React.memo(DepositModal)