add alpha limit to repay form

This commit is contained in:
tjs 2023-01-02 14:49:50 -05:00
parent c88944aa80
commit fc161c1906
2 changed files with 37 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
import { Bank, toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
import {
ArrowDownTrayIcon,
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) {
const { t } = useTranslation('common')
const { group } = useMangoGroup()
@ -79,6 +100,7 @@ function DepositForm({ onSuccess, token }: DepositFormProps) {
const group = mangoStore.getState().group
return group?.banksMapByName.get(selectedToken)?.[0]
}, [selectedToken])
const exceedsAlphaMax = useAlphaMax(inputAmount, bank)
const logoUri = useMemo(() => {
let logoURI
@ -176,24 +198,6 @@ function DepositForm({ onSuccess, token }: DepositFormProps) {
return banks
}, [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)
return (

View File

@ -25,12 +25,15 @@ import { EnterBottomExitBottom, FadeInFadeOut } from './shared/Transitions'
import { withValueLimit } from './swap/SwapForm'
import MaxAmountButton from '@components/shared/MaxAmountButton'
import HealthImpactTokenChange from '@components/HealthImpactTokenChange'
import { walletBalanceForToken } from './DepositForm'
import { useAlphaMax, walletBalanceForToken } from './DepositForm'
import SolBalanceWarnings from '@components/shared/SolBalanceWarnings'
import useMangoAccount from 'hooks/useMangoAccount'
import useJupiterMints from 'hooks/useJupiterMints'
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 {
onSuccess: () => void
@ -43,7 +46,9 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
const { mangoAccount } = useMangoAccount()
const [inputAmount, setInputAmount] = useState('')
const [submitting, setSubmitting] = useState(false)
const [selectedToken, setSelectedToken] = useState(token)
const [selectedToken, setSelectedToken] = useState(
token || INPUT_TOKEN_DEFAULT
)
const [showTokenList, setShowTokenList] = useState(false)
const [sizePercentage, setSizePercentage] = useState('')
const { mangoTokens } = useJupiterMints()
@ -51,7 +56,7 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
const bank = useMemo(() => {
const group = mangoStore.getState().group
return selectedToken ? group?.banksMapByName.get(selectedToken)?.[0] : null
return group?.banksMapByName.get(selectedToken)?.[0]
}, [selectedToken])
const logoUri = useMemo(() => {
@ -172,6 +177,8 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
}
}, [token, banks])
const exceedsAlphaMax = useAlphaMax(inputAmount, bank)
const showInsufficientBalance = walletBalance.maxAmount < Number(inputAmount)
return banks.length ? (
@ -325,7 +332,9 @@ function RepayForm({ onSuccess, token }: RepayFormProps) {
<Button
onClick={() => handleDeposit(inputAmount)}
className="flex w-full items-center justify-center"
disabled={!inputAmount || showInsufficientBalance}
disabled={
!inputAmount || showInsufficientBalance || exceedsAlphaMax
}
size="large"
>
{submitting ? (