mango-ui-v3/hooks/useTradeHistory.tsx

135 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-07-24 11:12:52 -07:00
import {
2021-08-13 08:05:49 -07:00
getMarketByPublicKey,
2021-07-24 11:12:52 -07:00
MangoGroup,
} from '@blockworks-foundation/mango-client'
import { PublicKey } from '@solana/web3.js'
import useMangoStore from '../stores/useMangoStore'
const byTimestamp = (a, b) => {
return (
2021-08-13 13:02:12 -07:00
new Date(b.loadTimestamp || b.timestamp * 1000).getTime() -
new Date(a.loadTimestamp || a.timestamp * 1000).getTime()
)
}
const reverseSide = (side) => (side === 'buy' ? 'sell' : 'buy')
2021-08-13 08:05:49 -07:00
function getPerpMarketName(event) {
const mangoGroupConfig = useMangoStore.getState().selectedMangoGroup.config
let marketName
if (!event.marketName) {
const marketInfo = getMarketByPublicKey(mangoGroupConfig, event.address)
marketName = marketInfo.name
}
return event.marketName || marketName
}
2021-07-24 11:12:52 -07:00
const parsedPerpEvent = (
mangoGroup: MangoGroup,
mangoAccountPk: PublicKey,
event
) => {
2021-08-13 08:05:49 -07:00
const maker = event.maker.toString() === mangoAccountPk.toString()
2021-07-24 11:12:52 -07:00
const orderId = maker ? event.makerOrderId : event.takerOrderId
const value = event.quantity * event.price
2021-08-13 08:05:49 -07:00
const feeRate = maker ? event.makerFee : event.takerFee
const side = maker ? reverseSide(event.takerSide) : event.takerSide
2021-07-24 11:12:52 -07:00
2021-06-24 09:30:45 -07:00
return {
2021-06-24 13:57:11 -07:00
...event,
2021-07-24 11:12:52 -07:00
key: orderId.toString(),
liquidity: maker ? 'Maker' : 'Taker',
size: event.quantity,
price: event.price,
value,
feeCost: (feeRate * value).toFixed(4),
side,
2021-08-13 08:05:49 -07:00
marketName: getPerpMarketName(event),
2021-06-24 09:30:45 -07:00
}
}
2021-06-24 13:57:11 -07:00
const parsedSerumEvent = (event) => {
2021-06-24 09:30:45 -07:00
return {
2021-06-24 13:57:11 -07:00
...event,
key: `${event.maker}-${event.price}`,
liquidity: event?.eventFlags?.maker ? 'Maker' : 'Taker',
value: event.price * event.size,
side: event.side,
2021-06-24 09:30:45 -07:00
}
}
2021-07-24 11:12:52 -07:00
const formatTradeHistory = (
selectedMangoGroup,
mangoAccountPk: PublicKey,
newTradeHistory
) => {
return newTradeHistory
.flat()
.map((trade) => {
2021-06-24 09:30:45 -07:00
if (trade.eventFlags) {
return parsedSerumEvent(trade)
} else {
2021-07-24 11:12:52 -07:00
return parsedPerpEvent(selectedMangoGroup, mangoAccountPk, trade)
}
})
.sort(byTimestamp)
}
2021-06-24 09:30:45 -07:00
export const useTradeHistory = () => {
2021-06-21 12:02:12 -07:00
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const fills = useMangoStore((s) => s.selectedMarket.fills)
2021-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-04-12 21:40:26 -07:00
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-06-24 09:30:45 -07:00
const tradeHistory = useMangoStore((s) => s.tradeHistory)
2021-04-12 21:40:26 -07:00
2021-06-23 08:32:33 -07:00
if (!mangoAccount || !selectedMangoGroup) return null
const openOrdersAccount =
2021-06-23 08:32:33 -07:00
mangoAccount.spotOpenOrdersAccounts[marketConfig.marketIndex]
2021-06-24 09:30:45 -07:00
const mangoAccountFills = fills
.filter((fill) => {
2021-06-24 13:57:11 -07:00
if (fill.openOrders) {
return openOrdersAccount?.publicKey
? fill.openOrders.equals(openOrdersAccount?.publicKey)
: false
2021-06-24 09:30:45 -07:00
} else {
return (
fill.taker.equals(mangoAccount.publicKey) ||
fill.maker.equals(mangoAccount.publicKey)
)
2021-06-24 09:30:45 -07:00
}
})
.map((fill) => ({ ...fill, marketName: marketConfig.name }))
2021-04-14 23:16:36 -07:00
const allTrades = []
2021-06-24 09:30:45 -07:00
if (mangoAccountFills && mangoAccountFills.length > 0) {
const newFills = mangoAccountFills.filter(
2021-04-14 23:16:36 -07:00
(fill) =>
2021-08-13 08:05:49 -07:00
!tradeHistory.flat().find((t) => {
if (t.orderId) {
return t.orderId === fill.orderId?.toString()
} else {
2021-08-13 13:02:12 -07:00
return t.seqNum === fill.seqNum?.toString()
2021-08-13 08:05:49 -07:00
}
})
2021-04-14 23:16:36 -07:00
)
const newTradeHistory = [...newFills, ...tradeHistory]
if (newFills.length > 0 && newTradeHistory.length !== allTrades.length) {
2021-07-24 11:12:52 -07:00
return formatTradeHistory(
selectedMangoGroup,
mangoAccount.publicKey,
newTradeHistory
)
}
2021-04-14 23:16:36 -07:00
}
2021-07-24 11:12:52 -07:00
return formatTradeHistory(
selectedMangoGroup,
mangoAccount.publicKey,
tradeHistory
)
}
export default useTradeHistory