mango-ui-v3/hooks/useOpenOrders.tsx

99 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-06-23 08:32:33 -07:00
import {
PerpOrder,
BookSide,
BookSideLayout,
getMarketByPublicKey,
MarketConfig,
PerpMarket,
MangoAccount,
2021-06-23 08:32:33 -07:00
} from '@blockworks-foundation/mango-client'
import { Market, Orderbook } from '@project-serum/serum'
2021-06-18 20:07:57 -07:00
import { Order } from '@project-serum/serum/lib/market'
import useMangoStore from '../stores/useMangoStore'
2021-06-18 20:07:57 -07:00
type OrderInfo = {
2021-06-23 08:32:33 -07:00
order: Order | PerpOrder
market: { account: Market | PerpMarket; config: MarketConfig }
}
2021-06-23 08:32:33 -07:00
function parseSpotOrders(
market: Market,
config: MarketConfig,
mangoAccount: MangoAccount,
accountInfos
) {
const openOrders = mangoAccount.spotOpenOrdersAccounts[config.marketIndex]
if (!openOrders) return []
2021-06-18 20:07:57 -07:00
const bidData = accountInfos[market['_decoded'].bids.toBase58()]?.data
const askData = accountInfos[market['_decoded'].asks.toBase58()]?.data
2021-06-23 08:32:33 -07:00
const bidOrderBook =
market && bidData ? Orderbook.decode(market, bidData) : []
const askOrderBook =
market && askData ? Orderbook.decode(market, askData) : []
2021-06-18 20:07:57 -07:00
const openOrdersForMarket = [...bidOrderBook, ...askOrderBook].filter((o) =>
2021-06-23 08:32:33 -07:00
o.openOrdersAddress.equals(openOrders.address)
2021-06-18 20:07:57 -07:00
)
return openOrdersForMarket.map<OrderInfo>((order) => ({
2021-06-23 08:32:33 -07:00
order,
market: { account: market, config: config },
}))
2021-06-18 20:07:57 -07:00
}
2021-06-23 08:32:33 -07:00
function parsePerpOpenOrders(
market: PerpMarket,
config: MarketConfig,
mangoAccount: MangoAccount,
accountInfos
) {
2021-06-18 20:07:57 -07:00
const bidData = accountInfos[market.bids.toBase58()]?.data
const askData = accountInfos[market.asks.toBase58()]?.data
2021-06-23 08:32:33 -07:00
const bidOrderBook =
market && bidData
? new BookSide(market.bids, market, BookSideLayout.decode(bidData))
: []
const askOrderBook =
market && askData
? new BookSide(market.asks, market, BookSideLayout.decode(askData))
: []
2021-06-18 20:07:57 -07:00
const openOrdersForMarket = [...bidOrderBook, ...askOrderBook].filter((o) =>
2021-06-23 08:32:33 -07:00
o.owner.equals(mangoAccount.publicKey)
2021-06-18 20:07:57 -07:00
)
return openOrdersForMarket.map<OrderInfo>((order) => ({
order,
market: { account: market, config: config },
2021-06-23 08:32:33 -07:00
}))
}
2021-04-07 08:44:22 -07:00
export function useOpenOrders() {
const markets = useMangoStore((s) => s.selectedMangoGroup.markets)
2021-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-06-18 20:07:57 -07:00
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
2021-04-12 20:39:08 -07:00
const accountInfos = useMangoStore((s) => s.accountInfos)
2021-04-07 08:44:22 -07:00
2021-06-23 08:32:33 -07:00
if (!mangoGroup || !mangoAccount || !accountInfos) return null
2021-04-07 08:44:22 -07:00
2021-06-23 08:32:33 -07:00
const openOrders = Object.entries(markets).map(([address, market]) => {
2021-06-18 20:07:57 -07:00
const marketConfig = getMarketByPublicKey(groupConfig, address)
if (market instanceof Market) {
2021-06-23 08:32:33 -07:00
return parseSpotOrders(market, marketConfig, mangoAccount, accountInfos)
2021-06-18 20:07:57 -07:00
} else if (market instanceof PerpMarket) {
2021-06-23 08:32:33 -07:00
return parsePerpOpenOrders(
market,
marketConfig,
mangoAccount,
accountInfos
)
2021-06-18 20:07:57 -07:00
}
})
2021-04-07 08:44:22 -07:00
return openOrders.flat()
2021-04-07 08:44:22 -07:00
}