mango-ui-v3/hooks/useBalances.tsx

154 lines
4.6 KiB
TypeScript
Raw Normal View History

import { Balances } from '../@types/types'
import { nativeToUi } from '@blockworks-foundation/mango-client'
import useMarketList from './useMarketList'
import useMangoStore from '../stores/useMangoStore'
import {
2021-06-23 08:32:33 -07:00
// displayBorrowsForMangoAccount,
// displayDepositsForMangoAccount,
floorToDecimal,
} from '../utils'
import useAllMarkets from './useAllMarkets'
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-06-12 13:57:09 -07:00
const net = (locked, currencyIndex) => {
2021-06-23 08:32:33 -07:00
const amount = mangoAccount
2021-06-22 20:32:10 -07:00
.getNativeDeposit(
mangoCache.rootBankCache[currencyIndex],
currencyIndex
)
.add(
I80F48.fromNumber(locked).sub(
2021-06-23 08:32:33 -07:00
mangoAccount.getNativeBorrow(
2021-06-22 20:32:10 -07:00
mangoCache.rootBankCache[currencyIndex],
currencyIndex
)
)
)
2021-06-22 20:32:10 -07:00
return amount.toString()
// return floorToDecimal(
// nativeToUi(amount, mangoGroup.tokens[currencyIndex].decimals),
// mangoGroup.tokens[currencyIndex].decimals
// )
}
const marketPair = [
{
2021-06-22 20:32:10 -07:00
market: null,
key: `${baseSymbol}${name}`,
coin: baseSymbol,
2021-06-23 08:32:33 -07:00
marginDeposits: mangoAccount
2021-06-22 20:32:10 -07:00
.getUiDeposit(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
)
.toString(),
2021-06-23 08:32:33 -07:00
borrows: mangoAccount
2021-06-22 20:32:10 -07:00
.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
)
.toString(),
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}`,
coin: mangoGroupConfig.quoteSymbol,
2021-06-23 08:32:33 -07:00
marginDeposits: mangoAccount
2021-06-22 20:32:10 -07:00
.getUiDeposit(
mangoCache.rootBankCache[quoteCurrencyIndex],
mangoGroup,
quoteCurrencyIndex
)
.toString(),
2021-06-23 08:32:33 -07:00
borrows: mangoAccount
2021-06-22 20:32:10 -07:00
.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
quoteCurrencyIndex
)
.toString(),
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-06-22 20:32:10 -07:00
// const quoteBalances = balances.map((b) => b[1])
// const quoteMeta = quoteBalances[0]
// const quoteInOrders = sumBy(quoteBalances, 'orders')
// const unsettled = sumBy(quoteBalances, 'unsettled')
// const net =
// quoteMeta.marginDeposits + unsettled - quoteMeta.borrows - quoteInOrders
// return baseBalances.concat([
// {
// market: null,
// key: `${quoteMeta.coin}${quoteMeta.coin}`,
// coin: quoteMeta.coin,
// marginDeposits: quoteMeta.marginDeposits,
// borrows: quoteMeta.borrows,
// orders: quoteInOrders,
// unsettled,
// net: floorToDecimal(net, mangoGroup.tokens[QUOTE_INDEX].decimals),
// },
// ])
2021-06-22 20:32:10 -07:00
return baseBalances
}