add close instructions

This commit is contained in:
saml33 2023-09-12 23:32:09 +10:00
parent 2ba547124b
commit 77c3bdd359
8 changed files with 58 additions and 41 deletions

View File

@ -43,10 +43,8 @@ const AccountSettings = () => {
usedSerum3,
usedPerps,
usedPerpOo,
// emptyTokens,
emptySerum3,
emptyPerps,
// emptyPerpOo,
totalTokens,
totalSerum3,
totalPerps,
@ -62,7 +60,7 @@ const AccountSettings = () => {
const bank = group.getFirstBankByTokenIndex(token.tokenIndex)
const tokenMax = getMaxWithdrawForBank(group, bank, mangoAccount)
const balance = mangoAccount.getTokenBalanceUi(bank)
const isClosable = tokenMax.eq(new Decimal(balance))
const isClosable = tokenMax.eq(new Decimal(balance)) && !token.inUseCount
tokens.push({ isClosable, balance, tokenIndex: token.tokenIndex })
}
return tokens
@ -159,6 +157,7 @@ const AccountSettings = () => {
txid: tx.signature,
})
await actions.reloadMangoAccount()
setSubmitting(false)
} catch (e) {
console.error(e)
setSubmitting(false)
@ -234,6 +233,11 @@ const AccountSettings = () => {
const status = tokenStatus.find(
(t) => t.tokenIndex === token.tokenIndex,
)
const isCollateral =
tokenBank
.scaledInitAssetWeight(tokenBank.price)
.toNumber() > 0
return (
<div className={SLOT_ROW_CLASSNAMES} key={token.tokenIndex}>
<div className="flex items-center">
@ -255,7 +259,28 @@ const AccountSettings = () => {
>
{t('close')}
</Button>
) : null}
) : (
<Tooltip
content={
tokenBank.name === 'USDC'
? t('settings:tooltip-close-usdc-instructions')
: isCollateral
? t(
'settings:tooltip-close-collateral-token-instructions',
{
token: tokenBank.name,
},
)
: t('settings:tooltip-close-token-instructions', {
token: tokenBank.name,
})
}
>
<p className="tooltip-underline">
{t('settings:close-instructions')}
</p>
</Tooltip>
)}
</div>
)
})

View File

