2021-07-23 07:07:05 -07:00
|
|
|
import React, { FunctionComponent, useEffect, useMemo, useState } from 'react'
|
|
|
|
import {
|
|
|
|
ExclamationCircleIcon,
|
|
|
|
InformationCircleIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
2021-06-20 18:21:37 -07:00
|
|
|
// import {
|
|
|
|
// nativeToUi,
|
|
|
|
// sleep,
|
|
|
|
// } from '@blockworks-foundation/mango-client/lib/utils/src'
|
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,
|
|
|
|
DECIMALS,
|
|
|
|
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'
|
2021-06-23 08:32:33 -07:00
|
|
|
// import { initMangoAccountAndDeposit } from '../utils/mango'
|
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 Loading from './Loading'
|
|
|
|
import Button from './Button'
|
|
|
|
import Slider from './Slider'
|
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-06-16 22:37:35 -07:00
|
|
|
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
|
2021-07-06 15:04:20 -07:00
|
|
|
import { deposit } from '../utils/mango'
|
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-06-16 22:37:35 -07:00
|
|
|
const groupConfig = useMangoGroupConfig()
|
2021-06-18 15:57:13 -07:00
|
|
|
// const tokenMints = useMemo(() => groupConfig.tokens.map(t => t.mintKey.toBase58()), [groupConfig]);
|
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 [inputAmount, setInputAmount] = useState(0)
|
|
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
|
|
|
const [sliderPercentage, setSliderPercentage] = useState(0)
|
|
|
|
const [maxButtonTransition, setMaxButtonTransition] = useState(false)
|
2021-07-23 07:07:05 -07:00
|
|
|
const [showNewAccountName, setShowNewAccountName] = useState(true)
|
|
|
|
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) => {
|
|
|
|
setInputAmount(0)
|
|
|
|
setSliderPercentage(0)
|
|
|
|
setInvalidAmountMessage('')
|
|
|
|
setSelectedAccount(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
const setMaxForSelectedAccount = () => {
|
2021-06-20 18:21:37 -07:00
|
|
|
const max = selectedAccount.uiBalance
|
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
|
|
|
setInputAmount(max)
|
|
|
|
setSliderPercentage(100)
|
|
|
|
setInvalidAmountMessage('')
|
|
|
|
setMaxButtonTransition(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNewAccountDeposit = () => {
|
|
|
|
setSubmitting(true)
|
2021-07-06 15:04:20 -07:00
|
|
|
deposit({
|
|
|
|
amount: inputAmount,
|
|
|
|
fromTokenAcc: selectedAccount.account,
|
|
|
|
})
|
|
|
|
.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-06-23 08:32:33 -07:00
|
|
|
actions.fetchMangoAccounts()
|
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-07-06 15:04:20 -07:00
|
|
|
onAccountCreation(response[0].publicKey)
|
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
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
setSubmitting(false)
|
|
|
|
console.error(err)
|
|
|
|
notify({
|
|
|
|
message:
|
|
|
|
'Could not perform init margin account and deposit operation',
|
|
|
|
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) {
|
|
|
|
setInvalidAmountMessage('Enter an amount to deposit')
|
|
|
|
}
|
2021-06-17 11:03:47 -07:00
|
|
|
if (Number(amount) > selectedAccount.uiBalance) {
|
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(
|
|
|
|
'Insufficient balance. Reduce the amount to deposit'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChangeAmountInput = (amount) => {
|
2021-06-17 11:03:47 -07:00
|
|
|
const max = selectedAccount.uiBalance
|
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
|
|
|
setInputAmount(amount)
|
|
|
|
setSliderPercentage((amount / max) * 100)
|
|
|
|
setInvalidAmountMessage('')
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChangeSlider = async (percentage) => {
|
2021-06-17 11:03:47 -07:00
|
|
|
const max = selectedAccount.uiBalance
|
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 amount = (percentage / 100) * max
|
2021-06-11 11:23:51 -07:00
|
|
|
if (percentage === 100) {
|
|
|
|
setInputAmount(amount)
|
|
|
|
} else {
|
|
|
|
setInputAmount(trimDecimals(amount, DECIMALS[symbol]))
|
|
|
|
}
|
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
|
|
|
setSliderPercentage(percentage)
|
|
|
|
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) {
|
|
|
|
setInvalidNameMessage('Account name must be 32 characters or less')
|
|
|
|
}
|
|
|
|
if (name.length === 0) {
|
|
|
|
setInvalidNameMessage('Enter an account name')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// turn off slider transition for dragging slider handle interaction
|
|
|
|
useEffect(() => {
|
|
|
|
if (maxButtonTransition) {
|
|
|
|
setMaxButtonTransition(false)
|
|
|
|
}
|
|
|
|
}, [maxButtonTransition])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-07-23 07:07:05 -07:00
|
|
|
<ElementTitle noMarignBottom>New Account</ElementTitle>
|
|
|
|
{/* {showNewAccountName ? (
|
|
|
|
<>
|
|
|
|
<div className="flex items-center justify-center text-th-fgd-3 pb-4 pt-2">
|
|
|
|
Create a public nickname for your account
|
|
|
|
<Tooltip content="Account names are stored on-chain">
|
|
|
|
<InformationCircleIcon className="h-5 w-5 ml-2 text-th-primary" />
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<div className="pb-2 text-th-fgd-1">
|
|
|
|
Account Name <span className="text-th-fgd-3">(Optional)</span>
|
|
|
|
</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}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<Button
|
|
|
|
onClick={() => setShowNewAccountName(false)}
|
|
|
|
disabled={name.length >= 33}
|
|
|
|
className="mt-4 w-full"
|
|
|
|
>
|
|
|
|
Next
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
) : ( */}
|
|
|
|
<>
|
|
|
|
<div className="text-th-fgd-3 text-center pb-4 pt-2">
|
|
|
|
Make a deposit to initialize your new account
|
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
|
|
|
</div>
|
2021-07-23 07:07:05 -07:00
|
|
|
<AccountSelect
|
|
|
|
accounts={walletTokens}
|
|
|
|
selectedAccount={selectedAccount}
|
|
|
|
onSelectAccount={handleAccountSelect}
|
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
|
|
|
<div className="flex justify-between pb-2 pt-4">
|
|
|
|
<div className={`text-th-fgd-1`}>Amount</div>
|
|
|
|
<div
|
|
|
|
className="text-th-fgd-1 underline cursor-pointer default-transition hover:text-th-primary hover:no-underline"
|
|
|
|
onClick={setMaxForSelectedAccount}
|
|
|
|
>
|
|
|
|
Max
|
|
|
|
</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
|
|
|
</div>
|
2021-07-23 07:07:05 -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}
|
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
|
|
|
</div>
|
2021-07-23 07:07:05 -07:00
|
|
|
) : null}
|
|
|
|
<div className="pt-3 pb-4">
|
|
|
|
<Slider
|
|
|
|
value={sliderPercentage}
|
|
|
|
onChange={(v) => onChangeSlider(v)}
|
|
|
|
step={1}
|
|
|
|
maxButtonTransition={maxButtonTransition}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={`pt-8 flex justify-center`}>
|
|
|
|
<Button
|
|
|
|
disabled={
|
|
|
|
inputAmount <= 0 || inputAmount > selectedAccount.uiBalance
|
|
|
|
}
|
|
|
|
onClick={handleNewAccountDeposit}
|
|
|
|
className="w-full"
|
|
|
|
>
|
|
|
|
<div className={`flex items-center justify-center`}>
|
|
|
|
{submitting && <Loading className="-ml-1 mr-3" />}
|
|
|
|
Create New Account
|
|
|
|
</div>
|
|
|
|
</Button>
|
|
|
|
</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
|