import { ChangeEvent, useState } from 'react' import { useTranslation } from 'next-i18next' import mangoStore from '@store/mangoStore' import { 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' 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 = ({ isFirstAccount, customClose, handleBack, }: { isFirstAccount?: boolean customClose?: () => void handleBack?: () => void }) => { const { t } = useTranslation('common') const [loading, setLoading] = useState(false) const [name, setName] = useState('') const { wallet } = useWallet() const { maxSolDeposit } = useSolBalance() 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 || !wallet) return setLoading(true) try { const newAccountNum = getNextAccountNumber(mangoAccounts) const tx = await client.createMangoAccount( group, newAccountNum, name || `Account ${newAccountNum + 1}`, undefined, // tokenCount undefined, // serum3Count 8, // perpCount 8 // perpOoCount ) if (tx) { const pk = wallet!.adapter.publicKey const mangoAccounts = await client.getMangoAccountsForOwner(group, pk!) const reloadedMangoAccounts = await Promise.all( mangoAccounts.map((ma) => ma.reloadAccountData(client)) ) const newAccount = mangoAccounts.find( (acc) => acc.accountNum === newAccountNum ) if (newAccount) { await newAccount.reloadAccountData(client) 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: any) { setLoading(false) notify({ title: t('new-account-failed'), txid: e?.txid, type: 'error', }) console.error(e) } } return loading ? (
) : (
{handleBack ? ( ) : null}

{t('create-account')}

{handleBack ?
: null}
{isFirstAccount ? (

You need a Mango Account to get started.

) : null}
) => setName(e.target.value) } charLimit={30} />
{maxSolDeposit <= 0 ? ( ) : null}
) } export default CreateAccountForm