mango-v4-ui/components/account/CreateAccountForm.tsx

165 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-11-17 20:43:23 -08:00
import { ChangeEvent, useState } from 'react'
2022-10-07 04:47:15 -07:00
import { useTranslation } from 'next-i18next'
import mangoStore from '@store/mangoStore'
import { createSolanaMessage, notify } from '../../utils/notifications'
2022-10-07 04:47:15 -07:00
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'
2022-11-16 17:53:33 -08:00
import useSolBalance from 'hooks/useSolBalance'
2023-02-27 23:20:11 -08:00
import { isMangoError } from 'types'
2023-08-24 05:05:22 -07:00
import { MAX_ACCOUNTS } from 'utils/constants'
import Switch from '@components/forms/Switch'
import NotificationCookieStore from '@store/notificationCookieStore'
2022-10-07 04:47:15 -07:00
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()
2022-11-17 20:43:23 -08:00
const { maxSolDeposit } = useSolBalance()
const setCookie = NotificationCookieStore((s) => s.setCookie)
2022-10-07 04:47:15 -07:00
const handleNewAccount = async () => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
2022-10-25 19:39:11 -07:00
const mangoAccounts = mangoStore.getState().mangoAccounts
2022-10-07 04:47:15 -07:00
const set = mangoStore.getState().set
if (!group || !walletContext.wallet) return
2022-10-07 04:47:15 -07:00
setLoading(true)
try {
const newAccountNum = getNextAccountNumber(mangoAccounts)
const { signature: tx } = await client.createMangoAccount(
2022-10-07 04:47:15 -07:00
group,
newAccountNum,
2022-10-29 18:46:56 -07:00
name || `Account ${newAccountNum + 1}`,
2023-08-23 18:40:40 -07:00
parseInt(MAX_ACCOUNTS.tokenAccounts), // tokens
parseInt(MAX_ACCOUNTS.spotOpenOrders), // serum3
parseInt(MAX_ACCOUNTS.perpAccounts), // perps
parseInt(MAX_ACCOUNTS.perpOpenOrders), // perp Oo
2022-10-07 04:47:15 -07:00
)
if (tx) {
if (signToNotifications) {
createSolanaMessage(walletContext, setCookie)
}
const pk = walletContext.wallet.adapter.publicKey
2022-10-25 19:39:11 -07:00
const mangoAccounts = await client.getMangoAccountsForOwner(group, pk!)
2022-11-15 09:10:44 -08:00
const reloadedMangoAccounts = await Promise.all(
2023-07-21 11:47:53 -07:00
mangoAccounts.map((ma) => ma.reloadSerum3OpenOrders(client)),
2022-11-15 09:10:44 -08:00
)
2022-10-07 04:47:15 -07:00
const newAccount = mangoAccounts.find(
2023-07-21 11:47:53 -07:00
(acc) => acc.accountNum === newAccountNum,
2022-10-07 04:47:15 -07:00
)
2022-10-31 09:39:43 -07:00
if (newAccount) {
set((s) => {
s.mangoAccount.current = newAccount
2022-11-15 09:10:44 -08:00
s.mangoAccounts = reloadedMangoAccounts
2022-10-31 09:39:43 -07:00
})
}
2022-10-07 04:47:15 -07:00
setLoading(false)
notify({
title: t('new-account-success'),
type: 'success',
txid: tx,
})
if (customClose) {
customClose()
}
}
2023-02-27 23:20:11 -08:00
} catch (e) {
console.error(e)
2022-10-07 04:47:15 -07:00
setLoading(false)
2023-02-27 23:20:11 -08:00
if (!isMangoError(e)) return
2022-10-07 04:47:15 -07:00
notify({
title: t('new-account-failed'),
2022-12-02 15:47:08 -08:00
txid: e?.txid,
2022-10-07 04:47:15 -07:00
type: 'error',
})
}
}
return loading ? (
<div className="flex h-full flex-col items-center justify-between">
<BounceLoader loadingMessage={t('creating-account')} />
</div>
) : (
<div className="flex h-full flex-col justify-between">
<div className="pb-3">
2022-10-07 04:47:15 -07:00
<div className="flex items-center">
{handleBack ? (
<IconButton className="mr-3" onClick={handleBack} size="small">
<ArrowLeftIcon className="h-5 w-5" />
</IconButton>
) : null}
2022-12-04 15:34:17 -08:00
<h2 className="w-full text-center">{t('create-account')}</h2>
{handleBack ? <div className="h-5 w-5" /> : null}
2022-10-07 04:47:15 -07:00
</div>
<p className="mt-1 text-center">{t('insufficient-sol')}</p>
2022-10-07 04:47:15 -07:00
<div className="pt-4">
<Label optional text={t('account-name')} />
</div>
<Input
type="text"
name="name"
id="name"
placeholder="Account"
value={name}
onChange={(e: ChangeEvent<HTMLInputElement>) =>
setName(e.target.value)
}
2023-01-25 01:19:12 -08:00
maxLength={30}
2022-10-07 04:47:15 -07:00
/>
<div className="my-3 flex items-center justify-between rounded-md border border-th-bkg-3 px-3 py-2">
<div>
<p className="text-th-fgd-2">{t('enable-notifications')}</p>
<p className="text-xs">{t('asked-sign-transaction')}</p>
</div>
<Switch
className="text-th-fgd-3"
checked={signToNotifications}
onChange={(checked) => setSignToNotifications(checked)}
/>
</div>
2022-11-17 20:43:23 -08:00
{maxSolDeposit <= 0 ? (
<InlineNotification type="error" desc={t('deposit-more-sol')} />
) : null}
2022-10-07 04:47:15 -07:00
</div>
<Button
className="mt-6 w-full"
disabled={maxSolDeposit <= 0}
onClick={handleNewAccount}
size="large"
>
{t('create-account')}
</Button>
2022-10-07 04:47:15 -07:00
</div>
)
}
export default CreateAccountForm