From a30b5a42e4759d79133d6281439911146365839e Mon Sep 17 00:00:00 2001
From: saml33
Date: Fri, 8 Sep 2023 12:39:39 +1000
Subject: [PATCH] show borrow amount in order desc when reducing short on
margin
---
components/swap/LimitSwapForm.tsx | 47 ++++++++++++++++++++++-----
components/swap/SwapTriggerOrders.tsx | 46 ++++++++++++++++++--------
public/locales/en/trade.json | 1 +
public/locales/es/trade.json | 1 +
public/locales/ru/trade.json | 1 +
public/locales/zh/trade.json | 1 +
public/locales/zh_tw/trade.json | 1 +
7 files changed, 77 insertions(+), 21 deletions(-)
diff --git a/components/swap/LimitSwapForm.tsx b/components/swap/LimitSwapForm.tsx
index 06d035b5..19dd3b63 100644
--- a/components/swap/LimitSwapForm.tsx
+++ b/components/swap/LimitSwapForm.tsx
@@ -81,6 +81,13 @@ export const getInputTokenBalance = (inputBank: Bank | undefined) => {
return balance
}
+const getOutputTokenBalance = (outputBank: Bank | undefined) => {
+ const mangoAccount = mangoStore.getState().mangoAccount.current
+ if (!outputBank || !mangoAccount) return 0
+ const balance = mangoAccount.getTokenBalanceUi(outputBank)
+ return balance
+}
+
const getOrderTypeMultiplier = (
orderType: OrderTypes,
flipPrices: boolean,
@@ -555,6 +562,18 @@ const LimitSwapForm = ({
const action = isReducingShort ? t('buy') : t('sell')
+ // calc borrowed amount when reducing short
+ let borrowToReduceShort = 0
+ if (isReducingShort && mangoAccountAddress) {
+ const balance = getOutputTokenBalance(outputBank)
+ if (balance >= 0 && parseFloat(amountOutFormValue) > balance) {
+ const amount = new Decimal(balance)
+ .sub(new Decimal(amountOutFormValue))
+ .toNumber()
+ borrowToReduceShort = Math.abs(amount)
+ } else borrowToReduceShort = parseFloat(amountOutFormValue)
+ }
+
// xor of two flip flags
const shouldFlip = flipPrices !== isReducingShort
const orderTypeString =
@@ -566,20 +585,32 @@ const LimitSwapForm = ({
? t('trade:falls-to')
: t('trade:rises-to')
- return t('trade:trigger-order-desc', {
- action: action,
- amount: floorToDecimal(amountInFormValue, inputBankDecimals),
- orderType: orderTypeString,
- priceUnit: quoteString,
- symbol: formattedInputTokenName,
- triggerPrice: priceToDisplayString(triggerPrice),
- })
+ return borrowToReduceShort
+ ? t('trade:trigger-order-desc-with-borrow', {
+ action: action,
+ amount: floorToDecimal(amountInFormValue, inputBankDecimals),
+ borrowAmount: borrowToReduceShort,
+ orderType: orderTypeString,
+ priceUnit: quoteString,
+ quoteSymbol: formattedOutputTokenName,
+ symbol: formattedInputTokenName,
+ triggerPrice: priceToDisplayString(triggerPrice),
+ })
+ : t('trade:trigger-order-desc', {
+ action: action,
+ amount: floorToDecimal(amountInFormValue, inputBankDecimals),
+ orderType: orderTypeString,
+ priceUnit: quoteString,
+ symbol: formattedInputTokenName,
+ triggerPrice: priceToDisplayString(triggerPrice),
+ })
}, [
amountInFormValue,
amountOutFormValue,
flipPrices,
inputBankDecimals,
inputBankName,
+ mangoAccountAddress,
orderType,
outputBankDecimals,
outputBankName,
diff --git a/components/swap/SwapTriggerOrders.tsx b/components/swap/SwapTriggerOrders.tsx
index 23524116..8aa064ad 100644
--- a/components/swap/SwapTriggerOrders.tsx
+++ b/components/swap/SwapTriggerOrders.tsx
@@ -31,6 +31,7 @@ import Loading from '@components/shared/Loading'
import SideBadge from '@components/shared/SideBadge'
import { Disclosure, Transition } from '@headlessui/react'
import SheenLoader from '@components/shared/SheenLoader'
+import { formatTokenSymbol } from 'utils/tokens'
const SwapOrders = () => {
const { t } = useTranslation(['common', 'swap', 'trade'])
@@ -46,13 +47,14 @@ const SwapOrders = () => {
return mangoAccount.tokenConditionalSwaps.filter((tcs) => tcs.hasData)
}, [mangoAccount])
+ console.log(orders)
+
const formattedTableData = useCallback(() => {
if (!group) return []
const formatted = []
for (const order of orders) {
const buyBank = group.getFirstBankByTokenIndex(order.buyTokenIndex)
const sellBank = group.getFirstBankByTokenIndex(order.sellTokenIndex)
- const pair = `${sellBank.name}/${buyBank.name}`
const maxBuy = floorToDecimal(
order.getMaxBuyUi(group),
buyBank.mintDecimals,
@@ -70,6 +72,12 @@ const SwapOrders = () => {
size = maxBuy
side = 'buy'
}
+ const buyTokenName = formatTokenSymbol(buyBank.name)
+ const sellTokenName = formatTokenSymbol(sellBank.name)
+ const pair =
+ side === 'sell'
+ ? `${sellTokenName}/${buyTokenName}`
+ : `${buyTokenName}/${sellTokenName}`
const triggerPrice = order.getThresholdPriceUi(group)
const pricePremium = order.getPricePremium()
@@ -278,7 +286,12 @@ const SwapOrders = () => {
triggerPrice,
} = data
- const bank = side === 'buy' ? buyBank : sellBank
+ const formattedBuyTokenName = formatTokenSymbol(buyBank.name)
+ const formattedSellTokenName = formatTokenSymbol(sellBank.name)
+ const formattedBaseName =
+ side === 'buy' ? formattedBuyTokenName : formattedSellTokenName
+ const formattedQuoteName =
+ side === 'buy' ? formattedSellTokenName : formattedBuyTokenName
return (
{pair} |
@@ -292,7 +305,7 @@ const SwapOrders = () => {
{size}
{' '}
- {bank.name}
+ {formattedBaseName}
@@ -301,7 +314,7 @@ const SwapOrders = () => {
{filled}/{size}
{' '}
- {bank.name}
+ {formattedBaseName}
@@ -310,7 +323,7 @@ const SwapOrders = () => {
{currentPrice}
{' '}
- {buyBank.name}
+ {formattedQuoteName}
@@ -319,7 +332,7 @@ const SwapOrders = () => {
{triggerPrice}
{' '}
- {side === 'buy' ? sellBank.name : buyBank.name}
+ {formattedQuoteName}
@@ -361,7 +374,12 @@ const SwapOrders = () => {
triggerPrice,
} = data
- const bank = side === 'buy' ? buyBank : sellBank
+ const formattedBuyTokenName = formatTokenSymbol(buyBank.name)
+ const formattedSellTokenName = formatTokenSymbol(sellBank.name)
+ const formattedBaseName =
+ side === 'buy' ? formattedBuyTokenName : formattedSellTokenName
+ const formattedQuoteName =
+ side === 'buy' ? formattedSellTokenName : formattedBuyTokenName
return (
{({ open }) => (
@@ -379,7 +397,7 @@ const SwapOrders = () => {
{size}
{' '}
- {bank.name}
+ {formattedBaseName}
{' at '}
@@ -387,7 +405,7 @@ const SwapOrders = () => {
{triggerPrice}
{' '}
- {side === 'buy' ? sellBank.name : buyBank.name}
+ {formattedQuoteName}
@@ -413,7 +431,7 @@ const SwapOrders = () => {
{size}
{' '}
- {bank.name}
+ {formattedBaseName}
@@ -425,7 +443,7 @@ const SwapOrders = () => {
{filled}/{size}
{' '}
- {bank.name}
+ {formattedBaseName}
@@ -437,7 +455,7 @@ const SwapOrders = () => {
{currentPrice}
{' '}
- {buyBank.name}
+ {formattedQuoteName}
@@ -449,7 +467,9 @@ const SwapOrders = () => {
{triggerPrice}
{' '}
- {side === 'buy' ? sellBank.name : buyBank.name}
+ {side === 'buy'
+ ? formattedSellTokenName
+ : formattedBuyTokenName}
diff --git a/public/locales/en/trade.json b/public/locales/en/trade.json
index 2af7995a..035f63ad 100644
--- a/public/locales/en/trade.json
+++ b/public/locales/en/trade.json
@@ -117,6 +117,7 @@
"trigger-price": "Trigger Price",
"trigger-order": "Trigger Order",
"trigger-order-desc": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}",
+ "trigger-order-desc-with-borrow": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}. You'll borrow ~{{borrowAmount}} {{quoteSymbol}} to execute this trade",
"trigger-orders": "Trigger Orders",
"tweet-position": "Tweet",
"unrealized-pnl": "Unrealized PnL",
diff --git a/public/locales/es/trade.json b/public/locales/es/trade.json
index 3f6c3589..ac9eb0c6 100644
--- a/public/locales/es/trade.json
+++ b/public/locales/es/trade.json
@@ -117,6 +117,7 @@
"trigger-price": "Trigger Price",
"trigger-order": "Trigger Order",
"trigger-order-desc": "{{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}",
+ "trigger-order-desc-with-borrow": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}. You'll borrow ~{{borrowAmount}} {{quoteSymbol}} to execute this trade",
"trigger-orders": "Trigger Orders",
"tweet-position": "Tweet",
"unrealized-pnl": "Unrealized PnL",
diff --git a/public/locales/ru/trade.json b/public/locales/ru/trade.json
index 3f6c3589..ac9eb0c6 100644
--- a/public/locales/ru/trade.json
+++ b/public/locales/ru/trade.json
@@ -117,6 +117,7 @@
"trigger-price": "Trigger Price",
"trigger-order": "Trigger Order",
"trigger-order-desc": "{{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}",
+ "trigger-order-desc-with-borrow": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}. You'll borrow ~{{borrowAmount}} {{quoteSymbol}} to execute this trade",
"trigger-orders": "Trigger Orders",
"tweet-position": "Tweet",
"unrealized-pnl": "Unrealized PnL",
diff --git a/public/locales/zh/trade.json b/public/locales/zh/trade.json
index ff7474ac..234f661c 100644
--- a/public/locales/zh/trade.json
+++ b/public/locales/zh/trade.json
@@ -96,6 +96,7 @@
"trigger-price": "Trigger Price",
"trigger-order": "Trigger Order",
"trigger-order-desc": "{{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}",
+ "trigger-order-desc-with-borrow": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}. You'll borrow ~{{borrowAmount}} {{quoteSymbol}} to execute this trade",
"trigger-orders": "Trigger Orders",
"size": "数量",
"spread": "差价",
diff --git a/public/locales/zh_tw/trade.json b/public/locales/zh_tw/trade.json
index 9940b73e..cb885342 100644
--- a/public/locales/zh_tw/trade.json
+++ b/public/locales/zh_tw/trade.json
@@ -117,6 +117,7 @@
"trigger-price": "Trigger Price",
"trigger-order": "Trigger Order",
"trigger-order-desc": "{{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}",
+ "trigger-order-desc-with-borrow": "{{action}} {{amount}} {{symbol}} if the oracle price {{orderType}} {{triggerPrice}} {{priceUnit}}. You'll borrow ~{{borrowAmount}} {{quoteSymbol}} to execute this trade",
"trigger-orders": "Trigger Orders",
"tweet-position": "分享至Twitter",
"unrealized-pnl": "未實現盈虧",