@ -212,7 +212,7 @@ const OpenOrders = () => {
o.perpMarketIndex,
o.orderId,
)
actions.fetchOpenOrders()
actions.fetchOpenOrders(true)
notify({
type: 'success',
title: 'Transaction successful',

View File

@ -8,7 +8,6 @@ import {
TokenPosition,
} from '@blockworks-foundation/mango-v4'
import { MAX_ACCOUNTS } from 'utils/constants'
import useBanksWithBalances from './useBanksWithBalances'
import { OpenOrders } from '@project-serum/serum'
import { BN } from '@coral-xyz/anchor'
@ -47,21 +46,18 @@ const getIsAccountSizeFull = () => {
}
export default function useMangoAccountAccounts() {
const { mangoAccountAddress, mangoAccount } = useMangoAccount()
const banks = useBanksWithBalances()
const { mangoAccountAddress } = useMangoAccount()
const [
usedTokens,
usedSerum3,
usedPerps,
usedPerpOo,
emptyTokens,
emptySerum3,
emptyPerps,
emptyPerpOo,
] = useMemo(() => {
if (!mangoAccountAddress || !mangoAccount)
return [[], [], [], [], [], [], [], []]
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount) return [[], [], [], [], [], []]
const { tokens, serum3, perps, perpOpenOrders } = mangoAccount
const usedTokens: TokenPosition[] = tokens.filter(
@ -77,13 +73,6 @@ export default function useMangoAccountAccounts() {
(p) => p.orderMarket !== 65535,
)
// const emptyPerpOo = [] // No instruction for closing perp oo
const emptyTokens = usedTokens.filter((t) => {
const bank = banks.find((b) => b.bank.tokenIndex === t.tokenIndex)
if (!bank) return false
return t.inUseCount && bank.balance === 0 && bank.borrowedAmount === 0
})
const emptyPerps = usedPerps.filter(
(p) =>
p.asksBaseLots.isZero() &&
@ -115,41 +104,26 @@ export default function useMangoAccountAccounts() {
usedSerum3,
usedPerps,
usedPerpOo,
emptyTokens,
emptySerum3,
emptyPerps,
[],
]
}, [mangoAccountAddress, mangoAccount])
}, [mangoAccountAddress])
const [totalTokens, totalSerum3, totalPerps, totalPerpOpenOrders] =
useMemo(() => {
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccountAddress || !mangoAccount) return [[], [], [], []]
if (!mangoAccount) return [[], [], [], []]
const { tokens, serum3, perps, perpOpenOrders } = mangoAccount
const totalTokens = tokens
const totalSerum3 = serum3
const totalPerps = perps
const totalPerpOpenOrders = perpOpenOrders
return [totalTokens, totalSerum3, totalPerps, totalPerpOpenOrders]
}, [mangoAccountAddress, mangoAccount])
// const [availableTokens, availableSerum3, availablePerps, availablePerpOo] =
// useMemo(() => {
// const [usedTokens, usedSerum3, usedPerps, usedPerpOo] =
// getUsedMangoAccountAccounts(mangoAccountAddress)
// const [totalTokens, totalSerum3, totalPerps, totalPerpOpenOrders] =
// getTotalMangoAccountAccounts(mangoAccountAddress)
// return [
// `${usedTokens}/${totalTokens}`,
// `${usedSerum3}/${totalSerum3}`,
// `${usedPerps}/${totalPerps}`,
// `${usedPerpOo}/${totalPerpOpenOrders}`,
// ]
// }, [mangoAccountAddress])
}, [mangoAccountAddress])
const isAccountFull = useMemo(() => {
if (!mangoAccountAddress) return true
const mangoAccount = mangoStore.getState().mangoAccount.current
if (!mangoAccount) return true
return getIsAccountSizeFull()
}, [mangoAccountAddress])
@ -158,10 +132,8 @@ export default function useMangoAccountAccounts() {
usedSerum3,
usedPerps,
usedPerpOo,
emptyTokens,
emptySerum3,
emptyPerps,
emptyPerpOo,
totalTokens,
totalSerum3,
totalPerps,

View File

@ -19,6 +19,7 @@
"chart-right": "Chart Right",
"chinese": "简体中文",
"chinese-traditional": "繁體中文",
"close-instructions": "Close Instructions",
"close-perp": "Close Perp Positions",
"close-perp-desc": "To close unused positions, close the position/s, cancel any open orders and settle any unsettled funding.",
"close-spot-oo": "Close Spot Open Orders",
@ -98,6 +99,9 @@
"swap-success": "Swap/Trade Success",
"swap-trade-size-selector": "Swap/Trade Size Selector",
"theme": "Theme",
"tooltip-close-collateral-token-instructions": "Close all spot open orders slots that use {{token}} and make sure you have enough free collateral to withdraw your total {{token}} balance.",
"tooltip-close-token-instructions": "Close all spot open orders slots that use {{token}}",
"tooltip-close-usdc-instructions": "Close all spot open orders slots that use USDC, close all perp position slots and make sure you have enough free collateral to withdraw your total USDC balance.",
"tooltip-hot-key-notional-size": "Set size as a USD value.",
"tooltip-hot-key-percentage-size": "Set size as a percentage of your max leverage.",
"tooltip-hot-key-price": "Set a price as a percentage change from the oracle price.",

View File

@ -19,6 +19,7 @@
"chart-right": "Chart Right",
"chinese": "简体中文",
"chinese-traditional": "繁體中文",
"close-instructions": "Close Instructions",
"close-perp": "Close Perp Positions",
"close-perp-desc": "To close unused positions, close the position/s, cancel any open orders and settle any unsettled funding.",
"close-spot-oo": "Close Spot Open Orders",
@ -98,6 +99,9 @@
"swap-success": "Swap/Trade Success",
"swap-trade-size-selector": "Swap/Trade Size Selector",
"theme": "Theme",
"tooltip-close-collateral-token-instructions": "Close all spot open orders slots that use {{token}} and make sure you have enough free collateral to withdraw your total {{token}} balance.",
"tooltip-close-token-instructions": "Close all spot open orders slots that use {{token}}",
"tooltip-close-usdc-instructions": "Close all spot open orders slots that use USDC, close all perp position slots and make sure you have enough free collateral to withdraw your total USDC balance.",
"tooltip-hot-key-notional-size": "Set size as a USD value.",
"tooltip-hot-key-percentage-size": "Set size as a percentage of your max leverage.",
"tooltip-hot-key-price": "Set a price as a percentage change from the oracle price.",

View File

@ -19,6 +19,7 @@
"chart-right": "Chart Right",
"chinese": "简体中文",
"chinese-traditional": "繁體中文",
"close-instructions": "Close Instructions",
"close-perp": "Close Perp Positions",
"close-perp-desc": "To close unused positions, close the position/s, cancel any open orders and settle any unsettled funding.",
"close-spot-oo": "Close Spot Open Orders",
@ -98,6 +99,9 @@
"swap-success": "Swap/Trade Success",
"swap-trade-size-selector": "Swap/Trade Size Selector",
"theme": "Theme",
"tooltip-close-collateral-token-instructions": "Close all spot open orders slots that use {{token}} and make sure you have enough free collateral to withdraw your total {{token}} balance.",
"tooltip-close-token-instructions": "Close all spot open orders slots that use {{token}}",
"tooltip-close-usdc-instructions": "Close all spot open orders slots that use USDC, close all perp position slots and make sure you have enough free collateral to withdraw your total USDC balance.",
"tooltip-hot-key-notional-size": "Set size as a USD value.",
"tooltip-hot-key-percentage-size": "Set size as a percentage of your max leverage.",
"tooltip-hot-key-price": "Set a price as a percentage change from the oracle price.",

View File

@ -19,6 +19,7 @@
"chart-right": "图表右",
"chinese": "简体中文",
"chinese-traditional": "繁體中文",
"close-instructions": "Close Instructions",
"close-perp": "Close Perp Positions",
"close-perp-desc": "To close unused positions, close the position/s, cancel any open orders and settle any unsettled funding.",
"close-spot-oo": "Close Spot Open Orders",
@ -98,6 +99,9 @@
"swap-success": "换币/交易成功",
"swap-trade-size-selector": "换币/交易大小选择器",
"theme": "模式",
"tooltip-close-collateral-token-instructions": "Close all spot open orders slots that use {{token}} and make sure you have enough free collateral to withdraw your total {{token}} balance.",
"tooltip-close-token-instructions": "Close all spot open orders slots that use {{token}}",
"tooltip-close-usdc-instructions": "Close all spot open orders slots that use USDC, close all perp position slots and make sure you have enough free collateral to withdraw your total USDC balance.",
"tooltip-hot-key-notional-size": "Set size as a USD value.",
"tooltip-hot-key-percentage-size": "Set size as a percentage of your max leverage.",
"tooltip-hot-key-price": "Set a price as a percentage change from the oracle price.",

View File

@ -20,6 +20,7 @@
"chinese": "简体中文",
"chinese-traditional": "繁體中文",
"close-perp": "Close Perp Positions",
"close-instructions": "Close Instructions",
"close-perp-desc": "To close unused positions, close the position/s, cancel any open orders and settle any unsettled funding.",
"close-spot-oo": "Close Spot Open Orders",
"close-spot-oo-desc": "To close unused markets, cancel your open orders and settle any unsettled balances.",
@ -98,6 +99,9 @@
"swap-success": "換幣/交易成功",
"swap-trade-size-selector": "換幣/交易大小選擇器",
"theme": "模式",
"tooltip-close-collateral-token-instructions": "Close all spot open orders slots that use {{token}} and make sure you have enough free collateral to withdraw your total {{token}} balance.",
"tooltip-close-token-instructions": "Close all spot open orders slots that use {{token}}",
"tooltip-close-usdc-instructions": "Close all spot open orders slots that use USDC, close all perp position slots and make sure you have enough free collateral to withdraw your total USDC balance.",
"tooltip-hot-key-notional-size": "Set size as a USD value.",
"tooltip-hot-key-percentage-size": "Set size as a percentage of your max leverage.",
"tooltip-hot-key-price": "Set a price as a percentage change from the oracle price.",