import React, { useMemo, useState } from 'react' import { nativeToUi } from '@blockworks-foundation/mango-client/lib/utils' import Modal from './Modal' import AccountSelect from './AccountSelect' import useMangoStore from '../stores/useMangoStore' import useConnection from '../hooks/useConnection' import { depositSrm } from '../utils/mango' import { PublicKey } from '@solana/web3.js' import Loading from './Loading' import Button from './Button' import { ElementTitle } from './styles' import Input from './Input' import { notify } from '../utils/notifications' import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions' import { floorToDecimal, sleep } from '../utils' const DepositSrmModal = ({ 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 mangoSrmAccountsForOwner = useMangoStore( (s) => s.wallet.srmAccountsForOwner ) const depositAccounts = useMemo( () => walletAccounts.filter( (acc) => srmMintAddress === acc.account.mint.toString() ), [walletAccounts, srmMintAddress] ) const [selectedAccount, setSelectedAccount] = useState(depositAccounts[0]) // TODO: remove duplication in AccountSelect const getBalanceForAccount = (account) => { const balance = nativeToUi(account?.account?.amount, SRM_DECIMALS) return floorToDecimal(balance, SRM_DECIMALS).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 if (marginAccount && mangoGroup) { depositSrm( connection, new PublicKey(programId), mangoGroup, wallet, selectedAccount.publicKey, Number(inputAmount), mangoSrmAccountsForOwner?.length ? mangoSrmAccountsForOwner[0].publicKey : undefined ) .then(async (_mangoSrmAcct: PublicKey) => { setSubmitting(false) onClose() await sleep(500) actions.fetchWalletBalances() actions.fetchMangoSrmAccounts() actions.fetchMangoGroup() }) .catch((err) => { setSubmitting(false) console.error(err) notify({ message: 'Could not perform SRM deposit operation', description: '', type: 'error', }) onClose() }) } } return (
X
Contribute SRM
<>
Amount
Max
setInputAmount(e.target.value)} suffix="SRM" />
) } export default React.memo(DepositSrmModal)