From fbb38629068309784115cb19061d5afad72e28ff Mon Sep 17 00:00:00 2001
From: saml33 <30796577+saml33@users.noreply.github.com>
Date: Mon, 1 Jan 2024 23:46:08 +1100
Subject: [PATCH] fix reduce only max amounts (#361)
---
components/swap/MaxSwapAmount.tsx | 17 +++++----
components/swap/SwapForm.tsx | 7 ++--
components/swap/SwapFormTokenList.tsx | 8 +----
components/swap/useTokenMax.tsx | 41 +++++++--------------
components/trade/SpotSlider.tsx | 51 +++++++++++++++++++--------
public/locales/en/swap.json | 2 +-
public/locales/es/swap.json | 2 +-
public/locales/ru/swap.json | 2 +-
public/locales/zh/swap.json | 2 +-
public/locales/zh_tw/swap.json | 2 +-
10 files changed, 67 insertions(+), 67 deletions(-)
diff --git a/components/swap/MaxSwapAmount.tsx b/components/swap/MaxSwapAmount.tsx
index 484a4505..28d2a765 100644
--- a/components/swap/MaxSwapAmount.tsx
+++ b/components/swap/MaxSwapAmount.tsx
@@ -16,7 +16,11 @@ const MaxSwapAmount = ({
}) => {
const { t } = useTranslation('common')
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
- const { amount: tokenMax, amountWithBorrow, decimals } = maxAmount(useMargin)
+ const {
+ amount: spotMax,
+ amountWithBorrow: leverageMax,
+ decimals,
+ } = maxAmount(useMargin)
if (mangoAccountLoading) return null
@@ -26,14 +30,13 @@ const MaxSwapAmount = ({
return (
- {tokenMax.lt(amountWithBorrow) ||
- (tokenMax.eq(amountWithBorrow) && !useMargin) ? (
+ {spotMax.lt(leverageMax) || (spotMax.eq(leverageMax) && !useMargin) ? (
setMax(tokenMax)}
- value={tokenMax}
+ onClick={() => setMax(spotMax)}
+ value={spotMax}
/>
) : null}
{useMargin ? (
@@ -41,8 +44,8 @@ const MaxSwapAmount = ({
className="mb-0.5 ml-2"
decimals={decimals}
label={t('max')}
- onClick={() => setMax(amountWithBorrow)}
- value={amountWithBorrow}
+ onClick={() => setMax(leverageMax)}
+ value={leverageMax}
/>
) : null}
diff --git a/components/swap/SwapForm.tsx b/components/swap/SwapForm.tsx
index c11c2f68..5e1dc0c5 100644
--- a/components/swap/SwapForm.tsx
+++ b/components/swap/SwapForm.tsx
@@ -201,7 +201,7 @@ const SwapForm = () => {
{inputBank &&
!walletSwap &&
inputBank.areBorrowsReduceOnly() &&
- inputBank.areDepositsReduceOnly() ? (
+ useMargin ? (
{
/>
) : null}
- {outputBank &&
- !walletSwap &&
- outputBank.areBorrowsReduceOnly() &&
- outputBank.areDepositsReduceOnly() ? (
+ {outputBank && !walletSwap && outputBank.areDepositsReduceOnly() ? (
{
const inputBank = group.getFirstBankByMint(inputMint)
const outputBank = group.getFirstBankByMint(outputMint)
@@ -87,13 +86,6 @@ export const getTokenInMax = (
const outputTokenBalance = new Decimal(
mangoAccount.getTokenBalanceUi(outputBank),
)
-
- const maxAmountWithoutMargin =
- (inputTokenBalance.gt(0) && !outputReduceOnly) ||
- (outputReduceOnly && outputTokenBalance.lt(0))
- ? inputTokenBalance
- : new Decimal(0)
-
const rawMaxUiAmountWithBorrow = getMaxSourceForSwap(
group,
mangoAccount,
@@ -101,12 +93,16 @@ export const getTokenInMax = (
outputBank.mint,
)
- const maxUiAmountWithBorrow =
- outputReduceOnly && (outputTokenBalance.gt(0) || outputTokenBalance.eq(0))
- ? new Decimal(0)
- : rawMaxUiAmountWithBorrow > 0
- ? floorToDecimal(rawMaxUiAmountWithBorrow, inputBank.mintDecimals)
- : new Decimal(0)
+ let spotMax = new Decimal(0)
+ let leverageMax = new Decimal(0)
+
+ if (!outputReduceOnly || (outputReduceOnly && outputTokenBalance.lt(0))) {
+ spotMax = inputTokenBalance
+ leverageMax = floorToDecimal(
+ rawMaxUiAmountWithBorrow,
+ inputBank.mintDecimals,
+ )
+ }
const inputBankVaultBalance = floorToDecimal(
group
@@ -115,21 +111,11 @@ export const getTokenInMax = (
inputBank.mintDecimals,
)
- const maxAmount = useMargin
- ? Decimal.min(
- maxAmountWithoutMargin,
- inputBankVaultBalance,
- maxUiAmountWithBorrow,
- )
- : Decimal.min(
- maxAmountWithoutMargin,
- inputBankVaultBalance,
- maxUiAmountWithBorrow,
- )
+ const maxAmount = Decimal.min(spotMax, leverageMax, inputBankVaultBalance)
const maxAmountWithBorrow = inputReduceOnly
- ? Decimal.min(maxAmountWithoutMargin, inputBankVaultBalance)
- : Decimal.min(maxUiAmountWithBorrow, inputBankVaultBalance)
+ ? Decimal.min(spotMax, leverageMax, inputBankVaultBalance)
+ : Decimal.min(leverageMax, inputBankVaultBalance)
return {
amount: maxAmount,
@@ -158,7 +144,6 @@ export const useTokenMax = (useMargin = true): TokenMaxResults => {
inputBank.mint,
outputBank.mint,
group,
- useMargin,
)
}
} catch (e) {
diff --git a/components/trade/SpotSlider.tsx b/components/trade/SpotSlider.tsx
index 477a0b7b..e51c867c 100644
--- a/components/trade/SpotSlider.tsx
+++ b/components/trade/SpotSlider.tsx
@@ -30,32 +30,53 @@ export const useSpotMarketMax = (
selectedMarket.serumMarketExternal,
)
if (side === 'buy') {
- leverageMax = mangoAccount.getMaxQuoteForSerum3BidUi(
- group,
- selectedMarket.serumMarketExternal,
- )
- const bank = group.getFirstBankByTokenIndex(
+ const quoteBank = group.getFirstBankByTokenIndex(
selectedMarket.quoteTokenIndex,
)
- const balance = mangoAccount.getTokenBalanceUi(bank)
- const unsettled = spotBalances[bank.mint.toString()]?.unsettled || 0
- const tickDecimals = getDecimalCount(market.tickSize)
- spotMax = floorToDecimal(balance + unsettled, tickDecimals).toNumber()
- } else {
- leverageMax = mangoAccount.getMaxBaseForSerum3AskUi(
+ const balance = mangoAccount.getTokenBalanceUi(quoteBank)
+ const quoteLeverageMax = mangoAccount.getMaxQuoteForSerum3BidUi(
group,
selectedMarket.serumMarketExternal,
)
- const bank = group.getFirstBankByTokenIndex(
+ const unsettled =
+ spotBalances[quoteBank?.mint.toString()]?.unsettled || 0
+ const tickDecimals = getDecimalCount(market.tickSize)
+ const roundedBalanceMax = floorToDecimal(
+ balance + unsettled,
+ tickDecimals,
+ ).toNumber()
+ spotMax = roundedBalanceMax
+
+ const isReduceOnly = quoteBank?.areBorrowsReduceOnly()
+ if (isReduceOnly) {
+ leverageMax = roundedBalanceMax
+ } else {
+ leverageMax = quoteLeverageMax
+ }
+ } else {
+ const baseBank = group.getFirstBankByTokenIndex(
selectedMarket.baseTokenIndex,
)
- const balance = mangoAccount.getTokenBalanceUi(bank)
- const unsettled = spotBalances[bank.mint.toString()]?.unsettled || 0
+ const baseLeverageMax = mangoAccount.getMaxBaseForSerum3AskUi(
+ group,
+ selectedMarket.serumMarketExternal,
+ )
+ const balance = mangoAccount.getTokenBalanceUi(baseBank)
+ const unsettled =
+ spotBalances[baseBank?.mint.toString()]?.unsettled || 0
const minOrderDecimals = getDecimalCount(market.minOrderSize)
- spotMax = floorToDecimal(
+ const roundedBalanceMax = floorToDecimal(
balance + unsettled,
minOrderDecimals,
).toNumber()
+ spotMax = roundedBalanceMax
+
+ const isReduceOnly = baseBank?.areBorrowsReduceOnly()
+ if (isReduceOnly) {
+ leverageMax = roundedBalanceMax
+ } else {
+ leverageMax = baseLeverageMax
+ }
}
return useMargin ? leverageMax : Math.max(spotMax, 0)
} catch (e) {
diff --git a/public/locales/en/swap.json b/public/locales/en/swap.json
index 7da7f28c..c61a1f1a 100644
--- a/public/locales/en/swap.json
+++ b/public/locales/en/swap.json
@@ -13,7 +13,7 @@
"hide-fees": "Hide Fees",
"hide-swap-history": "Hide Swap History",
"important-info": "Important Info",
- "input-reduce-only-warning": "{{symbol}} is in reduce only mode. You can swap your balance to another token",
+ "input-reduce-only-warning": "{{symbol}} borrows are disabled",
"insufficient-balance": "Insufficient {{symbol}} Balance",
"insufficient-collateral": "Insufficient Collateral",
"margin": "Margin",
diff --git a/public/locales/es/swap.json b/public/locales/es/swap.json
index 7da7f28c..c61a1f1a 100644
--- a/public/locales/es/swap.json
+++ b/public/locales/es/swap.json
@@ -13,7 +13,7 @@
"hide-fees": "Hide Fees",
"hide-swap-history": "Hide Swap History",
"important-info": "Important Info",
- "input-reduce-only-warning": "{{symbol}} is in reduce only mode. You can swap your balance to another token",
+ "input-reduce-only-warning": "{{symbol}} borrows are disabled",
"insufficient-balance": "Insufficient {{symbol}} Balance",
"insufficient-collateral": "Insufficient Collateral",
"margin": "Margin",
diff --git a/public/locales/ru/swap.json b/public/locales/ru/swap.json
index 7da7f28c..c61a1f1a 100644
--- a/public/locales/ru/swap.json
+++ b/public/locales/ru/swap.json
@@ -13,7 +13,7 @@
"hide-fees": "Hide Fees",
"hide-swap-history": "Hide Swap History",
"important-info": "Important Info",
- "input-reduce-only-warning": "{{symbol}} is in reduce only mode. You can swap your balance to another token",
+ "input-reduce-only-warning": "{{symbol}} borrows are disabled",
"insufficient-balance": "Insufficient {{symbol}} Balance",
"insufficient-collateral": "Insufficient Collateral",
"margin": "Margin",
diff --git a/public/locales/zh/swap.json b/public/locales/zh/swap.json
index 1371920f..2ee03131 100644
--- a/public/locales/zh/swap.json
+++ b/public/locales/zh/swap.json
@@ -13,7 +13,7 @@
"hide-fees": "隐藏费用",
"hide-swap-history": "隐藏换币纪录",
"important-info": "重要资料",
- "input-reduce-only-warning": "{{symbol}}处于仅减少模式。您可以将余额换成另一个币种",
+ "input-reduce-only-warning": "{{symbol}} borrows are disabled",
"insufficient-balance": "{{symbol}}余额不够",
"insufficient-collateral": "质押品不够",
"margin": "保证金",
diff --git a/public/locales/zh_tw/swap.json b/public/locales/zh_tw/swap.json
index 9a7d5509..02bfe845 100644
--- a/public/locales/zh_tw/swap.json
+++ b/public/locales/zh_tw/swap.json
@@ -13,7 +13,7 @@
"hide-fees": "隱藏費用",
"hide-swap-history": "隱藏換幣紀錄",
"important-info": "重要資料",
- "input-reduce-only-warning": "{{symbol}}處於僅減少模式。您可以將餘額換成另一個幣種",
+ "input-reduce-only-warning": "{{symbol}} borrows are disabled",
"insufficient-balance": "{{symbol}}餘額不夠",
"insufficient-collateral": "質押品不夠",
"margin": "保證金",