Skip to content

Commit

Permalink
improve decode logic for oderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 committed Dec 28, 2023
1 parent 7f9deff commit 40b13d5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 3 additions & 3 deletions components/trade/DepthChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ const DepthChart = () => {
</div>
</div>
<div
className={
className={`${
increaseHeight ? 'h-[570px]' : isTablet ? 'h-[538px]' : 'h-[482px]'
}
}`}
>
<ResponsiveContainer width="100%" height="100%">
<ResponsiveContainer width="100" height="100%">
<AreaChart
data={chartData}
layout="vertical"
Expand Down
23 changes: 17 additions & 6 deletions components/trade/Orderbook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ const Orderbook = () => {
connection
.getAccountInfoAndContext(bidsPk)
.then(({ context, value: info }) => {
if (!info) return
if (!info || !isMarketReadyForDecode(market)) return
const decodedBook = decodeBook(client, market, info, 'bids')
set((state) => {
state.selectedMarket.lastSeenSlot.bids = context.slot
Expand All @@ -319,8 +319,8 @@ const Orderbook = () => {
mangoStore.getState().selectedMarket.lastSeenSlot.bids
if (context.slot > lastSeenSlot) {
const market = getMarket()
if (!market) return
const decodedBook = decodeBook(client, market, info, 'bids')
if (!isMarketReadyForDecode(market)) return
const decodedBook = decodeBook(client, market!, info, 'bids')
if (decodedBook instanceof BookSide) {
updatePerpMarketOnGroup(decodedBook, 'bids')
}
Expand All @@ -340,7 +340,7 @@ const Orderbook = () => {
connection
.getAccountInfoAndContext(asksPk)
.then(({ context, value: info }) => {
if (!info) return
if (!info || !isMarketReadyForDecode(market)) return
const decodedBook = decodeBook(client, market, info, 'asks')
set((state) => {
state.selectedMarket.asksAccount = decodedBook
Expand All @@ -355,8 +355,8 @@ const Orderbook = () => {
mangoStore.getState().selectedMarket.lastSeenSlot.asks
if (context.slot > lastSeenSlot) {
const market = getMarket()
if (!market) return
const decodedBook = decodeBook(client, market, info, 'asks')
if (!isMarketReadyForDecode(market)) return
const decodedBook = decodeBook(client, market!, info, 'asks')
if (decodedBook instanceof BookSide) {
updatePerpMarketOnGroup(decodedBook, 'asks')
}
Expand Down Expand Up @@ -812,3 +812,14 @@ function usersOpenOrderPrices(market: Market | PerpMarket | null) {
}
return usersOpenOrderPrices
}

const isMarketReadyForDecode = (market: PerpMarket | Market | undefined) => {
if (
!market ||
(market instanceof Market &&
(!market.decoded.accountFlags.initialized ||
!(market.decoded.accountFlags.bids ^ market.decoded.accountFlags.asks)))
)
return false
else return true
}
4 changes: 3 additions & 1 deletion pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,9 @@ const VaultData = ({ bank }: { bank: Bank }) => {
<KeyValuePair
label="Vault balance"
value={
vault ? toUiDecimals(vault.amount.toNumber(), bank.mintDecimals) : '...'
vault
? toUiDecimals(new BN(vault.amount.toString()), bank.mintDecimals)
: '...'
}
/>
)
Expand Down

0 comments on commit 40b13d5

Please sign in to comment.