2022-12-17 04:37:00 -08:00
|
|
|
import { Bank, HealthType } from '@blockworks-foundation/mango-v4'
|
|
|
|
import {
|
2022-12-18 18:08:49 -08:00
|
|
|
ArrowLeftIcon,
|
2022-12-17 04:37:00 -08:00
|
|
|
ArrowUpTrayIcon,
|
|
|
|
ChevronDownIcon,
|
|
|
|
ExclamationCircleIcon,
|
2022-12-20 15:30:00 -08:00
|
|
|
LinkIcon,
|
2022-12-17 04:37:00 -08:00
|
|
|
} from '@heroicons/react/20/solid'
|
|
|
|
import Decimal from 'decimal.js'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import Image from 'next/legacy/image'
|
|
|
|
import { useCallback, useMemo, useState } from 'react'
|
|
|
|
import NumberFormat, { NumberFormatValues } from 'react-number-format'
|
|
|
|
|
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-12-18 04:30:49 -08:00
|
|
|
import {
|
|
|
|
ACCOUNT_ACTION_MODAL_INNER_HEIGHT,
|
|
|
|
INPUT_TOKEN_DEFAULT,
|
|
|
|
} from './../utils/constants'
|
2022-12-17 04:37:00 -08:00
|
|
|
import { notify } from './../utils/notifications'
|
2023-01-06 01:14:54 -08:00
|
|
|
import {
|
|
|
|
floorToDecimal,
|
|
|
|
formatDecimal,
|
|
|
|
formatFixedDecimals,
|
|
|
|
} from './../utils/numbers'
|
2022-12-17 04:37:00 -08:00
|
|
|
import ActionTokenList from './account/ActionTokenList'
|
|
|
|
import ButtonGroup from './forms/ButtonGroup'
|
|
|
|
import Label from './forms/Label'
|
|
|
|
import Button from './shared/Button'
|
|
|
|
import InlineNotification from './shared/InlineNotification'
|
|
|
|
import Loading from './shared/Loading'
|
|
|
|
import { EnterBottomExitBottom, FadeInFadeOut } from './shared/Transitions'
|
|
|
|
import { withValueLimit } from './swap/SwapForm'
|
|
|
|
import { getMaxWithdrawForBank } from './swap/useTokenMax'
|
|
|
|
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
|
|
|
import HealthImpactTokenChange from '@components/HealthImpactTokenChange'
|
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
|
|
|
import useJupiterMints from 'hooks/useJupiterMints'
|
|
|
|
import useMangoGroup from 'hooks/useMangoGroup'
|
|
|
|
import TokenVaultWarnings from '@components/shared/TokenVaultWarnings'
|
2022-12-20 15:30:00 -08:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
2023-01-03 03:06:37 -08:00
|
|
|
import { useEnhancedWallet } from './wallet/EnhancedWalletProvider'
|
2023-01-08 19:38:09 -08:00
|
|
|
import AmountWithValue from './shared/AmountWithValue'
|
2022-12-17 04:37:00 -08:00
|
|
|
|
|
|
|
interface WithdrawFormProps {
|
|
|
|
onSuccess: () => void
|
|
|
|
token?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function WithdrawForm({ onSuccess, token }: WithdrawFormProps) {
|
|
|
|
const { t } = useTranslation(['common', 'trade'])
|
|
|
|
const { group } = useMangoGroup()
|
|
|
|
const [inputAmount, setInputAmount] = useState('')
|
|
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const [selectedToken, setSelectedToken] = useState(
|
|
|
|
token || INPUT_TOKEN_DEFAULT
|
|
|
|
)
|
|
|
|
const [showTokenList, setShowTokenList] = useState(false)
|
|
|
|
const [sizePercentage, setSizePercentage] = useState('')
|
|
|
|
const { mangoTokens } = useJupiterMints()
|
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-12-20 15:30:00 -08:00
|
|
|
const { connected } = useWallet()
|
2023-01-03 03:06:37 -08:00
|
|
|
const { handleConnect } = useEnhancedWallet()
|
2022-12-17 04:37:00 -08:00
|
|
|
|
|
|
|
const bank = useMemo(() => {
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
return group?.banksMapByName.get(selectedToken)?.[0]
|
|
|
|
}, [selectedToken])
|
|
|
|
|
|
|
|
const logoUri = useMemo(() => {
|
|
|
|
let logoURI
|
|
|
|
if (mangoTokens?.length) {
|
|
|
|
logoURI = mangoTokens.find(
|
|
|
|
(t) => t.address === bank?.mint.toString()
|
|
|
|
)?.logoURI
|
|
|
|
}
|
|
|
|
return logoURI
|
|
|
|
}, [bank?.mint, mangoTokens])
|
|
|
|
|
|
|
|
const tokenMax = useMemo(() => {
|
|
|
|
if (!bank || !mangoAccount || !group) return new Decimal(0)
|
|
|
|
const amount = getMaxWithdrawForBank(group, bank, mangoAccount)
|
|
|
|
|
|
|
|
return amount && amount.gt(0)
|
|
|
|
? floorToDecimal(amount, bank.mintDecimals)
|
|
|
|
: new Decimal(0)
|
|
|
|
}, [mangoAccount, bank, group])
|
|
|
|
|
|
|
|
const handleSizePercentage = useCallback(
|
|
|
|
(percentage: string) => {
|
2023-01-05 20:10:25 -08:00
|
|
|
if (!bank) return
|
2022-12-17 04:37:00 -08:00
|
|
|
setSizePercentage(percentage)
|
|
|
|
const amount = tokenMax.mul(Number(percentage) / 100)
|
2023-01-05 20:10:25 -08:00
|
|
|
setInputAmount(floorToDecimal(amount, bank.mintDecimals).toFixed())
|
2022-12-17 04:37:00 -08:00
|
|
|
},
|
2023-01-05 20:10:25 -08:00
|
|
|
[bank, tokenMax]
|
2022-12-17 04:37:00 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const handleWithdraw = useCallback(async () => {
|
|
|
|
const client = mangoStore.getState().client
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
const mangoAccount = mangoStore.getState().mangoAccount.current
|
|
|
|
const actions = mangoStore.getState().actions
|
|
|
|
if (!mangoAccount || !group || !bank) return
|
|
|
|
setSubmitting(true)
|
|
|
|
try {
|
|
|
|
const tx = await client.tokenWithdraw(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
bank.mint,
|
|
|
|
parseFloat(inputAmount),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
notify({
|
|
|
|
title: 'Transaction confirmed',
|
|
|
|
type: 'success',
|
|
|
|
txid: tx,
|
|
|
|
})
|
2022-12-17 05:01:33 -08:00
|
|
|
await actions.reloadMangoAccount()
|
|
|
|
setSubmitting(false)
|
|
|
|
onSuccess()
|
2022-12-17 04:37:00 -08:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e)
|
|
|
|
notify({
|
|
|
|
title: 'Transaction failed',
|
|
|
|
description: e.message,
|
|
|
|
txid: e?.txid,
|
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
setSubmitting(false)
|
|
|
|
}
|
|
|
|
}, [bank, inputAmount])
|
|
|
|
|
|
|
|
const handleSelectToken = useCallback((token: string) => {
|
|
|
|
setSelectedToken(token)
|
|
|
|
setShowTokenList(false)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const withdrawBanks = useMemo(() => {
|
|
|
|
if (mangoAccount) {
|
|
|
|
const banks = group?.banksMapByName
|
|
|
|
? Array.from(group?.banksMapByName, ([key, value]) => {
|
|
|
|
const bank: Bank = value[0]
|
|
|
|
const accountBalance = getMaxWithdrawForBank(
|
|
|
|
group,
|
|
|
|
bank,
|
|
|
|
mangoAccount
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
accountBalance: accountBalance
|
|
|
|
? floorToDecimal(accountBalance, bank.mintDecimals).toNumber()
|
|
|
|
: 0,
|
|
|
|
accountBalanceValue:
|
|
|
|
accountBalance && bank.uiPrice
|
|
|
|
? accountBalance.toNumber() * bank.uiPrice
|
|
|
|
: 0,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: []
|
|
|
|
return banks
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}, [mangoAccount, group])
|
|
|
|
|
|
|
|
const initHealth = useMemo(() => {
|
|
|
|
return group && mangoAccount
|
|
|
|
? mangoAccount.getHealthRatioUi(group, HealthType.init)
|
|
|
|
: 100
|
|
|
|
}, [mangoAccount])
|
|
|
|
|
|
|
|
const showInsufficientBalance = Number(inputAmount)
|
|
|
|
? tokenMax.lt(inputAmount)
|
|
|
|
: false
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<EnterBottomExitBottom
|
|
|
|
className="absolute bottom-0 left-0 z-20 h-full w-full overflow-auto rounded-lg bg-th-bkg-1 p-6"
|
|
|
|
show={showTokenList}
|
|
|
|
>
|
2022-12-18 18:08:49 -08:00
|
|
|
<button
|
|
|
|
onClick={() => setShowTokenList(false)}
|
|
|
|
className={`absolute left-4 top-4 z-40 w-6 text-th-fgd-4 focus:outline-none md:right-2 md:top-2 md:hover:text-th-active`}
|
|
|
|
>
|
|
|
|
<ArrowLeftIcon className={`h-6 w-6`} />
|
|
|
|
</button>
|
2022-12-17 04:37:00 -08:00
|
|
|
<h2 className="mb-4 text-center text-lg">
|
|
|
|
{t('select-withdraw-token')}
|
|
|
|
</h2>
|
2023-01-05 17:19:39 -08:00
|
|
|
<div className="flex items-center px-4 pb-2">
|
|
|
|
<div className="w-1/2 text-left">
|
2022-12-17 04:37:00 -08:00
|
|
|
<p className="text-xs">{t('token')}</p>
|
|
|
|
</div>
|
2023-01-05 17:19:39 -08:00
|
|
|
<div className="w-1/2 text-right">
|
2022-12-17 04:37:00 -08:00
|
|
|
<p className="text-xs">{t('available-balance')}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ActionTokenList
|
|
|
|
banks={withdrawBanks}
|
|
|
|
onSelect={handleSelectToken}
|
|
|
|
sortByKey="accountBalanceValue"
|
|
|
|
valueKey="accountBalance"
|
|
|
|
/>
|
|
|
|
</EnterBottomExitBottom>
|
2022-12-27 04:07:02 -08:00
|
|
|
<FadeInFadeOut show={!showTokenList}>
|
|
|
|
<div
|
|
|
|
className="flex flex-col justify-between"
|
|
|
|
style={{ height: ACCOUNT_ACTION_MODAL_INNER_HEIGHT }}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
{initHealth <= 0 ? (
|
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="error"
|
|
|
|
desc="You have no available collateral to withdraw."
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-01-09 15:47:18 -08:00
|
|
|
{bank ? <TokenVaultWarnings bank={bank} type="withdraw" /> : null}
|
2022-12-27 04:07:02 -08:00
|
|
|
<div className="grid grid-cols-2">
|
|
|
|
<div className="col-span-2 flex justify-between">
|
|
|
|
<Label text={`${t('withdraw')} ${t('token')}`} />
|
2023-01-05 20:10:25 -08:00
|
|
|
{bank ? (
|
|
|
|
<MaxAmountButton
|
|
|
|
className="mb-2"
|
|
|
|
label={t('max')}
|
|
|
|
onClick={() => handleSizePercentage('100')}
|
|
|
|
value={floorToDecimal(
|
|
|
|
Number(tokenMax),
|
|
|
|
bank.mintDecimals
|
|
|
|
).toFixed()}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
|
|
|
<div className="col-span-1 rounded-lg rounded-r-none border border-r-0 border-th-input-border bg-th-input-bkg">
|
|
|
|
<button
|
|
|
|
onClick={() => setShowTokenList(true)}
|
|
|
|
className="default-transition flex h-full w-full items-center rounded-lg rounded-r-none py-2 px-3 text-th-fgd-2 hover:cursor-pointer hover:bg-th-bkg-2 hover:text-th-fgd-1"
|
|
|
|
>
|
|
|
|
<div className="mr-2.5 flex min-w-[24px] items-center">
|
|
|
|
<Image
|
|
|
|
alt=""
|
|
|
|
width="24"
|
|
|
|
height="24"
|
|
|
|
src={
|
|
|
|
logoUri || `/icons/${selectedToken.toLowerCase()}.svg`
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex w-full items-center justify-between">
|
|
|
|
<div className="text-xl font-bold">{selectedToken}</div>
|
|
|
|
<ChevronDownIcon className="h-6 w-6" />
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-1">
|
|
|
|
<NumberFormat
|
|
|
|
name="amountIn"
|
|
|
|
id="amountIn"
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
|
|
|
allowNegative={false}
|
|
|
|
isNumericString={true}
|
|
|
|
decimalScale={bank?.mintDecimals || 6}
|
2023-01-05 18:03:58 -08:00
|
|
|
className="w-full rounded-lg rounded-l-none border border-th-input-border bg-th-input-bkg p-3 text-right font-mono text-xl text-th-fgd-1 focus:border-th-input-border-hover focus:outline-none md:hover:border-th-input-border-hover"
|
2022-12-27 04:07:02 -08:00
|
|
|
placeholder="0.00"
|
|
|
|
value={inputAmount}
|
|
|
|
onValueChange={(e: NumberFormatValues) =>
|
|
|
|
setInputAmount(
|
|
|
|
!Number.isNaN(Number(e.value)) ? e.value : ''
|
|
|
|
)
|
|
|
|
}
|
|
|
|
isAllowed={withValueLimit}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-span-2 mt-2">
|
|
|
|
<ButtonGroup
|
|
|
|
activeValue={sizePercentage}
|
|
|
|
className="font-mono"
|
|
|
|
onChange={(p) => handleSizePercentage(p)}
|
|
|
|
values={['10', '25', '50', '75', '100']}
|
|
|
|
unit="%"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-17 04:37:00 -08:00
|
|
|
</div>
|
2023-01-06 01:14:54 -08:00
|
|
|
{bank ? (
|
2023-01-09 19:57:56 -08:00
|
|
|
<div className="my-6 space-y-1.5 border-y border-th-bkg-3 px-2 py-4">
|
2023-01-06 01:14:54 -08:00
|
|
|
<HealthImpactTokenChange
|
|
|
|
mintPk={bank.mint}
|
|
|
|
uiAmount={Number(inputAmount)}
|
|
|
|
/>
|
|
|
|
<div className="flex justify-between">
|
|
|
|
<p>{t('withdraw-amount')}</p>
|
2023-01-08 19:38:09 -08:00
|
|
|
{inputAmount ? (
|
|
|
|
<AmountWithValue
|
|
|
|
amount={formatDecimal(
|
|
|
|
Number(inputAmount),
|
|
|
|
bank.mintDecimals
|
|
|
|
)}
|
|
|
|
value={formatFixedDecimals(
|
|
|
|
bank.uiPrice * Number(inputAmount),
|
|
|
|
true
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<AmountWithValue amount="0" value="$0.00" />
|
|
|
|
)}
|
2023-01-06 01:14:54 -08:00
|
|
|
</div>
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
2023-01-06 01:14:54 -08:00
|
|
|
) : null}
|
2022-12-17 04:37:00 -08:00
|
|
|
</div>
|
|
|
|
<Button
|
2023-01-03 03:06:37 -08:00
|
|
|
onClick={connected ? handleWithdraw : handleConnect}
|
2022-12-17 04:37:00 -08:00
|
|
|
className="flex w-full items-center justify-center"
|
|
|
|
size="large"
|
|
|
|
disabled={
|
2023-01-03 03:06:37 -08:00
|
|
|
connected &&
|
|
|
|
(!inputAmount || showInsufficientBalance || initHealth <= 0)
|
2022-12-17 04:37:00 -08:00
|
|
|
}
|
|
|
|
>
|
2022-12-20 15:30:00 -08:00
|
|
|
{!connected ? (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<LinkIcon className="mr-2 h-5 w-5" />
|
|
|
|
{t('connect')}
|
|
|
|
</div>
|
|
|
|
) : submitting ? (
|
2022-12-17 04:37:00 -08:00
|
|
|
<Loading className="mr-2 h-5 w-5" />
|
|
|
|
) : showInsufficientBalance ? (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ExclamationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0" />
|
|
|
|
{t('swap:insufficient-balance', {
|
|
|
|
symbol: selectedToken,
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ArrowUpTrayIcon className="mr-2 h-5 w-5" />
|
|
|
|
{t('withdraw')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</FadeInFadeOut>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WithdrawForm
|