import React, { FunctionComponent, useState } from 'react' import { ExclamationCircleIcon, InformationCircleIcon, } from '@heroicons/react/outline' import Input, { Label } from './Input' import AccountSelect from './AccountSelect' import { ElementTitle } from './styles' import useMangoStore from '../stores/useMangoStore' import { getSymbolForTokenMintAddress, trimDecimals, sleep, } from '../utils/index' import Loading from './Loading' import Button from './Button' import Tooltip from './Tooltip' import { notify } from '../utils/notifications' import { deposit } from '../utils/mango' import { useTranslation } from 'next-i18next' import ButtonGroup from './ButtonGroup' import InlineNotification from './InlineNotification' import Modal from './Modal' interface NewAccountProps { onAccountCreation?: (x?) => void } const NewAccount: FunctionComponent = ({ onAccountCreation, }) => { const { t } = useTranslation('common') const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const [invalidAmountMessage, setInvalidAmountMessage] = useState('') const [depositPercentage, setDepositPercentage] = useState('') const [invalidNameMessage, setInvalidNameMessage] = useState('') const [name, setName] = useState('') const walletTokens = useMangoStore((s) => s.wallet.tokens) const actions = useMangoStore((s) => s.actions) const [selectedAccount, setSelectedAccount] = useState(walletTokens[0]) const symbol = getSymbolForTokenMintAddress( selectedAccount?.account?.mint.toString() ) const handleAccountSelect = (account) => { setInputAmount('') setDepositPercentage('') setInvalidAmountMessage('') setSelectedAccount(account) } const handleNewAccountDeposit = () => { setSubmitting(true) deposit({ amount: parseFloat(inputAmount), fromTokenAcc: selectedAccount.account, accountName: name, }) .then(async (response) => { await sleep(1000) actions.fetchWalletTokens() actions.fetchAllMangoAccounts() setSubmitting(false) onAccountCreation(response[0]) notify({ title: 'Mango Account Created', txid: response[1], }) }) .catch((e) => { setSubmitting(false) console.error(e) notify({ title: t('init-error'), description: e.message, type: 'error', }) onAccountCreation() }) } const validateAmountInput = (amount) => { if (Number(amount) <= 0) { setInvalidAmountMessage(t('enter-amount')) } if (Number(amount) > selectedAccount.uiBalance) { setInvalidAmountMessage(t('insufficient-balance-deposit')) } } const onChangeAmountInput = (amount) => { setInputAmount(amount) setDepositPercentage('') setInvalidAmountMessage('') } const onChangeAmountButtons = async (percentage) => { setDepositPercentage(percentage) if (!selectedAccount) { setInvalidAmountMessage(t('supported-assets')) return } const max = selectedAccount.uiBalance const amount = ((parseInt(percentage) / 100) * max).toString() if (percentage === '100') { setInputAmount(amount) } else { setInputAmount(trimDecimals(amount, 6).toString()) } setInvalidAmountMessage('') validateAmountInput(amount) } const validateNameInput = () => { if (name.length >= 33) { setInvalidNameMessage(t('character-limit')) } } const onChangeNameInput = (name) => { setName(name) if (invalidNameMessage) { setInvalidNameMessage('') } } return ( <> {t('create-account')}

{t('insufficient-sol')}

onChangeNameInput(e.target.value)} /> {invalidNameMessage ? (
{invalidNameMessage}
) : null}

{t('initial-deposit')}

validateAmountInput(e.target.value)} value={inputAmount || ''} onChange={(e) => onChangeAmountInput(e.target.value)} suffix={symbol} /> {invalidAmountMessage ? (
{invalidAmountMessage}
) : null}
onChangeAmountButtons(v)} unit="%" values={['25', '50', '75', '100']} />
) } export default NewAccount