2021-07-01 07:12:42 -07:00
|
|
|
import React, { FunctionComponent, useEffect, useState } from 'react'
|
2021-08-16 10:00:43 -07:00
|
|
|
import { ExclamationCircleIcon } from '@heroicons/react/outline'
|
2021-04-09 17:01:00 -07:00
|
|
|
import Modal from './Modal'
|
2021-04-18 03:34:37 -07:00
|
|
|
import Input from './Input'
|
2021-04-09 17:01:00 -07:00
|
|
|
import AccountSelect from './AccountSelect'
|
2021-04-18 03:34:37 -07:00
|
|
|
import { ElementTitle } from './styles'
|
2021-06-18 21:26:47 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-04-09 17:01:00 -07:00
|
|
|
import Loading from './Loading'
|
2021-10-26 07:25:36 -07:00
|
|
|
import Button from './Button'
|
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 InlineNotification from './InlineNotification'
|
2021-06-21 06:54:56 -07:00
|
|
|
import { deposit } from '../utils/mango'
|
2021-07-06 15:04:20 -07:00
|
|
|
import { notify } from '../utils/notifications'
|
2021-10-26 07:25:36 -07:00
|
|
|
import { sleep, trimDecimals } from '../utils'
|
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'
|
2021-04-09 17:01:00 -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
|
|
|
interface DepositModalProps {
|
|
|
|
onClose: () => void
|
|
|
|
isOpen: boolean
|
2021-11-09 16:34:03 -08:00
|
|
|
repayAmount?: 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
|
|
|
tokenSymbol?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|
|
|
isOpen,
|
|
|
|
onClose,
|
2021-11-09 16:34:03 -08:00
|
|
|
repayAmount,
|
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
|
|
|
tokenSymbol = '',
|
|
|
|
}) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-11-09 16:34:03 -08:00
|
|
|
const [inputAmount, setInputAmount] = useState<string>(repayAmount || '')
|
2021-04-09 17:01:00 -07:00
|
|
|
const [submitting, setSubmitting] = useState(false)
|
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 [invalidAmountMessage, setInvalidAmountMessage] = useState('')
|
2021-10-26 07:25:36 -07:00
|
|
|
const [depositPercentage, setDepositPercentage] = useState('')
|
2021-06-17 11:03:47 -07:00
|
|
|
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
2021-04-09 17:01:00 -07:00
|
|
|
const actions = useMangoStore((s) => s.actions)
|
2021-06-17 11:03:47 -07:00
|
|
|
const [selectedAccount, setSelectedAccount] = useState(walletTokens[0])
|
2021-09-08 12:11:02 -07:00
|
|
|
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
2021-04-09 17:01:00 -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
|
|
|
useEffect(() => {
|
|
|
|
if (tokenSymbol) {
|
2021-06-17 11:03:47 -07:00
|
|
|
const symbolAccount = walletTokens.find(
|
|
|
|
(a) => a.config.symbol === tokenSymbol
|
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 (symbolAccount) {
|
|
|
|
setSelectedAccount(symbolAccount)
|
|
|
|
} else {
|
|
|
|
setSelectedAccount(null)
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 11:03:47 -07:00
|
|
|
}, [tokenSymbol, walletTokens])
|
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-04-18 06:45:11 -07:00
|
|
|
const handleAccountSelect = (account) => {
|
2021-07-23 10:07:31 -07:00
|
|
|
setInputAmount('')
|
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-04-18 06:45:11 -07:00
|
|
|
setSelectedAccount(account)
|
|
|
|
}
|
|
|
|
|
2021-04-09 17:01:00 -07:00
|
|
|
const handleDeposit = () => {
|
2021-07-06 15:04:20 -07:00
|
|
|
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
|
|
|
|
2021-04-09 17:01:00 -07:00
|
|
|
setSubmitting(true)
|
2021-06-17 18:10:47 -07:00
|
|
|
deposit({
|
2021-07-06 15:04:20 -07:00
|
|
|
amount: parseFloat(inputAmount),
|
2021-06-18 08:37:42 -07:00
|
|
|
fromTokenAcc: selectedAccount.account,
|
2021-07-06 15:04:20 -07:00
|
|
|
mangoAccount,
|
2021-06-17 18:10:47 -07:00
|
|
|
})
|
2021-07-06 15:04:20 -07:00
|
|
|
.then((response) => {
|
|
|
|
notify({
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('deposit-successful'),
|
2021-07-06 17:13:17 -07:00
|
|
|
type: 'success',
|
2021-08-17 13:47:57 -07:00
|
|
|
txid: response.toString(),
|
2021-07-06 15:04:20 -07:00
|
|
|
})
|
|
|
|
setSubmitting(false)
|
|
|
|
onClose()
|
2021-08-31 10:47:03 -07:00
|
|
|
sleep(500).then(() => {
|
2021-08-31 14:50:17 -07:00
|
|
|
mangoAccount
|
|
|
|
? actions.reloadMangoAccount()
|
2021-09-29 15:09:35 -07:00
|
|
|
: actions.fetchAllMangoAccounts()
|
2021-08-31 11:11:41 -07:00
|
|
|
actions.fetchWalletTokens()
|
2021-08-31 10:47:03 -07:00
|
|
|
})
|
2021-07-06 15:04:20 -07:00
|
|
|
})
|
2021-07-06 17:13:17 -07:00
|
|
|
.catch((err) => {
|
2021-07-06 15:04:20 -07:00
|
|
|
notify({
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('deposit-failed'),
|
2021-07-06 17:13:17 -07:00
|
|
|
description: err.message,
|
2021-12-28 14:22:50 -08:00
|
|
|
txid: err?.txid,
|
2021-07-06 15:04:20 -07:00
|
|
|
type: 'error',
|
|
|
|
})
|
2022-01-08 12:52:42 -08:00
|
|
|
onClose()
|
2021-07-06 15:04:20 -07:00
|
|
|
})
|
2021-04-09 17:01:00 -07:00
|
|
|
}
|
|
|
|
|
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 (selectedAccount && 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-06-17 11:03:47 -07:00
|
|
|
|
|
|
|
if (!selectedAccount) {
|
2021-10-20 05:42:40 -07:00
|
|
|
setInvalidAmountMessage(t('supported-assets'))
|
2021-06-17 11:03:47 -07:00
|
|
|
return
|
|
|
|
}
|
2021-06-17 15:38:17 -07:00
|
|
|
|
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)
|
2021-06-17 11:03:47 -07:00
|
|
|
|
|
|
|
if (!selectedAccount) {
|
2021-10-20 05:42:40 -07:00
|
|
|
setInvalidAmountMessage(t('supported-assets'))
|
2021-06-17 11:03:47 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const max = selectedAccount.uiBalance
|
2021-10-26 07:25:36 -07:00
|
|
|
const amount = ((parseInt(percentage) / 100) * max).toString()
|
|
|
|
if (percentage === '100') {
|
|
|
|
setInputAmount(amount)
|
2021-06-11 11:23:51 -07:00
|
|
|
} 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-11-09 16:34:03 -08:00
|
|
|
const percentage = (parseFloat(inputAmount) / parseFloat(repayAmount)) * 100
|
|
|
|
const net = parseFloat(inputAmount) - parseFloat(repayAmount)
|
|
|
|
const repayMessage =
|
|
|
|
percentage === 100
|
|
|
|
? t('repay-full')
|
|
|
|
: percentage > 100
|
|
|
|
? t('repay-and-deposit', {
|
|
|
|
amount: trimDecimals(net, 6).toString(),
|
|
|
|
symbol: selectedAccount.config.symbol,
|
|
|
|
})
|
|
|
|
: t('repay-partial', {
|
|
|
|
percentage: percentage.toFixed(2),
|
|
|
|
})
|
|
|
|
|
2021-11-18 08:49:48 -08:00
|
|
|
const inputDisabled =
|
2021-12-08 02:01:55 -08:00
|
|
|
selectedAccount &&
|
2021-11-18 08:49:48 -08:00
|
|
|
selectedAccount.config.symbol === 'SOL' &&
|
|
|
|
selectedAccount.uiBalance.toString() === inputAmount
|
|
|
|
|
2021-04-09 17:01:00 -07:00
|
|
|
return (
|
2021-08-16 10:59:44 -07:00
|
|
|
<Modal isOpen={isOpen} onClose={onClose}>
|
2021-12-17 04:23:58 -08:00
|
|
|
<ElementTitle noMarignBottom>{t('deposit-funds')}</ElementTitle>
|
|
|
|
{!mangoAccount ? (
|
|
|
|
<div className="mb-4 mt-2 text-center text-th-fgd-3 text-xs">
|
|
|
|
{t('first-deposit-desc')}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-10-26 07:25:36 -07:00
|
|
|
{tokenSymbol && !selectedAccount ? (
|
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification
|
|
|
|
desc={t('deposit-help', { tokenSymbol: tokenSymbol })}
|
|
|
|
title={t('no-address', { tokenSymbol: tokenSymbol })}
|
|
|
|
type="error"
|
2021-04-18 03:34:37 -07:00
|
|
|
/>
|
2021-10-26 07:25:36 -07:00
|
|
|
</div>
|
|
|
|
) : null}
|
2021-12-08 02:01:55 -08:00
|
|
|
{repayAmount && selectedAccount?.uiBalance < parseFloat(repayAmount) ? (
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification
|
|
|
|
desc={t('deposit-before', {
|
|
|
|
tokenSymbol: tokenSymbol,
|
|
|
|
})}
|
|
|
|
title={t('not-enough-balance')}
|
2021-11-09 16:34:03 -08:00
|
|
|
type="warning"
|
2021-10-26 07:25:36 -07:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<AccountSelect
|
|
|
|
accounts={walletTokens}
|
|
|
|
selectedAccount={selectedAccount}
|
|
|
|
onSelectAccount={handleAccountSelect}
|
|
|
|
/>
|
|
|
|
<div className="flex justify-between pb-2 pt-4">
|
|
|
|
<div className={`text-th-fgd-1`}>{t('amount')}</div>
|
|
|
|
</div>
|
|
|
|
<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={selectedAccount?.config.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}
|
|
|
|
<div className="pt-1">
|
|
|
|
<ButtonGroup
|
|
|
|
activeValue={depositPercentage}
|
|
|
|
onChange={(v) => onChangeAmountButtons(v)}
|
|
|
|
unit="%"
|
|
|
|
values={['25', '50', '75', '100']}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-12-08 02:01:55 -08:00
|
|
|
{selectedAccount?.config.symbol === 'SOL' &&
|
|
|
|
parseFloat(inputAmount) > selectedAccount?.uiBalance - 0.01 ? (
|
2021-12-17 04:23:58 -08:00
|
|
|
<div className="text-xs text-center text-th-red -mb-4 mt-1">
|
2021-11-24 00:17:17 -08:00
|
|
|
{t('you-must-leave-enough-sol')}
|
2021-11-18 08:49:48 -08:00
|
|
|
</div>
|
|
|
|
) : null}
|
2021-11-09 16:34:03 -08:00
|
|
|
{repayAmount ? (
|
|
|
|
<div className="pt-3">
|
|
|
|
<InlineNotification desc={repayMessage} type="info" />
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className={`pt-6 flex justify-center`}>
|
|
|
|
<Button
|
|
|
|
onClick={handleDeposit}
|
|
|
|
className="w-full"
|
2021-11-18 08:49:48 -08:00
|
|
|
disabled={submitting || inputDisabled}
|
2021-10-26 07:25:36 -07:00
|
|
|
>
|
|
|
|
<div className={`flex items-center justify-center`}>
|
|
|
|
{submitting && <Loading className="-ml-1 mr-3" />}
|
|
|
|
{t('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
|
|
|
</div>
|
2021-10-26 07:25:36 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
2021-11-09 16:34:03 -08:00
|
|
|
{!repayAmount ? (
|
2021-10-26 07:25:36 -07:00
|
|
|
<div className="pt-3">
|
|
|
|
<InlineNotification desc={t('interest-info')} type="info" />
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-04-09 17:01:00 -07:00
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default React.memo(DepositModal)
|