2021-04-07 14:49:37 -07:00
|
|
|
import { Balances } from '../@types/types'
|
2021-08-03 16:32:24 -07:00
|
|
|
import {
|
|
|
|
nativeI80F48ToUi,
|
|
|
|
nativeToUi,
|
|
|
|
QUOTE_INDEX,
|
|
|
|
} from '@blockworks-foundation/mango-client'
|
2021-04-07 14:49:37 -07:00
|
|
|
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'
|
2021-04-07 14:49:37 -07:00
|
|
|
|
|
|
|
export function useBalances(): Balances[] {
|
2021-06-12 10:46:06 -07:00
|
|
|
const balances = []
|
2021-04-07 14:49:37 -07:00
|
|
|
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-04-07 14:49:37 -07:00
|
|
|
|
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) {
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
return []
|
|
|
|
}
|
2021-04-07 14:49:37 -07:00
|
|
|
|
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-04-07 14:49:37 -07:00
|
|
|
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const nativeBaseFree = openOrders?.baseTokenFree || 0
|
2021-06-12 10:46:06 -07:00
|
|
|
const nativeQuoteFree =
|
|
|
|
(openOrders?.quoteTokenFree || 0) +
|
|
|
|
(openOrders?.['referrerRebatesAccrued'].toNumber() || 0)
|
2021-04-07 14:49:37 -07:00
|
|
|
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const nativeBaseLocked = openOrders
|
2021-06-12 10:46:06 -07:00
|
|
|
? openOrders.baseTokenTotal - openOrders?.baseTokenFree
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
: 0
|
2021-06-12 10:46:06 -07:00
|
|
|
const nativeQuoteLocked = openOrders
|
2021-06-12 14:55:27 -07:00
|
|
|
? openOrders?.quoteTokenTotal - (openOrders?.quoteTokenFree || 0)
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
: 0
|
2021-04-07 14:49:37 -07:00
|
|
|
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
const tokenIndex = marketIndex
|
2021-04-07 14:49:37 -07:00
|
|
|
|
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-04-29 14:27:13 -07:00
|
|
|
|
2021-07-07 13:38:04 -07:00
|
|
|
return amount
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
),
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
orders: nativeToUi(
|
|
|
|
nativeBaseLocked,
|
2021-06-22 20:32:10 -07:00
|
|
|
mangoGroup.tokens[tokenIndex].decimals
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
),
|
|
|
|
unsettled: nativeToUi(
|
2021-06-12 10:46:06 -07:00
|
|
|
nativeBaseFree,
|
2021-06-22 20:32:10 -07:00
|
|
|
mangoGroup.tokens[tokenIndex].decimals
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
),
|
|
|
|
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
|
|
|
|
),
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
orders: nativeToUi(
|
|
|
|
nativeQuoteLocked,
|
2021-06-22 20:32:10 -07:00
|
|
|
mangoGroup.tokens[quoteCurrencyIndex].decimals
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
),
|
|
|
|
unsettled: nativeToUi(
|
2021-06-12 10:46:06 -07:00
|
|
|
nativeQuoteFree,
|
2021-06-22 20:32:10 -07:00
|
|
|
mangoGroup.tokens[quoteCurrencyIndex].decimals
|
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
|
|
|
),
|
|
|
|
net: net(nativeQuoteLocked, quoteCurrencyIndex),
|
|
|
|
},
|
|
|
|
]
|
2021-06-12 10:46:06 -07:00
|
|
|
balances.push(marketPair)
|
2021-04-07 14:49:37 -07:00
|
|
|
}
|
|
|
|
|
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-07-08 07:16:39 -07:00
|
|
|
const net: I80F48 = quoteMeta.marginDeposits
|
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-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,
|
|
|
|
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
|
|
|
},
|
2021-07-24 13:15:27 -07:00
|
|
|
].concat(baseBalances)
|
2021-04-07 14:49:37 -07:00
|
|
|
}
|