show borrow amount in order desc when reducing short on margin

This commit is contained in:
saml33 2023-09-08 12:39:39 +10:00
parent d0845efbae
commit a30b5a42e4
7 changed files with 77 additions and 21 deletions

View File

@ -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,

View File

@ -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 (
<TrBody key={i} className="text-sm">
<Td>{pair}</Td>
@ -292,7 +305,7 @@ const SwapOrders = () => {
{size}
<span className="font-body text-th-fgd-3">
{' '}
{bank.name}
{formattedBaseName}
</span>
</p>
</Td>
@ -301,7 +314,7 @@ const SwapOrders = () => {
{filled}/{size}
<span className="font-body text-th-fgd-3">
{' '}
{bank.name}
{formattedBaseName}
</span>
</p>
</Td>
@ -310,7 +323,7 @@ const SwapOrders = () => {
{currentPrice}
<span className="font-body text-th-fgd-3">
{' '}
{buyBank.name}
{formattedQuoteName}
</span>
</p>
</Td>
@ -319,7 +332,7 @@ const SwapOrders = () => {
{triggerPrice}
<span className="font-body text-th-fgd-3">
{' '}
{side === 'buy' ? sellBank.name : buyBank.name}
{formattedQuoteName}
</span>
</p>
</Td>
@ -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 (
<Disclosure key={i}>
{({ open }) => (
@ -379,7 +397,7 @@ const SwapOrders = () => {
{size}
<span className="font-body text-th-fgd-3">
{' '}
{bank.name}
{formattedBaseName}
</span>
<span className="font-body text-th-fgd-3">
{' at '}
@ -387,7 +405,7 @@ const SwapOrders = () => {
{triggerPrice}
<span className="font-body text-th-fgd-3">
{' '}
{side === 'buy' ? sellBank.name : buyBank.name}
{formattedQuoteName}
</span>
</p>
</div>
@ -413,7 +431,7 @@ const SwapOrders = () => {
{size}
<span className="font-body text-th-fgd-3">
{' '}
{bank.name}
{formattedBaseName}
</span>
</p>
</div>
@ -425,7 +443,7 @@ const SwapOrders = () => {
{filled}/{size}
<span className="font-body text-th-fgd-3">
{' '}
{bank.name}
{formattedBaseName}
</span>
</p>
</div>
@ -437,7 +455,7 @@ const SwapOrders = () => {
{currentPrice}
<span className="font-body text-th-fgd-3">
{' '}
{buyBank.name}
{formattedQuoteName}
</span>
</p>
</div>
@ -449,7 +467,9 @@ const SwapOrders = () => {
{triggerPrice}
<span className="font-body text-th-fgd-3">
{' '}
{side === 'buy' ? sellBank.name : buyBank.name}
{side === 'buy'
? formattedSellTokenName
: formattedBuyTokenName}
</span>
</p>
</div>

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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": "差价",

View File

@ -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": "未實現盈虧",