fix max swap amounts for reduce only

This commit is contained in:
saml33 2023-05-12 12:25:16 +10:00
parent ef383bec36
commit e314901f27
3 changed files with 16 additions and 15 deletions

View File

@ -28,7 +28,8 @@ const MaxSwapAmount = ({
return (
<div className="flex flex-wrap justify-end pl-6 text-xs">
{tokenMax.lt(amountWithBorrow) ? (
{tokenMax.lt(amountWithBorrow) ||
(tokenMax.eq(amountWithBorrow) && !useMargin) ? (
<MaxAmountButton
className="mb-0.5"
decimals={decimals}

View File

@ -86,7 +86,6 @@ const SwapForm = () => {
const [debouncedAmountIn] = useDebounce(amountInFormValue, 300)
const [debouncedAmountOut] = useDebounce(amountOutFormValue, 300)
const { mangoAccount } = useMangoAccount()
const { isDelegatedAccount } = useUnownedAccount()
const { connected, publicKey } = useWallet()
const amountInAsDecimal: Decimal | null = useMemo(() => {
@ -441,7 +440,6 @@ const SwapForm = () => {
amountOut={
selectedRoute ? amountOutAsDecimal.toNumber() : undefined
}
isDelegatedAccount={isDelegatedAccount}
/>
) : (
<Button
@ -457,7 +455,9 @@ const SwapForm = () => {
{group && inputBank ? (
<TokenVaultWarnings bank={inputBank} type="swap" />
) : null}
{inputBank && inputBank.reduceOnly ? (
{inputBank &&
inputBank.areBorrowsReduceOnly() &&
inputBank.areDepositsReduceOnly() ? (
<div className="pb-4">
<InlineNotification
type="warning"
@ -467,7 +467,9 @@ const SwapForm = () => {
/>
</div>
) : null}
{outputBank && outputBank.reduceOnly ? (
{outputBank &&
outputBank.areBorrowsReduceOnly() &&
outputBank.areDepositsReduceOnly() ? (
<div className="pb-4">
<InlineNotification
type="warning"
@ -513,7 +515,6 @@ const SwapFormSubmitButton = ({
selectedRoute,
setShowConfirm,
useMargin,
isDelegatedAccount,
}: {
amountIn: Decimal
amountOut: number | undefined
@ -522,7 +523,6 @@ const SwapFormSubmitButton = ({
selectedRoute: RouteInfo | undefined | null
setShowConfirm: (x: boolean) => void
useMargin: boolean
isDelegatedAccount: boolean
}) => {
const { t } = useTranslation('common')
const { connected } = useWallet()
@ -550,9 +550,7 @@ const SwapFormSubmitButton = ({
disabled={disabled}
size="large"
>
{isDelegatedAccount ? (
<div>Swap Unavailable for Delegates</div>
) : connected ? (
{connected ? (
showInsufficientBalance ? (
<div className="flex items-center">
<ExclamationCircleIcon className="mr-2 h-5 w-5 flex-shrink-0" />

View File

@ -45,6 +45,9 @@ export const getTokenInMax = (
}
}
const inputReduceOnly = inputBank.areBorrowsReduceOnly()
const outputReduceOnly = outputBank.areDepositsReduceOnly()
const inputTokenBalance = new Decimal(
mangoAccount.getTokenBalanceUi(inputBank)
)
@ -54,8 +57,8 @@ export const getTokenInMax = (
)
const maxAmountWithoutMargin =
(inputTokenBalance.gt(0) && !outputBank.reduceOnly) ||
(outputBank.reduceOnly && outputTokenBalance.lt(0))
(inputTokenBalance.gt(0) && !outputReduceOnly) ||
(outputReduceOnly && outputTokenBalance.lt(0))
? inputTokenBalance
: new Decimal(0)
@ -66,8 +69,7 @@ export const getTokenInMax = (
)
const maxUiAmountWithBorrow =
outputBank.reduceOnly &&
(outputTokenBalance.gt(0) || outputTokenBalance.eq(0))
outputReduceOnly && (outputTokenBalance.gt(0) || outputTokenBalance.eq(0))
? new Decimal(0)
: rawMaxUiAmountWithBorrow > 0
? floorToDecimal(rawMaxUiAmountWithBorrow, inputBank.mintDecimals)
@ -92,7 +94,7 @@ export const getTokenInMax = (
maxUiAmountWithBorrow
)
const maxAmountWithBorrow = inputBank.reduceOnly
const maxAmountWithBorrow = inputReduceOnly
? Decimal.min(maxAmountWithoutMargin, inputBankVaultBalance)
: Decimal.min(maxUiAmountWithBorrow, inputBankVaultBalance)