2021-10-26 07:25:36 -07:00
|
|
|
import React, { FunctionComponent, useState } from 'react'
|
2021-07-23 07:07:05 -07:00
|
|
|
import {
|
|
|
|
ExclamationCircleIcon,
|
|
|
|
InformationCircleIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import Input from './Input'
|
|
|
|
import AccountSelect from './AccountSelect'
|
|
|
|
import { ElementTitle } from './styles'
|
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
|
|
|
import {
|
|
|
|
getSymbolForTokenMintAddress,
|
|
|
|
trimDecimals,
|
2021-07-06 15:04:20 -07:00
|
|
|
sleep,
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
} from '../utils/index'
|
|
|
|
import Loading from './Loading'
|
|
|
|
import Button from './Button'
|
2021-07-23 07:07:05 -07:00
|
|
|
import Tooltip from './Tooltip'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
import { notify } from '../utils/notifications'
|
2021-07-06 15:04:20 -07:00
|
|
|
import { deposit } from '../utils/mango'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-10-26 07:25:36 -07:00
|
|
|
import ButtonGroup from './ButtonGroup'
|
|
|
|
import InlineNotification from './InlineNotification'
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
|
|
|
|
interface NewAccountProps {
|
|
|
|
onAccountCreation?: (x?) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
const NewAccount: FunctionComponent<NewAccountProps> = ({
|
|
|
|
onAccountCreation,
|
|
|
|
}) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-10-26 07:25:36 -07:00
|
|
|
const [inputAmount, setInputAmount] = useState<string>('')
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
2021-10-26 07:25:36 -07:00
|
|
|
const [depositPercentage, setDepositPercentage] = useState('')
|
2021-07-23 07:07:05 -07:00
|
|
|
const [invalidNameMessage, setInvalidNameMessage] = useState('')
|
|
|
|
const [name, setName] = useState('')
|
2021-06-17 11:03:47 -07:00
|
|
|
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const actions = useMangoStore((s) => s.actions)
|
2021-06-17 11:03:47 -07:00
|
|
|
|
|
|
|
const [selectedAccount, setSelectedAccount] = useState(walletTokens[0])
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
|
|
|
|
const symbol = getSymbolForTokenMintAddress(
|
|
|
|
selectedAccount?.account?.mint.toString()
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleAccountSelect = (account) => {
|
2021-10-26 07:25:36 -07:00
|
|
|
setInputAmount('')
|
|
|
|
setDepositPercentage('')
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
setInvalidAmountMessage('')
|
|
|
|
setSelectedAccount(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNewAccountDeposit = () => {
|
|
|
|
setSubmitting(true)
|
2021-07-06 15:04:20 -07:00
|
|
|
deposit({
|
2021-10-26 07:25:36 -07:00
|
|
|
amount: parseFloat(inputAmount),
|
2021-07-06 15:04:20 -07:00
|
|
|
fromTokenAcc: selectedAccount.account,
|
2021-08-17 14:52:53 -07:00
|
|
|
accountName: name,
|
2021-07-06 15:04:20 -07:00
|
|
|
})
|
|
|
|
.then(async (response) => {
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
await sleep(1000)
|
2021-06-17 11:03:47 -07:00
|
|
|
actions.fetchWalletTokens()
|
2021-09-29 15:09:35 -07:00
|
|
|
actions.fetchAllMangoAccounts()
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
setSubmitting(false)
|
2021-08-17 13:09:27 -07:00
|
|
|
console.log('response', response)
|
|
|
|
|
2021-08-17 10:47:52 -07:00
|
|
|
onAccountCreation(response)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
})
|
2021-08-24 09:28:25 -07:00
|
|
|
.catch((e) => {
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
setSubmitting(false)
|
2021-08-24 09:28:25 -07:00
|
|
|
console.error(e)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
notify({
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('init-error'),
|
2021-08-24 09:28:25 -07:00
|
|
|
description: e.message,
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
onAccountCreation()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:23:51 -07:00
|
|
|
const validateAmountInput = (amount) => {
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
if (Number(amount) <= 0) {
|
2021-10-20 05:42:40 -07:00
|
|
|
setInvalidAmountMessage(t('enter-amount'))
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
}
|
2021-06-17 11:03:47 -07:00
|
|
|
if (Number(amount) > selectedAccount.uiBalance) {
|
2021-10-20 05:42:40 -07:00
|
|
|
setInvalidAmountMessage(t('insufficient-balance-deposit'))
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChangeAmountInput = (amount) => {
|
|
|
|
setInputAmount(amount)
|
2021-10-26 07:25:36 -07:00
|
|
|
setDepositPercentage('')
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
setInvalidAmountMessage('')
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:25:36 -07:00
|
|
|
const onChangeAmountButtons = async (percentage) => {
|
|
|
|
setDepositPercentage(percentage)
|
|
|
|
|
|
|
|
if (!selectedAccount) {
|
|
|
|
setInvalidAmountMessage(t('supported-assets'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:03:47 -07:00
|
|
|
const max = selectedAccount.uiBalance
|
2021-10-26 07:25:36 -07:00
|
|
|
const amount = ((parseInt(percentage) / 100) * max).toString()
|
|
|
|
if (percentage === '100') {
|
2021-06-11 11:23:51 -07:00
|
|
|
setInputAmount(amount)
|
|
|
|
} else {
|
2021-10-26 07:25:36 -07:00
|
|
|
setInputAmount(trimDecimals(amount, 6).toString())
|
2021-06-11 11:23:51 -07:00
|
|
|
}
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
setInvalidAmountMessage('')
|
2021-06-11 11:23:51 -07:00
|
|
|
validateAmountInput(amount)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:07:05 -07:00
|
|
|
const validateNameInput = () => {
|
|
|
|
if (name.length >= 33) {
|
2021-10-20 05:42:40 -07:00
|
|
|
setInvalidNameMessage(t('character-limit'))
|
2021-07-23 07:07:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChangeNameInput = (name) => {
|
|
|
|
setName(name)
|
|
|
|
if (invalidNameMessage) {
|
|
|
|
setInvalidNameMessage('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
return (
|
|
|
|
<>
|
2021-10-26 07:25:36 -07:00
|
|
|
<ElementTitle>Create Account</ElementTitle>
|
|
|
|
<div className="mx-auto pb-4 text-center text-th-fgd-3 text-xs">
|
|
|
|
{t('insufficient-sol')}
|
|
|
|
</div>
|
|
|
|
<div className="border-b border-th-bkg-4 mb-4 pb-6">
|
2021-08-18 23:15:25 -07:00
|
|
|
<div className="flex items-center pb-2 text-th-fgd-1">
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('account-name')}{' '}
|
|
|
|
<span className="ml-1 text-th-fgd-3">{t('optional')}</span>
|
|
|
|
<Tooltip content={t('tooltip-name-onchain')}>
|
2021-08-18 23:15:25 -07:00
|
|
|
<InformationCircleIcon className="h-5 w-5 ml-2 text-th-primary" />
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
className={`border border-th-fgd-4 flex-grow`}
|
|
|
|
error={!!invalidNameMessage}
|
|
|
|
placeholder="e.g. Calypso"
|
|
|
|
value={name}
|
|
|
|
onBlur={validateNameInput}
|
|
|
|
onChange={(e) => onChangeNameInput(e.target.value)}
|
|
|
|
/>
|
|
|
|
{invalidNameMessage ? (
|
|
|
|
<div className="flex items-center pt-1.5 text-th-red">
|
|
|
|
<ExclamationCircleIcon className="h-4 w-4 mr-1.5" />
|
|
|
|
{invalidNameMessage}
|
2021-07-23 07:07:05 -07:00
|
|
|
</div>
|
2021-08-18 23:15:25 -07:00
|
|
|
) : null}
|
|
|
|
</div>
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className="font-bold pb-2 text-center text-th-fgd-2">
|
|
|
|
{t('initial-deposit')}
|
|
|
|
</div>
|
2021-08-18 23:15:25 -07:00
|
|
|
<AccountSelect
|
|
|
|
accounts={walletTokens}
|
|
|
|
selectedAccount={selectedAccount}
|
|
|
|
onSelectAccount={handleAccountSelect}
|
|
|
|
/>
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className={`text-th-fgd-1 pb-2 pt-4`}>{t('amount')}</div>
|
2021-08-18 23:15:25 -07:00
|
|
|
<div className="flex">
|
|
|
|
<Input
|
|
|
|
type="number"
|
|
|
|
min="0"
|
|
|
|
className={`border border-th-fgd-4 flex-grow pr-11`}
|
|
|
|
placeholder="0.00"
|
|
|
|
error={!!invalidAmountMessage}
|
|
|
|
onBlur={(e) => validateAmountInput(e.target.value)}
|
|
|
|
value={inputAmount || ''}
|
|
|
|
onChange={(e) => onChangeAmountInput(e.target.value)}
|
|
|
|
suffix={symbol}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{invalidAmountMessage ? (
|
|
|
|
<div className="flex items-center pt-1.5 text-th-red">
|
|
|
|
<ExclamationCircleIcon className="h-4 w-4 mr-1.5" />
|
|
|
|
{invalidAmountMessage}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className="pt-1">
|
|
|
|
<ButtonGroup
|
|
|
|
activeValue={depositPercentage}
|
|
|
|
onChange={(v) => onChangeAmountButtons(v)}
|
|
|
|
unit="%"
|
|
|
|
values={['25', '50', '75', '100']}
|
2021-08-18 23:15:25 -07:00
|
|
|
/>
|
|
|
|
</div>
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className={`flex justify-center pt-6`}>
|
2021-08-18 23:15:25 -07:00
|
|
|
<Button
|
2021-10-26 07:25:36 -07:00
|
|
|
disabled={
|
|
|
|
parseFloat(inputAmount) <= 0 ||
|
|
|
|
parseFloat(inputAmount) > selectedAccount.uiBalance
|
|
|
|
}
|
2021-08-18 23:15:25 -07:00
|
|
|
onClick={handleNewAccountDeposit}
|
|
|
|
className="w-full"
|
|
|
|
>
|
|
|
|
<div className={`flex items-center justify-center`}>
|
|
|
|
{submitting && <Loading className="-ml-1 mr-3" />}
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('lets-go')}
|
2021-07-23 07:07:05 -07:00
|
|
|
</div>
|
2021-08-18 23:15:25 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className="pt-3">
|
|
|
|
<InlineNotification desc={t('interest-info')} type="info" />
|
2021-09-08 12:11:02 -07:00
|
|
|
</div>
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default NewAccount
|