From 77c3bdd3597b33f720e89350e651ab3684a2c659 Mon Sep 17 00:00:00 2001 From: saml33 Date: Tue, 12 Sep 2023 23:32:09 +1000 Subject: [PATCH] add close instructions --- components/settings/AccountSettings.tsx | 33 ++++++++++++++++--- components/trade/OpenOrders.tsx | 2 +- hooks/useMangoAccountAccounts.ts | 44 +++++-------------------- public/locales/en/settings.json | 4 +++ public/locales/es/settings.json | 4 +++ public/locales/ru/settings.json | 4 +++ public/locales/zh/settings.json | 4 +++ public/locales/zh_tw/settings.json | 4 +++ 8 files changed, 58 insertions(+), 41 deletions(-) diff --git a/components/settings/AccountSettings.tsx b/components/settings/AccountSettings.tsx index 9f0147f8..f519068a 100644 --- a/components/settings/AccountSettings.tsx +++ b/components/settings/AccountSettings.tsx @@ -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 (
@@ -255,7 +259,28 @@ const AccountSettings = () => { > {t('close')} - ) : null} + ) : ( + +

+ {t('settings:close-instructions')} +

+
+ )}
) }) diff --git a/components/trade/OpenOrders.tsx b/components/trade/OpenOrders.tsx index dc19d597..03c0ab29 100644 --- a/components/trade/OpenOrders.tsx +++ b/components/trade/OpenOrders.tsx @@ -212,7 +212,7 @@ const OpenOrders = () => { o.perpMarketIndex, o.orderId, ) - actions.fetchOpenOrders() + actions.fetchOpenOrders(true) notify({ type: 'success', title: 'Transaction successful', diff --git a/hooks/useMangoAccountAccounts.ts b/hooks/useMangoAccountAccounts.ts index 22301a24..eacef41c 100644 --- a/hooks/useMangoAccountAccounts.ts +++ b/hooks/useMangoAccountAccounts.ts @@ -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, diff --git a/public/locales/en/settings.json b/public/locales/en/settings.json index 5deb6613..3f34fad4 100644 --- a/public/locales/en/settings.json +++ b/public/locales/en/settings.json @@ -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.", diff --git a/public/locales/es/settings.json b/public/locales/es/settings.json index 293b5189..63cc77ae 100644 --- a/public/locales/es/settings.json +++ b/public/locales/es/settings.json @@ -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.", diff --git a/public/locales/ru/settings.json b/public/locales/ru/settings.json index 293b5189..63cc77ae 100644 --- a/public/locales/ru/settings.json +++ b/public/locales/ru/settings.json @@ -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.", diff --git a/public/locales/zh/settings.json b/public/locales/zh/settings.json index f9e0215e..d2355f3f 100644 --- a/public/locales/zh/settings.json +++ b/public/locales/zh/settings.json @@ -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.", diff --git a/public/locales/zh_tw/settings.json b/public/locales/zh_tw/settings.json index 7bb1ebbf..4719eccd 100644 --- a/public/locales/zh_tw/settings.json +++ b/public/locales/zh_tw/settings.json @@ -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.",