2021-04-07 14:49:37 -07:00
|
|
|
import { Balances } from '../@types/types'
|
|
|
|
import { nativeToUi } from '@blockworks-foundation/mango-client'
|
|
|
|
import useMarketList from './useMarketList'
|
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-04-29 14:27:13 -07:00
|
|
|
import {
|
2021-06-23 08:32:33 -07:00
|
|
|
// displayBorrowsForMangoAccount,
|
|
|
|
// displayDepositsForMangoAccount,
|
2021-04-29 14:27:13 -07:00
|
|
|
floorToDecimal,
|
|
|
|
} from '../utils'
|
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
|
|
|
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'
|
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-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-04-29 14:27:13 -07:00
|
|
|
|
2021-06-22 20:32:10 -07:00
|
|
|
return amount.toString()
|
|
|
|
// return floorToDecimal(
|
|
|
|
// nativeToUi(amount, mangoGroup.tokens[currencyIndex].decimals),
|
|
|
|
// mangoGroup.tokens[currencyIndex].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
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
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}`,
|
|
|
|
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(),
|
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-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-09 08:52:32 -07:00
|
|
|
|
2021-06-22 20:32:10 -07:00
|
|
|
return baseBalances
|
2021-04-07 14:49:37 -07:00
|
|
|
}
|