mango-ui-v3/hooks/useBalances.tsx

166 lines
4.7 KiB
TypeScript
Raw Normal View History

import { Balances } from '../@types/types'
2021-08-03 16:32:24 -07:00
import {
2021-09-03 05:11:21 -07:00
getTokenBySymbol,
2021-08-03 16:32:24 -07:00
nativeI80F48ToUi,
nativeToUi,
QUOTE_INDEX,
} from '@blockworks-foundation/mango-client'
import useMangoStore from '../stores/useMangoStore'
2021-06-12 11:04:30 -07:00
import { sumBy } from 'lodash'
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
2021-08-14 14:14:43 -07:00
let nativeBaseFree = 0
let nativeQuoteFree = 0
let nativeBaseLocked = 0
let nativeQuoteLocked = 0
if (openOrders) {
nativeBaseFree = openOrders.baseTokenFree.toNumber()
nativeQuoteFree = openOrders.quoteTokenFree
.add(openOrders['referrerRebatesAccrued'])
.toNumber()
nativeBaseLocked = openOrders.baseTokenTotal
.sub(openOrders.baseTokenFree)
.toNumber()
nativeQuoteLocked = openOrders.quoteTokenTotal
.sub(openOrders.quoteTokenFree)
.toNumber()
}
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-08-03 16:32:24 -07:00
nativeI80F48ToUi(
I80F48.fromNumber(nativeBaseLocked),
mangoGroup.tokens[tokenIndex].decimals
).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
}
2021-09-03 05:11:21 -07:00
const value = (nativeBaseLocked, tokenIndex) => {
const amount = mangoGroup
.getPrice(tokenIndex, mangoCache)
.mul(net(nativeBaseLocked, tokenIndex))
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,
2021-08-18 13:43:51 -07:00
deposits: mangoAccount.getUiDeposit(
2021-07-05 08:03:57 -07:00
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-09-03 05:11:21 -07:00
value: value(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,
2021-08-18 13:43:51 -07:00
deposits: mangoAccount.getUiDeposit(
2021-07-05 08:03:57 -07:00
mangoCache.rootBankCache[quoteCurrencyIndex],
mangoGroup,
quoteCurrencyIndex
),
borrows: mangoAccount.getUiBorrow(
2021-08-24 12:16:40 -07:00
mangoCache.rootBankCache[quoteCurrencyIndex],
2021-07-05 08:03:57 -07:00
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-09-03 05:11:21 -07:00
value: value(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
2021-08-18 13:43:51 -07:00
const net: I80F48 = quoteMeta.deposits
2021-07-05 08:03:57 -07:00
.add(I80F48.fromNumber(unsettled))
.sub(quoteMeta.borrows)
2021-08-03 16:32:24 -07:00
.add(I80F48.fromNumber(quoteInOrders))
2021-06-22 20:32:10 -07:00
2021-09-03 05:11:21 -07:00
const token = getTokenBySymbol(mangoGroupConfig, quoteMeta.symbol)
const tokenIndex = mangoGroup.getTokenIndex(token.mintKey)
const value = net.mul(mangoGroup.getPrice(tokenIndex, mangoCache))
2021-07-24 13:15:27 -07:00
return [
2021-07-05 08:03:57 -07:00
{
market: null,
key: `${quoteMeta.symbol}${quoteMeta.symbol}`,
symbol: quoteMeta.symbol,
2021-08-18 13:43:51 -07:00
deposits: quoteMeta.deposits,
2021-07-05 08:03:57 -07:00
borrows: quoteMeta.borrows,
orders: quoteInOrders,
unsettled,
2021-07-07 13:38:04 -07:00
net,
2021-09-03 05:11:21 -07:00
value,
2021-07-05 08:03:57 -07:00
},
2021-07-24 13:15:27 -07:00
].concat(baseBalances)
}