diff --git a/components/UiLock.tsx b/components/UiLock.tsx
index d356192a..6fb73172 100644
--- a/components/UiLock.tsx
+++ b/components/UiLock.tsx
@@ -15,12 +15,12 @@ const UiLock = ({ className = '' }) => {
diff --git a/hooks/useConnection.tsx b/hooks/useConnection.tsx
index 4b872d67..80116683 100644
--- a/hooks/useConnection.tsx
+++ b/hooks/useConnection.tsx
@@ -4,9 +4,7 @@ import { IDS } from '@blockworks-foundation/mango-client'
import useMangoStore from '../stores/useMangoStore'
const useConnection = () => {
- // console.log('loading useConnection')
-
- const setSolanaStore = useMangoStore((s) => s.set)
+ const setMangoStore = useMangoStore((s) => s.set)
const { cluster, current: connection, endpoint } = useMangoStore(
(s) => s.connection
)
@@ -16,17 +14,17 @@ const useConnection = () => {
])
useEffect(() => {
- // @ts-ignore
- if (connection && endpoint === connection._rpcEndpoint) return
+ if (connection && endpoint === connection['_rpcEndpoint']) return
console.log('setting new connection')
const newConnection = new Connection(endpoint, 'recent')
- setSolanaStore((state) => {
+ setMangoStore((state) => {
state.connection.current = newConnection
})
}, [endpoint])
useEffect(() => {
+ if (connection && endpoint === connection['_rpcEndpoint']) return
const id = connection.onAccountChange(new Account().publicKey, () => {})
return () => {
connection.removeAccountChangeListener(id)
@@ -34,6 +32,7 @@ const useConnection = () => {
}, [endpoint])
useEffect(() => {
+ if (connection && endpoint === connection['_rpcEndpoint']) return
const id = connection.onSlotChange(() => null)
return () => {
connection.removeSlotChangeListener(id)
diff --git a/hooks/useHydrateStore.tsx b/hooks/useHydrateStore.tsx
index ced1708d..5246eef5 100644
--- a/hooks/useHydrateStore.tsx
+++ b/hooks/useHydrateStore.tsx
@@ -18,9 +18,14 @@ const useHydrateStore = () => {
const setSerumStore = useSerumStore((s) => s.set)
const selectedMarketAddress = useMangoStore(marketAddressSelector)
const marketsForSelectedMangoGroup = useMangoStore(mangoGroupMarketsSelector)
+ const actions = useMangoStore((s) => s.actions)
const { connection, dexProgramId } = useConnection()
const { marketList } = useMarketList()
+ useEffect(() => {
+ actions.fetchMangoGroup()
+ }, [actions])
+
// load selected market
useEffect(() => {
Market.load(
@@ -76,7 +81,7 @@ const useHydrateStore = () => {
})
}, [marketList])
- // hydrate orderbook for all markets in mango group
+ // hydrate orderbook with all markets in mango group
useEffect(() => {
const subscriptionIds = Object.entries(marketsForSelectedMangoGroup).map(
([, market]) => {
@@ -131,6 +136,7 @@ const useHydrateStore = () => {
}
}, [marketsForSelectedMangoGroup])
+ // fetch filled trades for selected market
useInterval(() => {
async function fetchFills() {
const market = useMangoStore.getState().market.current
diff --git a/hooks/useMarginAccount.tsx b/hooks/useMarginAccount.tsx
index 328114ba..7de96ee8 100644
--- a/hooks/useMarginAccount.tsx
+++ b/hooks/useMarginAccount.tsx
@@ -23,12 +23,6 @@ const useMarginAccount = () => {
}
}, [connected, actions])
- useEffect(() => {
- if (selectedMarginAccount) {
- actions.fetchTradeHistory()
- }
- }, [selectedMarginAccount])
-
useInterval(() => {
if (connected) {
actions.fetchMarginAccounts()
diff --git a/hooks/useTradeHistory.tsx b/hooks/useTradeHistory.tsx
index de973f66..df4911e5 100644
--- a/hooks/useTradeHistory.tsx
+++ b/hooks/useTradeHistory.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect } from 'react'
+import { useEffect, useRef } from 'react'
import useMangoStore from '../stores/useMangoStore'
import useSerumStore from '../stores/useSerumStore'
import useMarket from './useMarket'
@@ -19,7 +19,7 @@ const formatTradeHistory = (newTradeHistory) => {
marketName: trade.marketName
? trade.marketName
: `${trade.baseCurrency}/${trade.quoteCurrency}`,
- key: `${trade.orderId}${trade.side}${trade.uuid}`,
+ key: `${trade.orderId}-${trade.uuid}`,
liquidity: trade.maker || trade?.eventFlags?.maker ? 'Maker' : 'Taker',
}
})
@@ -27,7 +27,17 @@ const formatTradeHistory = (newTradeHistory) => {
}
const useFills = () => {
- const fills = useSerumStore((s) => s.fills)
+ const fillsRef = useRef(useSerumStore.getState().fills)
+ const fills = fillsRef.current
+ useEffect(
+ () =>
+ useSerumStore.subscribe(
+ (fills) => (fillsRef.current = fills as []),
+ (state) => state.fills
+ ),
+ []
+ )
+
const { market, marketName } = useMarket()
const marginAccount = useMangoStore((s) => s.selectedMarginAccount.current)
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
@@ -44,7 +54,6 @@ const useFills = () => {
export const useTradeHistory = () => {
const eventQueueFills = useFills()
- const [allTrades, setAllTrades] = useState
([])
const tradeHistory = useMangoStore((s) => s.tradeHistory)
const marginAccount = useMangoStore((s) => s.selectedMarginAccount.current)
const actions = useMangoStore((s) => s.actions)
@@ -55,22 +64,19 @@ export const useTradeHistory = () => {
}
}, 12000)
- useEffect(() => {
- if (eventQueueFills && eventQueueFills.length > 0) {
- const newFills = eventQueueFills.filter(
- (fill) =>
- !tradeHistory.find((t) => t.orderId === fill.orderId.toString())
- )
- const newTradeHistory = [...newFills, ...tradeHistory]
- if (newFills.length > 0 && newTradeHistory.length !== allTrades.length) {
- const formattedTradeHistory = formatTradeHistory(newTradeHistory)
-
- setAllTrades(formattedTradeHistory)
- }
+ 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)
}
- }, [tradeHistory, eventQueueFills])
+ }
- return { tradeHistory: allTrades }
+ return tradeHistory.flat()
}
export default useTradeHistory
diff --git a/pages/stats.tsx b/pages/stats.tsx
index 345773b4..5241db3e 100644
--- a/pages/stats.tsx
+++ b/pages/stats.tsx
@@ -191,7 +191,7 @@ export default function StatsPage() {
-
+
Mango Stats
@@ -253,7 +253,7 @@ export default function StatsPage() {
{selectedAsset ? (
-
+
Historical