import { ChangeEvent, useState } from 'react' import { useTranslation } from 'next-i18next' import mangoStore from '@store/mangoStore' import { createSolanaMessage, notify } from '../../utils/notifications' import Button, { IconButton } from '../shared/Button' import BounceLoader from '../shared/BounceLoader' import Input from '../forms/Input' import Label from '../forms/Label' import { useWallet } from '@solana/wallet-adapter-react' import InlineNotification from '../shared/InlineNotification' import { MangoAccount } from '@blockworks-foundation/mango-v4' import { ArrowLeftIcon } from '@heroicons/react/20/solid' import useSolBalance from 'hooks/useSolBalance' import { isMangoError } from 'types' import { MAX_ACCOUNTS } from 'utils/constants' import Switch from '@components/forms/Switch' import NotificationCookieStore from '@store/notificationCookieStore' const getNextAccountNumber = (accounts: MangoAccount[]): number => { if (accounts.length > 1) { return ( accounts .map((a) => a.accountNum) .reduce((a, b) => Math.max(a, b), -Infinity) + 1 ) } else if (accounts.length === 1) { return accounts[0].accountNum + 1 } return 0 } const CreateAccountForm = ({ customClose, handleBack, }: { customClose?: () => void handleBack?: () => void }) => { const { t } = useTranslation('common') const [loading, setLoading] = useState(false) const [name, setName] = useState('') const [signToNotifications, setSignToNotifications] = useState(true) //whole context needed to sign msgs const walletContext = useWallet() const { maxSolDeposit } = useSolBalance() const setCookie = NotificationCookieStore((s) => s.setCookie) const handleNewAccount = async () => { const client = mangoStore.getState().client const group = mangoStore.getState().group const mangoAccounts = mangoStore.getState().mangoAccounts const set = mangoStore.getState().set if (!group || !walletContext.wallet) return setLoading(true) try { const newAccountNum = getNextAccountNumber(mangoAccounts) const { signature: tx } = await client.createMangoAccount( group, newAccountNum, name || `Account ${newAccountNum + 1}`, parseInt(MAX_ACCOUNTS.tokenAccounts), // tokens parseInt(MAX_ACCOUNTS.spotOpenOrders), // serum3 parseInt(MAX_ACCOUNTS.perpAccounts), // perps parseInt(MAX_ACCOUNTS.perpOpenOrders), // perp Oo ) if (tx) { if (signToNotifications) { createSolanaMessage(walletContext, setCookie) } const pk = walletContext.wallet.adapter.publicKey const mangoAccounts = await client.getMangoAccountsForOwner(group, pk!) const reloadedMangoAccounts = await Promise.all( mangoAccounts.map((ma) => ma.reloadSerum3OpenOrders(client)), ) const newAccount = mangoAccounts.find( (acc) => acc.accountNum === newAccountNum, ) if (newAccount) { set((s) => { s.mangoAccount.current = newAccount s.mangoAccounts = reloadedMangoAccounts }) } setLoading(false) notify({ title: t('new-account-success'), type: 'success', txid: tx, }) if (customClose) { customClose() } } } catch (e) { console.error(e) setLoading(false) if (!isMangoError(e)) return notify({ title: t('new-account-failed'), txid: e?.txid, type: 'error', }) } } return loading ? (
{t('insufficient-sol')}
{t('enable-notifications')}
{t('asked-sign-transaction')}