show spot balances

This commit is contained in:
Tyler Shipe 2021-06-22 23:32:10 -04:00
parent 2a1e13a1b0
commit 0d64771bc2
3 changed files with 102 additions and 95 deletions

View File

@ -24,14 +24,14 @@ const BalancesTable = () => {
const wallet = useMangoStore.getState().wallet.current
try {
await settleAll(
connection,
new PublicKey(programId),
mangoGroup,
marginAccount,
markets,
wallet
)
// await settleAll(
// connection,
// new PublicKey(programId),
// mangoGroup,
// marginAccount,
// markets,
// wallet
// )
await sleep(250)
actions.fetchMarginAccounts()
} catch (e) {

View File

@ -1,12 +1,11 @@
import { useState } from 'react'
import FloatingElement from './FloatingElement'
import OpenOrdersTable from './OpenOrdersTable'
// import BalancesTable from './BalancesTable'
import BalancesTable from './BalancesTable'
import TradeHistoryTable from './TradeHistoryTable'
// import FeeDiscountsTable from './FeeDiscountsTable'
// const TABS = ['Open Orders', 'Balances', 'Trade History', 'Fee Discounts']
const TABS = ['Open Orders', 'Trade History']
const TABS = ['Open Orders', 'Balances', 'Trade History' /*'Fee Discounts'*/]
const UserInfoTabs = ({ activeTab, setActiveTab }) => {
const handleTabChange = (tabName) => {
@ -61,8 +60,8 @@ const TabContent = ({ activeTab }) => {
switch (activeTab) {
case 'Orders':
return <OpenOrdersTable />
// case 'Balances':
// return <BalancesTable />
case 'Balances':
return <BalancesTable />
case 'Trade History':
return <TradeHistoryTable />
// case 'Fee Discounts':

View File

@ -3,42 +3,33 @@ import { nativeToUi } from '@blockworks-foundation/mango-client'
import useMarketList from './useMarketList'
import useMangoStore from '../stores/useMangoStore'
import {
displayBorrowsForMarginAccount,
displayDepositsForMarginAccount,
// displayBorrowsForMarginAccount,
// displayDepositsForMarginAccount,
floorToDecimal,
} from '../utils'
import useAllMarkets from './useAllMarkets'
import { sumBy } from 'lodash'
import { QUOTE_INDEX } from '@blockworks-foundation/mango-client/lib/src/MerpsGroup'
import { I80F48 } from '@blockworks-foundation/mango-client/lib/src/fixednum'
export function useBalances(): Balances[] {
const balances = []
const markets = useAllMarkets()
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const marginAccount = useMangoStore((s) => s.selectedMarginAccount.current)
const { symbols } = useMarketList()
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
for (const { market, baseCurrency, quoteCurrency } of markets) {
if (!marginAccount || !mangoGroup || !market) {
for (const {
marketIndex,
baseSymbol,
name,
} of mangoGroupConfig.spotMarkets) {
if (!marginAccount || !mangoGroup) {
return []
}
const marketIndex = mangoGroup.getMarketIndex(market)
const openOrders: any = marginAccount.openOrdersAccounts[marketIndex]
const baseCurrencyIndex = Object.entries(symbols).findIndex(
(x) => x[0] === baseCurrency
)
const quoteCurrencyIndex = Object.entries(symbols).findIndex(
(x) => x[0] === quoteCurrency
)
if (
baseCurrency === 'UNKNOWN' ||
quoteCurrency === 'UNKNOWN' ||
!baseCurrency ||
!quoteCurrency
) {
return []
}
const openOrders: any = marginAccount.spotOpenOrdersAccounts[marketIndex]
const quoteCurrencyIndex = QUOTE_INDEX
const nativeBaseFree = openOrders?.baseTokenFree || 0
const nativeQuoteFree =
@ -55,63 +46,81 @@ export function useBalances(): Balances[] {
const tokenIndex = marketIndex
const net = (locked, currencyIndex) => {
const amount =
marginAccount.getNativeDeposit(mangoGroup, currencyIndex) +
locked -
marginAccount.getNativeBorrow(mangoGroup, currencyIndex)
const amount = marginAccount
.getNativeDeposit(
mangoCache.rootBankCache[currencyIndex],
currencyIndex
)
.add(
I80F48.fromNumber(locked).sub(
marginAccount.getNativeBorrow(
mangoCache.rootBankCache[currencyIndex],
currencyIndex
)
)
)
return floorToDecimal(
nativeToUi(amount, mangoGroup.mintDecimals[currencyIndex]),
mangoGroup.mintDecimals[currencyIndex]
)
return amount.toString()
// return floorToDecimal(
// nativeToUi(amount, mangoGroup.tokens[currencyIndex].decimals),
// mangoGroup.tokens[currencyIndex].decimals
// )
}
const marketPair = [
{
market,
key: `${baseCurrency}${quoteCurrency}${baseCurrency}`,
coin: baseCurrency,
marginDeposits: displayDepositsForMarginAccount(
marginAccount,
mangoGroup,
baseCurrencyIndex
),
borrows: displayBorrowsForMarginAccount(
marginAccount,
mangoGroup,
baseCurrencyIndex
),
market: null,
key: `${baseSymbol}${name}`,
coin: baseSymbol,
marginDeposits: marginAccount
.getUiDeposit(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
)
.toString(),
borrows: marginAccount
.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
tokenIndex
)
.toString(),
orders: nativeToUi(
nativeBaseLocked,
mangoGroup.mintDecimals[tokenIndex]
mangoGroup.tokens[tokenIndex].decimals
),
unsettled: nativeToUi(
nativeBaseFree,
mangoGroup.mintDecimals[tokenIndex]
mangoGroup.tokens[tokenIndex].decimals
),
net: net(nativeBaseLocked, tokenIndex),
},
{
market,
key: `${quoteCurrency}${baseCurrency}${quoteCurrency}`,
coin: quoteCurrency,
marginDeposits: displayDepositsForMarginAccount(
marginAccount,
mangoGroup,
quoteCurrencyIndex
),
borrows: displayBorrowsForMarginAccount(
marginAccount,
mangoGroup,
quoteCurrencyIndex
),
market: null,
key: `${name}`,
coin: mangoGroupConfig.quoteSymbol,
marginDeposits: marginAccount
.getUiDeposit(
mangoCache.rootBankCache[quoteCurrencyIndex],
mangoGroup,
quoteCurrencyIndex
)
.toString(),
borrows: marginAccount
.getUiBorrow(
mangoCache.rootBankCache[tokenIndex],
mangoGroup,
quoteCurrencyIndex
)
.toString(),
orders: nativeToUi(
nativeQuoteLocked,
mangoGroup.mintDecimals[quoteCurrencyIndex]
mangoGroup.tokens[quoteCurrencyIndex].decimals
),
unsettled: nativeToUi(
nativeQuoteFree,
mangoGroup.mintDecimals[quoteCurrencyIndex]
mangoGroup.tokens[quoteCurrencyIndex].decimals
),
net: net(nativeQuoteLocked, quoteCurrencyIndex),
},
@ -120,26 +129,25 @@ export function useBalances(): Balances[] {
}
const baseBalances = balances.map((b) => b[0])
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
const quoteCurrencyIndex = Object.entries(symbols).findIndex(
(x) => x[0] === quoteMeta.coin
)
// 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.mintDecimals[quoteCurrencyIndex]),
},
])
// 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),
// },
// ])
return baseBalances
}