mango-ui-v3/components/DepositModal.tsx

282 lines
8.8 KiB
TypeScript
Raw Normal View History

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'
import { ChevronLeftIcon } from '@heroicons/react/solid'
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'
import useMangoStore from '../stores/useMangoStore'
2021-04-09 17:01:00 -07:00
import Loading from './Loading'
import Button, { LinkButton } from './Button'
import Slider from './Slider'
import InlineNotification from './InlineNotification'
2021-06-21 06:54:56 -07:00
import { deposit } from '../utils/mango'
import { notify } from '../utils/notifications'
2021-08-31 10:47:03 -07:00
import { sleep } from '../utils'
2021-04-09 17:01:00 -07:00
interface DepositModalProps {
onClose: () => void
isOpen: boolean
settleDeficit?: string
tokenSymbol?: string
}
const DepositModal: FunctionComponent<DepositModalProps> = ({
isOpen,
onClose,
settleDeficit,
tokenSymbol = '',
}) => {
const [inputAmount, setInputAmount] = useState<string>(settleDeficit || '')
2021-04-09 17:01:00 -07:00
const [submitting, setSubmitting] = useState(false)
2021-08-16 10:00:43 -07:00
const [showConfirm, setShowConfirm] = useState(false)
const [invalidAmountMessage, setInvalidAmountMessage] = useState('')
const [sliderPercentage, setSliderPercentage] = useState(0)
const [maxButtonTransition, setMaxButtonTransition] = useState(false)
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])
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-04-09 17:01:00 -07:00
useEffect(() => {
if (tokenSymbol) {
2021-06-17 11:03:47 -07:00
const symbolAccount = walletTokens.find(
(a) => a.config.symbol === tokenSymbol
)
if (symbolAccount) {
setSelectedAccount(symbolAccount)
} else {
setSelectedAccount(null)
}
}
2021-06-17 11:03:47 -07:00
}, [tokenSymbol, walletTokens])
const handleAccountSelect = (account) => {
2021-07-23 10:07:31 -07:00
setInputAmount('')
setSliderPercentage(0)
setInvalidAmountMessage('')
setSelectedAccount(account)
}
2021-04-09 17:01:00 -07:00
const setMaxForSelectedAccount = () => {
setInputAmount(selectedAccount.uiBalance.toString())
setSliderPercentage(100)
setInvalidAmountMessage('')
setMaxButtonTransition(true)
2021-04-09 17:01:00 -07:00
}
const handleDeposit = () => {
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
2021-04-09 17:01:00 -07:00
setSubmitting(true)
deposit({
amount: parseFloat(inputAmount),
2021-06-18 08:37:42 -07:00
fromTokenAcc: selectedAccount.account,
mangoAccount,
})
.then((response) => {
notify({
title: 'Deposit successful',
2021-07-06 17:13:17 -07:00
type: 'success',
2021-08-17 13:47:57 -07:00
txid: response.toString(),
})
setSubmitting(false)
onClose()
2021-08-31 10:47:03 -07:00
sleep(500).then(() => {
mangoAccount
? actions.reloadMangoAccount()
: actions.fetchMangoAccounts()
2021-08-31 11:11:41 -07:00
actions.fetchWalletTokens()
2021-08-31 10:47:03 -07:00
})
})
2021-07-06 17:13:17 -07:00
.catch((err) => {
notify({
title: 'Deposit failed',
2021-07-06 17:13:17 -07:00
description: err.message,
type: 'error',
})
})
2021-04-09 17:01:00 -07:00
}
const validateAmountInput = (amount) => {
if (Number(amount) <= 0) {
setInvalidAmountMessage('Enter an amount to deposit')
}
2021-06-17 11:03:47 -07:00
if (selectedAccount && Number(amount) > selectedAccount.uiBalance) {
setInvalidAmountMessage(
'Insufficient balance. Reduce the amount to deposit'
)
}
}
const onChangeAmountInput = (amount) => {
setInputAmount(amount)
2021-06-17 11:03:47 -07:00
if (!selectedAccount) {
2021-06-17 15:38:17 -07:00
setInvalidAmountMessage(
'Please fund wallet with one of the supported assets.'
)
2021-06-17 11:03:47 -07:00
return
}
2021-06-17 15:38:17 -07:00
2021-06-17 11:03:47 -07:00
const max = selectedAccount.uiBalance
setSliderPercentage((amount / max) * 100)
setInvalidAmountMessage('')
}
const onChangeSlider = async (percentage) => {
2021-06-17 11:03:47 -07:00
setSliderPercentage(percentage)
if (!selectedAccount) {
2021-06-17 15:38:17 -07:00
setInvalidAmountMessage(
'Please fund wallet with one of the supported assets.'
)
2021-06-17 11:03:47 -07:00
return
}
const max = selectedAccount.uiBalance
const amount = (percentage / 100) * max
if (percentage === 100) {
setInputAmount(amount.toString())
} else {
setInputAmount(amount.toString())
}
setInvalidAmountMessage('')
validateAmountInput(amount)
}
// turn off slider transition for dragging slider handle interaction
useEffect(() => {
if (maxButtonTransition) {
setMaxButtonTransition(false)
}
}, [maxButtonTransition])
2021-04-09 17:01:00 -07:00
return (
2021-08-16 10:59:44 -07:00
<Modal isOpen={isOpen} onClose={onClose}>
2021-08-16 10:00:43 -07:00
{!showConfirm ? (
<>
<Modal.Header>
<ElementTitle noMarignBottom>Deposit Funds</ElementTitle>
</Modal.Header>
{tokenSymbol && !selectedAccount ? (
<InlineNotification
desc={`Add ${tokenSymbol} to your wallet and fund it with ${tokenSymbol} to deposit.`}
title={`No ${tokenSymbol} wallet address found`}
type="error"
/>
) : null}
{settleDeficit ? (
<InlineNotification
desc={`Deposit ${settleDeficit} ${tokenSymbol} before settling your borrow.`}
title="Not enough balance to settle"
type="error"
/>
) : null}
<AccountSelect
2021-06-17 11:03:47 -07:00
accounts={walletTokens}
selectedAccount={selectedAccount}
onSelectAccount={handleAccountSelect}
2021-04-18 03:34:37 -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
2021-04-09 17:01:00 -07:00
</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)}
2021-06-17 11:03:47 -07:00
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-3 pb-4">
<Slider
disabled={null}
value={sliderPercentage}
onChange={(v) => onChangeSlider(v)}
step={1}
maxButtonTransition={maxButtonTransition}
/>
</div>
<div className={`pt-8 flex justify-center`}>
<Button
2021-08-16 10:00:43 -07:00
onClick={() => setShowConfirm(true)}
className="w-full"
disabled={
2021-08-16 10:00:43 -07:00
!inputAmount ||
parseFloat(inputAmount) <= 0 ||
2021-06-17 15:38:17 -07:00
!selectedAccount ||
parseFloat(inputAmount) > selectedAccount.uiBalance
}
>
Next
</Button>
</div>
{!mangoAccount ? (
<div className="flex text-th-fgd-4 text-xxs mt-1">
<div className="mx-auto">
You need 0.035 SOL to create a mango account.
</div>
</div>
) : null}
</>
) : (
<>
<Modal.Header>
<ElementTitle noMarignBottom>Confirm Deposit</ElementTitle>
</Modal.Header>
<div className="bg-th-bkg-1 p-4 rounded-lg text-th-fgd-1 text-center">
<div className="text-th-fgd-3 pb-1">{`You're about to deposit`}</div>
<div className="flex items-center justify-center">
<div className="font-semibold relative text-xl">
{inputAmount}
<span className="absolute bottom-0.5 font-normal ml-1.5 text-xs text-th-fgd-4">
2021-06-17 11:42:39 -07:00
{selectedAccount?.config.symbol}
</span>
</div>
</div>
</div>
<div className={`mt-5 flex justify-center`}>
2021-08-30 13:40:32 -07:00
<Button
onClick={handleDeposit}
className="w-full"
disabled={submitting}
>
<div className={`flex items-center justify-center`}>
{submitting && <Loading className="-ml-1 mr-3" />}
2021-08-30 13:40:32 -07:00
Deposit
</div>
</Button>
</div>
<LinkButton
className="flex items-center mt-4 text-th-fgd-3"
2021-08-16 10:00:43 -07:00
onClick={() => setShowConfirm(false)}
>
<ChevronLeftIcon className="h-5 w-5 mr-1" />
Back
</LinkButton>
</>
)}
2021-04-09 17:01:00 -07:00
</Modal>
)
}
export default React.memo(DepositModal)