2021-04-14 23:16:36 -07:00
|
|
|
import { useEffect, useRef } from 'react'
|
2021-04-05 07:32:11 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
|
|
|
|
|
|
|
const byTimestamp = (a, b) => {
|
|
|
|
return (
|
|
|
|
new Date(b.loadTimestamp).getTime() - new Date(a.loadTimestamp).getTime()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatTradeHistory = (newTradeHistory) => {
|
|
|
|
return newTradeHistory
|
|
|
|
.flat()
|
|
|
|
.map((trade) => {
|
|
|
|
return {
|
|
|
|
...trade,
|
|
|
|
marketName: trade.marketName
|
|
|
|
? trade.marketName
|
|
|
|
: `${trade.baseCurrency}/${trade.quoteCurrency}`,
|
2021-04-14 23:16:36 -07:00
|
|
|
key: `${trade.orderId}-${trade.uuid}`,
|
2021-04-05 07:32:11 -07:00
|
|
|
liquidity: trade.maker || trade?.eventFlags?.maker ? 'Maker' : 'Taker',
|
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
|
|
|
value: trade.price * trade.size,
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.sort(byTimestamp)
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:40:26 -07:00
|
|
|
const useFills = () => {
|
2021-06-20 11:08:14 -07:00
|
|
|
const fillsRef = useRef(useMangoStore.getState().selectedMarket.fills)
|
2021-04-14 23:16:36 -07:00
|
|
|
const fills = fillsRef.current
|
|
|
|
useEffect(
|
|
|
|
() =>
|
2021-06-20 11:08:14 -07:00
|
|
|
useMangoStore.subscribe(
|
2021-04-14 23:16:36 -07:00
|
|
|
(fills) => (fillsRef.current = fills as []),
|
2021-06-20 11:08:14 -07:00
|
|
|
(state) => state.selectedMarket.fills
|
2021-04-14 23:16:36 -07:00
|
|
|
),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2021-04-12 21:40:26 -07:00
|
|
|
const marginAccount = useMangoStore((s) => s.selectedMarginAccount.current)
|
|
|
|
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
2021-06-20 11:08:14 -07:00
|
|
|
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
2021-04-12 21:40:26 -07:00
|
|
|
|
|
|
|
if (!marginAccount || !selectedMangoGroup) return null
|
|
|
|
|
2021-06-20 11:08:14 -07:00
|
|
|
const openOrdersAccount =
|
|
|
|
marginAccount.spotOpenOrdersAccounts[marketConfig.marketIndex]
|
2021-04-13 17:14:38 -07:00
|
|
|
if (!openOrdersAccount) return []
|
2021-06-20 11:08:14 -07:00
|
|
|
|
2021-04-12 21:40:26 -07:00
|
|
|
return fills
|
|
|
|
.filter((fill) => fill.openOrders.equals(openOrdersAccount.publicKey))
|
2021-06-20 11:08:14 -07:00
|
|
|
.map((fill) => ({ ...fill, marketName: marketConfig.name }))
|
2021-04-12 21:40:26 -07:00
|
|
|
}
|
|
|
|
|
2021-04-05 07:32:11 -07:00
|
|
|
export const useTradeHistory = () => {
|
2021-04-12 21:40:26 -07:00
|
|
|
const eventQueueFills = useFills()
|
2021-04-14 15:46:36 -07:00
|
|
|
const tradeHistory = useMangoStore((s) => s.tradeHistory)
|
2021-04-05 07:32:11 -07:00
|
|
|
|
2021-04-14 23:16:36 -07:00
|
|
|
const allTrades = []
|
|
|
|
if (eventQueueFills && eventQueueFills.length > 0) {
|
|
|
|
const newFills = eventQueueFills.filter(
|
|
|
|
(fill) =>
|
|
|
|
!tradeHistory.flat().find((t) => t.orderId === fill.orderId.toString())
|
|
|
|
)
|
|
|
|
const newTradeHistory = [...newFills, ...tradeHistory]
|
|
|
|
if (newFills.length > 0 && newTradeHistory.length !== allTrades.length) {
|
|
|
|
return formatTradeHistory(newTradeHistory)
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
2021-04-14 23:16:36 -07:00
|
|
|
}
|
2021-04-05 07:32:11 -07:00
|
|
|
|
2021-04-15 10:20:24 -07:00
|
|
|
return formatTradeHistory(tradeHistory)
|
2021-04-05 07:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useTradeHistory
|