add alpha limit to repay form
This commit is contained in:
parent
c88944aa80
commit
fc161c1906
|
@ -1,4 +1,4 @@
|
||||||
import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
|
import { Bank, toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
|
||||||
import {
|
import {
|
||||||
ArrowDownTrayIcon,
|
ArrowDownTrayIcon,
|
||||||
ArrowLeftIcon,
|
ArrowLeftIcon,
|
||||||
|
@ -63,6 +63,27 @@ export const walletBalanceForToken = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useAlphaMax = (inputAmount: string, bank: Bank | undefined) => {
|
||||||
|
const exceedsAlphaMax = useMemo(() => {
|
||||||
|
const mangoAccount = mangoStore.getState().mangoAccount.current
|
||||||
|
const group = mangoStore.getState().group
|
||||||
|
if (!group || !mangoAccount) return
|
||||||
|
if (
|
||||||
|
mangoAccount.owner.toString() ===
|
||||||
|
'8SSLjXBEVk9nesbhi9UMCA32uijbVBUqWoKPPQPTekzt'
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
const accountValue = toUiDecimalsForQuote(
|
||||||
|
mangoAccount.getEquity(group).toNumber()
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
parseFloat(inputAmount) * (bank?.uiPrice || 1) + accountValue >
|
||||||
|
ALPHA_DEPOSIT_LIMIT || accountValue > ALPHA_DEPOSIT_LIMIT
|
||||||
|
)
|
||||||
|
}, [inputAmount, bank])
|
||||||
|
return exceedsAlphaMax
|
||||||
|
}
|
||||||
|
|
||||||
function DepositForm({ onSuccess, token }: DepositFormProps) {
|
function DepositForm({ onSuccess, token }: DepositFormProps) {
|
||||||
const { t } = useTranslation('common')
|
const { t } = useTranslation('common')
|
||||||
const { group } = useMangoGroup()
|
const { group } = useMangoGroup()
|
||||||
|
@ -79,6 +100,7 @@ function DepositForm({ onSuccess, token }: DepositFormProps) {
|
||||||
const group = mangoStore.getState().group
|
const group = mangoStore.getState().group
|
||||||
return group?.banksMapByName.get(selectedToken)?.[0]
|
return group?.banksMapByName.get(selectedToken)?.[0]
|
||||||
}, [selectedToken])
|
}, [selectedToken])
|
||||||
|
const exceedsAlphaMax = useAlphaMax(inputAmount, bank)
|
||||||
|
|
||||||
const logoUri = useMemo(() => {
|
const logoUri = useMemo(() => {
|
||||||
let logoURI
|
let logoURI
|
||||||
|
@ -176,24 +198,6 @@ function DepositForm({ onSuccess, token }: DepositFormProps) {
|
||||||
return banks
|
return banks
|
||||||
}, [group?.banksMapByName, walletTokens])
|
}, [group?.banksMapByName, walletTokens])
|
||||||
|
|
||||||
const exceedsAlphaMax = useMemo(() => {
|
|
||||||
const mangoAccount = mangoStore.getState().mangoAccount.current
|
|
||||||
const group = mangoStore.getState().group
|
|
||||||
if (!group || !mangoAccount) return
|
|
||||||
if (
|
|
||||||
mangoAccount.owner.toString() ===
|
|
||||||
'8SSLjXBEVk9nesbhi9UMCA32uijbVBUqWoKPPQPTekzt'
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
const accountValue = toUiDecimalsForQuote(
|
|
||||||
mangoAccount.getEquity(group).toNumber()
|
|
||||||
)
|
|
||||||
return (
|
|
||||||
parseFloat(inputAmount) * (bank?.uiPrice || 1) + accountValue >
|
|
||||||
ALPHA_DEPOSIT_LIMIT || accountValue > ALPHA_DEPOSIT_LIMIT
|
|
||||||
)
|
|
||||||
}, [inputAmount, bank])
|
|
||||||
|
|
||||||
const showInsufficientBalance = tokenMax.maxAmount < Number(inputAmount)
|
const showInsufficientBalance = tokenMax.maxAmount < Number(inputAmount)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -25,12 +25,15 @@ import { EnterBottomExitBottom, FadeInFadeOut } from './shared/Transitions'
|
||||||
import { withValueLimit } from './swap/SwapForm'
|
import { withValueLimit } from './swap/SwapForm'
|
||||||
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
||||||
import HealthImpactTokenChange from '@components/HealthImpactTokenChange'
|
import HealthImpactTokenChange from '@components/HealthImpactTokenChange'
|
||||||
import { walletBalanceForToken } from './DepositForm'
|
import { useAlphaMax, walletBalanceForToken } from './DepositForm'
|
||||||
import SolBalanceWarnings from '@components/shared/SolBalanceWarnings'
|
import SolBalanceWarnings from '@components/shared/SolBalanceWarnings'
|
||||||
import useMangoAccount from 'hooks/useMangoAccount'
|
import useMangoAccount from 'hooks/useMangoAccount'
|
||||||
import useJupiterMints from 'hooks/useJupiterMints'
|
import useJupiterMints from 'hooks/useJupiterMints'
|
||||||
import useMangoGroup from 'hooks/useMangoGroup'
|
import useMangoGroup from 'hooks/useMangoGroup'
|
||||||
import { ACCOUNT_ACTION_MODAL_INNER_HEIGHT } from 'utils/constants'
|
import {
|
||||||
|
ACCOUNT_ACTION_MODAL_INNER_HEIGHT,
|
||||||
|
INPUT_TOKEN_DEFAULT,
|
||||||
|
} from 'utils/constants'
|
||||||
|
|
||||||
interface RepayFormProps {
|
interface RepayFormProps {
|
||||||
onSuccess: () => void
|
onSuccess: () => void
|
||||||
|
@ -43,7 +46,9 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
|
||||||
const { mangoAccount } = useMangoAccount()
|
const { mangoAccount } = useMangoAccount()
|
||||||
const [inputAmount, setInputAmount] = useState('')
|
const [inputAmount, setInputAmount] = useState('')
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
const [selectedToken, setSelectedToken] = useState(token)
|
const [selectedToken, setSelectedToken] = useState(
|
||||||
|
token || INPUT_TOKEN_DEFAULT
|
||||||
|
)
|
||||||
const [showTokenList, setShowTokenList] = useState(false)
|
const [showTokenList, setShowTokenList] = useState(false)
|
||||||
const [sizePercentage, setSizePercentage] = useState('')
|
const [sizePercentage, setSizePercentage] = useState('')
|
||||||
const { mangoTokens } = useJupiterMints()
|
const { mangoTokens } = useJupiterMints()
|
||||||
|
@ -51,7 +56,7 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
|
||||||
|
|
||||||
const bank = useMemo(() => {
|
const bank = useMemo(() => {
|
||||||
const group = mangoStore.getState().group
|
const group = mangoStore.getState().group
|
||||||
return selectedToken ? group?.banksMapByName.get(selectedToken)?.[0] : null
|
return group?.banksMapByName.get(selectedToken)?.[0]
|
||||||
}, [selectedToken])
|
}, [selectedToken])
|
||||||
|
|
||||||
const logoUri = useMemo(() => {
|
const logoUri = useMemo(() => {
|
||||||
|
@ -172,6 +177,8 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
|
||||||
}
|
}
|
||||||
}, [token, banks])
|
}, [token, banks])
|
||||||
|
|
||||||
|
const exceedsAlphaMax = useAlphaMax(inputAmount, bank)
|
||||||
|
|
||||||
const showInsufficientBalance = walletBalance.maxAmount < Number(inputAmount)
|
const showInsufficientBalance = walletBalance.maxAmount < Number(inputAmount)
|
||||||
|
|
||||||
return banks.length ? (
|
return banks.length ? (
|
||||||
|
@ -325,7 +332,9 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
|
||||||
<Button
|
<Button
|
||||||
onClick={() => handleDeposit(inputAmount)}
|
onClick={() => handleDeposit(inputAmount)}
|
||||||
className="flex w-full items-center justify-center"
|
className="flex w-full items-center justify-center"
|
||||||
disabled={!inputAmount || showInsufficientBalance}
|
disabled={
|
||||||
|
!inputAmount || showInsufficientBalance || exceedsAlphaMax
|
||||||
|
}
|
||||||
size="large"
|
size="large"
|
||||||
>
|
>
|
||||||
{submitting ? (
|
{submitting ? (
|
||||||
|
|
Loading…
Reference in New Issue