2023-08-21 07:19:28 -07:00
|
|
|
import { ArrowDownRightIcon } from '@heroicons/react/20/solid'
|
2022-11-16 04:14:53 -08:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
|
|
|
import Decimal from 'decimal.js'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
2023-12-06 15:26:30 -08:00
|
|
|
import NumberFormat from 'react-number-format'
|
2022-11-16 04:14:53 -08:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-12-18 04:30:49 -08:00
|
|
|
import { notify } from './../utils/notifications'
|
2023-01-24 18:54:06 -08:00
|
|
|
import { formatNumericValue } from './../utils/numbers'
|
2022-12-18 04:30:49 -08:00
|
|
|
import ActionTokenList from './account/ActionTokenList'
|
|
|
|
import ButtonGroup from './forms/ButtonGroup'
|
|
|
|
import Label from './forms/Label'
|
|
|
|
import Button from './shared/Button'
|
|
|
|
import Loading from './shared/Loading'
|
|
|
|
import { EnterBottomExitBottom, FadeInFadeOut } from './shared/Transitions'
|
2023-06-16 05:23:50 -07:00
|
|
|
import { withValueLimit } from './swap/MarketSwapForm'
|
2022-11-16 04:14:53 -08:00
|
|
|
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
|
|
|
import HealthImpactTokenChange from '@components/HealthImpactTokenChange'
|
2022-11-17 20:43:23 -08:00
|
|
|
import SolBalanceWarnings from '@components/shared/SolBalanceWarnings'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2023-01-02 11:49:50 -08:00
|
|
|
import {
|
2024-01-22 10:21:24 -08:00
|
|
|
BORROW_REPAY_MODAL_INNER_HEIGHT,
|
2023-01-02 11:49:50 -08:00
|
|
|
INPUT_TOKEN_DEFAULT,
|
|
|
|
} from 'utils/constants'
|
2023-01-03 03:06:37 -08:00
|
|
|
import ConnectEmptyState from './shared/ConnectEmptyState'
|
2023-01-28 17:13:36 -08:00
|
|
|
import BankAmountWithValue from './shared/BankAmountWithValue'
|
2023-01-29 20:13:38 -08:00
|
|
|
import useBanksWithBalances from 'hooks/useBanksWithBalances'
|
2023-02-27 23:20:11 -08:00
|
|
|
import { isMangoError } from 'types'
|
2023-03-24 05:22:29 -07:00
|
|
|
import TokenListButton from './shared/TokenListButton'
|
2023-04-05 20:24:29 -07:00
|
|
|
import { ACCOUNT_ACTIONS_NUMBER_FORMAT_CLASSES, BackButton } from './BorrowForm'
|
2023-07-04 21:40:47 -07:00
|
|
|
import TokenLogo from './shared/TokenLogo'
|
2023-10-20 03:59:51 -07:00
|
|
|
import InlineNotification from './shared/InlineNotification'
|
2023-12-06 15:26:30 -08:00
|
|
|
import { handleInputChange } from 'utils/account'
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2022-12-18 04:30:49 -08:00
|
|
|
interface RepayFormProps {
|
|
|
|
onSuccess: () => void
|
2022-11-16 04:14:53 -08:00
|
|
|
token?: string
|
|
|
|
}
|
|
|
|
|
2022-12-18 04:30:49 -08:00
|
|
|
function RepayForm({ onSuccess, token }: RepayFormProps) {
|
2022-11-16 04:14:53 -08:00
|
|
|
const { t } = useTranslation('common')
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2023-10-20 03:59:51 -07:00
|
|
|
const walletTokens = mangoStore((s) => s.wallet.tokens)
|
2022-11-16 04:14:53 -08:00
|
|
|
const [inputAmount, setInputAmount] = useState('')
|
|
|
|
const [submitting, setSubmitting] = useState(false)
|
2023-01-02 11:49:50 -08:00
|
|
|
const [selectedToken, setSelectedToken] = useState(
|
2023-07-21 11:47:53 -07:00
|
|
|
token || INPUT_TOKEN_DEFAULT,
|
2023-01-02 11:49:50 -08:00
|
|
|
)
|
2022-11-16 04:14:53 -08:00
|
|
|
const [showTokenList, setShowTokenList] = useState(false)
|
|
|
|
const [sizePercentage, setSizePercentage] = useState('')
|
2023-01-29 20:13:38 -08:00
|
|
|
const banks = useBanksWithBalances('borrowedAmount')
|
2022-11-18 11:11:06 -08:00
|
|
|
// const { maxSolDeposit } = useSolBalance()
|
2022-11-16 04:14:53 -08:00
|
|
|
|
|
|
|
const bank = useMemo(() => {
|
|
|
|
const group = mangoStore.getState().group
|
2023-01-02 11:49:50 -08:00
|
|
|
return group?.banksMapByName.get(selectedToken)?.[0]
|
2022-11-16 04:14:53 -08:00
|
|
|
}, [selectedToken])
|
|
|
|
|
2023-01-14 13:50:45 -08:00
|
|
|
const { connected, publicKey } = useWallet()
|
2022-11-16 04:14:53 -08:00
|
|
|
|
|
|
|
const borrowAmount = useMemo(() => {
|
2023-01-24 18:54:06 -08:00
|
|
|
if (!mangoAccount || !bank) return new Decimal(0)
|
|
|
|
const amount = new Decimal(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.getTokenBorrowsUi(bank),
|
2023-01-24 18:54:06 -08:00
|
|
|
).toDecimalPlaces(bank.mintDecimals, Decimal.ROUND_UP)
|
|
|
|
return amount
|
2022-11-16 04:14:53 -08:00
|
|
|
}, [bank, mangoAccount])
|
|
|
|
|
2024-02-22 19:20:16 -08:00
|
|
|
const walletBalance = useMemo(() => {
|
|
|
|
if (!bank || !walletTokens?.length) return 0
|
2023-10-20 03:59:51 -07:00
|
|
|
const hasBorrowToken = walletTokens.find(
|
|
|
|
(token) => token.mint.toString() === bank.mint.toString(),
|
|
|
|
)
|
2024-02-22 19:20:16 -08:00
|
|
|
|
|
|
|
return hasBorrowToken?.uiAmount || 0
|
|
|
|
}, [bank, walletTokens])
|
|
|
|
|
|
|
|
const max = useMemo(() => {
|
|
|
|
if (!walletBalance) return new Decimal(0)
|
|
|
|
return walletBalance >= borrowAmount.toNumber()
|
|
|
|
? borrowAmount
|
|
|
|
: new Decimal(walletBalance)
|
|
|
|
}, [borrowAmount, walletBalance])
|
2023-10-20 03:59:51 -07:00
|
|
|
|
2023-10-12 04:51:02 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (token && !borrowAmount.eq(0)) {
|
|
|
|
setInputAmount(borrowAmount.toFixed())
|
|
|
|
}
|
|
|
|
}, [token, borrowAmount])
|
|
|
|
|
2022-11-16 04:14:53 -08:00
|
|
|
const setMax = useCallback(() => {
|
2023-01-05 20:10:25 -08:00
|
|
|
if (!bank) return
|
2024-02-22 19:20:16 -08:00
|
|
|
const amount = max.toDecimalPlaces(bank.mintDecimals, Decimal.ROUND_UP)
|
2023-02-10 01:41:06 -08:00
|
|
|
setInputAmount(amount.toFixed())
|
2022-11-16 04:14:53 -08:00
|
|
|
setSizePercentage('100')
|
2024-02-22 19:20:16 -08:00
|
|
|
}, [bank, max])
|
2022-11-16 04:14:53 -08:00
|
|
|
|
|
|
|
const handleSizePercentage = useCallback(
|
|
|
|
(percentage: string) => {
|
2023-01-05 20:10:25 -08:00
|
|
|
if (!bank) return
|
2022-11-16 04:14:53 -08:00
|
|
|
setSizePercentage(percentage)
|
2024-02-22 19:20:16 -08:00
|
|
|
const amount = max
|
2022-11-20 18:22:45 -08:00
|
|
|
.mul(percentage)
|
|
|
|
.div(100)
|
2023-01-24 18:54:06 -08:00
|
|
|
.toDecimalPlaces(bank.mintDecimals, Decimal.ROUND_UP)
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2023-02-10 01:41:06 -08:00
|
|
|
setInputAmount(amount.toFixed())
|
2022-11-16 04:14:53 -08:00
|
|
|
},
|
2024-02-22 19:20:16 -08:00
|
|
|
[bank, max],
|
2022-11-16 04:14:53 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const handleSelectToken = (token: string) => {
|
|
|
|
setSelectedToken(token)
|
|
|
|
setShowTokenList(false)
|
|
|
|
}
|
|
|
|
|
2022-12-08 10:19:15 -08:00
|
|
|
const handleDeposit = useCallback(
|
|
|
|
async (amount: string) => {
|
2023-01-13 16:58:07 -08:00
|
|
|
const mangoAccount = mangoStore.getState().mangoAccount.current
|
2022-12-08 10:19:15 -08:00
|
|
|
const client = mangoStore.getState().client
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
const actions = mangoStore.getState().actions
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2023-01-14 13:50:45 -08:00
|
|
|
if (!mangoAccount || !group || !bank || !publicKey) return
|
2023-01-13 17:05:02 -08:00
|
|
|
|
2023-02-10 02:21:26 -08:00
|
|
|
// we don't want to leave negative dust in the account if someone wants to repay the full amount
|
2023-01-13 16:58:07 -08:00
|
|
|
const actualAmount =
|
|
|
|
sizePercentage === '100'
|
2023-10-31 14:49:08 -07:00
|
|
|
? borrowAmount.toNumber() * 1.01
|
2023-01-13 16:58:07 -08:00
|
|
|
: parseFloat(amount)
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2022-12-18 04:30:49 -08:00
|
|
|
setSubmitting(true)
|
2022-12-08 10:19:15 -08:00
|
|
|
try {
|
2023-08-12 11:40:09 -07:00
|
|
|
const { signature: tx, slot } = await client.tokenDeposit(
|
2022-12-08 10:19:15 -08:00
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
bank.mint,
|
2023-01-13 16:58:07 -08:00
|
|
|
actualAmount,
|
2023-07-21 11:47:53 -07:00
|
|
|
true,
|
2022-12-08 10:19:15 -08:00
|
|
|
)
|
|
|
|
notify({
|
|
|
|
title: 'Transaction confirmed',
|
|
|
|
type: 'success',
|
|
|
|
txid: tx,
|
|
|
|
})
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2023-08-12 11:40:09 -07:00
|
|
|
await actions.reloadMangoAccount(slot)
|
2023-01-14 13:50:45 -08:00
|
|
|
actions.fetchWalletTokens(publicKey)
|
2022-12-08 10:19:15 -08:00
|
|
|
setSubmitting(false)
|
2022-12-18 04:30:49 -08:00
|
|
|
onSuccess()
|
2023-02-27 23:20:11 -08:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Error repaying:', e)
|
|
|
|
setSubmitting(false)
|
|
|
|
if (!isMangoError(e)) return
|
2022-12-08 10:19:15 -08:00
|
|
|
notify({
|
|
|
|
title: 'Transaction failed',
|
|
|
|
description: e.message,
|
|
|
|
txid: e?.txid,
|
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2024-02-22 19:20:16 -08:00
|
|
|
[bank, borrowAmount, publicKey, onSuccess, sizePercentage],
|
2022-12-08 10:19:15 -08:00
|
|
|
)
|
2022-11-16 04:14:53 -08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-20 15:21:57 -08:00
|
|
|
if (!selectedToken && !token && banks.length) {
|
2023-01-29 20:13:38 -08:00
|
|
|
setSelectedToken(banks[0].bank.name)
|
2022-11-16 04:14:53 -08:00
|
|
|
}
|
2023-01-20 15:21:57 -08:00
|
|
|
}, [token, banks, selectedToken])
|
2022-11-16 04:14:53 -08:00
|
|
|
|
2024-02-22 19:20:16 -08:00
|
|
|
const outstandingAmount = inputAmount
|
|
|
|
? borrowAmount.toNumber() - parseFloat(inputAmount)
|
|
|
|
: borrowAmount.toNumber()
|
2023-02-10 02:21:26 -08:00
|
|
|
const isDeposit = parseFloat(inputAmount) > borrowAmount.toNumber()
|
|
|
|
|
2022-12-18 04:30:49 -08:00
|
|
|
return banks.length ? (
|
|
|
|
<>
|
2022-11-16 04:14:53 -08:00
|
|
|
<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}
|
|
|
|
>
|
2023-03-26 20:11:10 -07:00
|
|
|
<BackButton onClick={() => setShowTokenList(false)} />
|
2022-12-18 04:30:49 -08:00
|
|
|
<h2 className="mb-4 text-center text-lg">{t('select-repay-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-11-16 04:14:53 -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-11-16 04:14:53 -08:00
|
|
|
<p className="whitespace-nowrap text-xs">{t('amount-owed')}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ActionTokenList
|
|
|
|
banks={banks}
|
|
|
|
onSelect={handleSelectToken}
|
2023-01-29 20:13:38 -08:00
|
|
|
valueKey="borrowedAmount"
|
2022-11-16 04:14:53 -08:00
|
|
|
/>
|
|
|
|
</EnterBottomExitBottom>
|
2022-12-27 04:07:02 -08:00
|
|
|
<FadeInFadeOut show={!showTokenList}>
|
|
|
|
<div
|
|
|
|
className="flex flex-col justify-between"
|
2024-01-22 10:21:24 -08:00
|
|
|
style={{ height: BORROW_REPAY_MODAL_INNER_HEIGHT }}
|
2022-12-27 04:07:02 -08:00
|
|
|
>
|
|
|
|
<div>
|
2023-01-06 01:14:54 -08:00
|
|
|
<SolBalanceWarnings
|
|
|
|
amount={inputAmount}
|
|
|
|
className="mb-4"
|
|
|
|
setAmount={setInputAmount}
|
|
|
|
selectedToken={selectedToken}
|
|
|
|
/>
|
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('repay')} ${t('token')}`} />
|
2023-01-23 19:04:05 -08:00
|
|
|
{bank ? (
|
|
|
|
<MaxAmountButton
|
|
|
|
className="mb-2"
|
|
|
|
decimals={bank.mintDecimals}
|
2024-02-22 19:20:16 -08:00
|
|
|
label={t('max')}
|
2023-01-23 19:04:05 -08:00
|
|
|
onClick={setMax}
|
2024-02-22 19:20:16 -08:00
|
|
|
value={max}
|
2023-01-23 19:04:05 -08:00
|
|
|
/>
|
|
|
|
) : null}
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
2023-03-24 05:22:29 -07:00
|
|
|
<div className="col-span-1">
|
|
|
|
<TokenListButton
|
|
|
|
token={selectedToken}
|
2023-07-04 21:40:47 -07:00
|
|
|
logo={<TokenLogo bank={bank} />}
|
2023-03-24 05:22:29 -07:00
|
|
|
setShowList={setShowTokenList}
|
|
|
|
/>
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
|
|
|
<div className="col-span-1">
|
|
|
|
<NumberFormat
|
|
|
|
name="amountIn"
|
|
|
|
id="amountIn"
|
|
|
|
inputMode="decimal"
|
|
|
|
thousandSeparator=","
|
|
|
|
allowNegative={false}
|
|
|
|
isNumericString={true}
|
|
|
|
decimalScale={bank?.mintDecimals || 6}
|
2023-04-05 20:24:29 -07:00
|
|
|
className={ACCOUNT_ACTIONS_NUMBER_FORMAT_CLASSES}
|
2022-12-27 04:07:02 -08:00
|
|
|
placeholder="0.00"
|
|
|
|
value={inputAmount}
|
2023-12-06 15:26:30 -08:00
|
|
|
onValueChange={(values, source) =>
|
|
|
|
handleInputChange(
|
|
|
|
values,
|
|
|
|
source,
|
|
|
|
setInputAmount,
|
|
|
|
setSizePercentage,
|
2022-11-19 11:20:36 -08:00
|
|
|
)
|
2023-12-06 15:26:30 -08:00
|
|
|
}
|
2022-12-27 04:07:02 -08:00
|
|
|
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-11-16 04:14:53 -08:00
|
|
|
</div>
|
2023-01-06 01:14:54 -08:00
|
|
|
{bank ? (
|
|
|
|
<div className="my-6 space-y-1.5 border-y border-th-bkg-3 px-2 py-4 text-sm ">
|
2022-12-27 04:07:02 -08:00
|
|
|
<HealthImpactTokenChange
|
|
|
|
mintPk={bank.mint}
|
|
|
|
uiAmount={Number(inputAmount)}
|
|
|
|
isDeposit
|
|
|
|
/>
|
2023-01-06 01:14:54 -08:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<p>{t('repayment-amount')}</p>
|
2023-01-28 17:13:36 -08:00
|
|
|
<BankAmountWithValue amount={inputAmount} bank={bank} />
|
2023-01-06 01:14:54 -08:00
|
|
|
</div>
|
2023-02-10 02:21:26 -08:00
|
|
|
{isDeposit ? (
|
|
|
|
<div className="flex justify-between">
|
|
|
|
<p>{t('deposit-amount')}</p>
|
|
|
|
<BankAmountWithValue
|
|
|
|
amount={parseFloat(inputAmount) - borrowAmount.toNumber()}
|
|
|
|
bank={bank}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-01-06 01:14:54 -08:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<div className="flex items-center">
|
2024-02-22 19:20:16 -08:00
|
|
|
<p>{t('outstanding-borrow')}</p>
|
2023-01-06 01:14:54 -08:00
|
|
|
</div>
|
|
|
|
<p className="font-mono text-th-fgd-2">
|
2023-02-10 02:21:26 -08:00
|
|
|
{outstandingAmount > 0
|
|
|
|
? formatNumericValue(outstandingAmount, bank.mintDecimals)
|
|
|
|
: 0}{' '}
|
2023-01-06 01:14:54 -08:00
|
|
|
<span className="font-body text-th-fgd-4">
|
|
|
|
{selectedToken}
|
|
|
|
</span>
|
|
|
|
</p>
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
2022-11-16 04:14:53 -08:00
|
|
|
</div>
|
2023-01-06 01:14:54 -08:00
|
|
|
) : null}
|
2022-11-16 04:14:53 -08:00
|
|
|
</div>
|
2023-10-20 03:59:51 -07:00
|
|
|
<div>
|
2024-02-22 19:20:16 -08:00
|
|
|
{(borrowAmount.toNumber() && max.eq(0)) ||
|
|
|
|
(inputAmount && new Decimal(inputAmount).gt(max)) ? (
|
2023-10-20 03:59:51 -07:00
|
|
|
<div className="pb-3">
|
|
|
|
<InlineNotification
|
|
|
|
desc={t('error-repay-insufficient-funds', {
|
|
|
|
token: bank?.name,
|
|
|
|
})}
|
|
|
|
type="error"
|
|
|
|
/>
|
2022-12-27 04:07:02 -08:00
|
|
|
</div>
|
2023-10-20 03:59:51 -07:00
|
|
|
) : null}
|
|
|
|
<Button
|
|
|
|
onClick={() => handleDeposit(inputAmount)}
|
|
|
|
className="flex w-full items-center justify-center"
|
|
|
|
disabled={!inputAmount}
|
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
{submitting ? (
|
|
|
|
<Loading className="mr-2 h-5 w-5" />
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ArrowDownRightIcon className="mr-2 h-5 w-5" />
|
|
|
|
{isDeposit ? t('repay-deposit') : t('repay')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-11-16 04:14:53 -08:00
|
|
|
</div>
|
|
|
|
</FadeInFadeOut>
|
2022-12-18 04:30:49 -08:00
|
|
|
</>
|
2022-12-20 15:30:00 -08:00
|
|
|
) : !connected ? (
|
|
|
|
<div className="flex h-[356px] flex-col items-center justify-center">
|
2023-01-03 03:06:37 -08:00
|
|
|
<ConnectEmptyState text="Connect to repay your borrows" />
|
2022-12-20 15:30:00 -08:00
|
|
|
</div>
|
2022-12-18 04:30:49 -08:00
|
|
|
) : (
|
|
|
|
<div className="flex h-[356px] flex-col items-center justify-center">
|
|
|
|
<span className="text-2xl">😎</span>
|
|
|
|
<p>No borrows to repay...</p>
|
|
|
|
</div>
|
2022-11-16 04:14:53 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-18 04:30:49 -08:00
|
|
|
export default RepayForm
|