mango-ui-v3/hooks/useBalances.tsx

138 lines
4.0 KiB
TypeScript
Raw Normal View History

import { Balances } from '../@types/types'
import { nativeToUi } from '@blockworks-foundation/mango-client'
import useMangoStore from '../stores/useMangoStore'
2021-06-12 11:04:30 -07:00
import { sumBy } from 'lodash'
2021-06-23 08:32:33 -07:00
import { QUOTE_INDEX } from '@blockworks-foundation/mango-client/lib/src/MangoGroup'
2021-06-22 20:32:10 -07:00
import { I80F48 } from '@blockworks-foundation/mango-client/lib/src/fixednum'
export function useBalances(): Balances[] {
2021-06-12 10:46:06 -07:00
const balances = []
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-06-22 20:32:10 -07:00
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
2021-06-22 20:32:10 -07:00
for (const {
marketIndex,
baseSymbol,
name,
} of mangoGroupConfig.spotMarkets) {
2021-06-23 08:32:33 -07:00
if (!mangoAccount || !mangoGroup) {
return []
}
2021-06-23 08:32:33 -07:00
const openOrders: any = mangoAccount.spotOpenOrdersAccounts[marketIndex]
2021-06-22 20:32:10 -07:00
const quoteCurrencyIndex = QUOTE_INDEX
const nativeBaseFree = openOrders?.baseTokenFree || 0
2021-06-12 10:46:06 -07:00
const nativeQuoteFree =
(openOrders?.quoteTokenFree || 0) +
(openOrders?.['referrerRebatesAccrued'].toNumber() || 0)
const nativeBaseLocked = openOrders
2021-06-12 10:46:06 -07:00
? openOrders.baseTokenTotal - openOrders?.baseTokenFree
: 0
2021-06-12 10:46:06 -07:00
const nativeQuoteLocked = openOrders
? openOrders?.quoteTokenTotal - (openOrders?.quoteTokenFree || 0)
: 0
const tokenIndex = marketIndex
2021-07-07 10:27:11 -07:00
const net = (nativeBaseLocked, tokenIndex) => {
2021-06-23 08:32:33 -07:00
const amount = mangoAccount
2021-07-06 21:34:21 -07:00
.getUiDeposit(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
2021-06-22 20:32:10 -07:00
)
.add(
2021-07-07 10:27:11 -07:00
I80F48.fromNumber(nativeBaseLocked).sub(
2021-07-06 21:34:21 -07:00
mangoAccount.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
2021-06-22 20:32:10 -07:00
)
)
)
2021-07-07 13:38:04 -07:00
return amount
}
const marketPair = [
{
2021-06-22 20:32:10 -07:00
market: null,
key: `${baseSymbol}${name}`,
2021-07-05 08:03:57 -07:00
symbol: baseSymbol,
marginDeposits: mangoAccount.getUiDeposit(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
),
borrows: mangoAccount.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
),
orders: nativeToUi(
nativeBaseLocked,
2021-06-22 20:32:10 -07:00
mangoGroup.tokens[tokenIndex].decimals
),
unsettled: nativeToUi(
2021-06-12 10:46:06 -07:00
nativeBaseFree,
2021-06-22 20:32:10 -07:00
mangoGroup.tokens[tokenIndex].decimals
),
net: net(nativeBaseLocked, tokenIndex),
},
{
2021-06-22 20:32:10 -07:00
market: null,
key: `${name}`,
2021-07-05 08:03:57 -07:00
symbol: mangoGroupConfig.quoteSymbol,
marginDeposits: mangoAccount.getUiDeposit(
mangoCache.rootBankCache[quoteCurrencyIndex],
mangoGroup,
quoteCurrencyIndex
),
borrows: mangoAccount.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
quoteCurrencyIndex
),
orders: nativeToUi(
nativeQuoteLocked,
2021-06-22 20:32:10 -07:00
mangoGroup.tokens[quoteCurrencyIndex].decimals
),
unsettled: nativeToUi(
2021-06-12 10:46:06 -07:00
nativeQuoteFree,
2021-06-22 20:32:10 -07:00
mangoGroup.tokens[quoteCurrencyIndex].decimals
),
net: net(nativeQuoteLocked, quoteCurrencyIndex),
},
]
2021-06-12 10:46:06 -07:00
balances.push(marketPair)
}
2021-06-12 10:46:06 -07:00
const baseBalances = balances.map((b) => b[0])
2021-07-05 08:03:57 -07:00
const quoteBalances = balances.map((b) => b[1])
const quoteMeta = quoteBalances[0]
const quoteInOrders = sumBy(quoteBalances, 'orders')
const unsettled = sumBy(quoteBalances, 'unsettled')
2021-07-06 21:34:21 -07:00
const net: I80F48 = quoteMeta.marginDeposits
2021-07-05 08:03:57 -07:00
.add(I80F48.fromNumber(unsettled))
.sub(quoteMeta.borrows)
.sub(I80F48.fromNumber(quoteInOrders))
2021-06-22 20:32:10 -07:00
2021-07-05 08:03:57 -07:00
return baseBalances.concat([
{
market: null,
key: `${quoteMeta.symbol}${quoteMeta.symbol}`,
symbol: quoteMeta.symbol,
marginDeposits: quoteMeta.marginDeposits,
borrows: quoteMeta.borrows,
orders: quoteInOrders,
unsettled,
2021-07-07 13:38:04 -07:00
net,
2021-07-05 08:03:57 -07:00
},
])
}