Skip to content

Commit

Permalink
feat(bonsai-core): bonsai ontology (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo authored and tinaszheng committed Jan 7, 2025
1 parent df32a1a commit b73decb
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 17 deletions.
20 changes: 19 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
"plugin:react-hooks/recommended",
"prettier"
],
"overrides": [],
"overrides": [
{
"files": ["**/abacus-ts/**"],
"rules": {
"no-restricted-imports": "off"
}
}
],
"parserOptions": {
"project": "./tsconfig.json",
"ecmaVersion": "latest",
Expand Down Expand Up @@ -42,6 +49,17 @@
"no-multi-assign": "off",
"no-nested-ternary": "off",
"no-param-reassign": ["error", { "props": false }],
"no-restricted-imports": [
"error",
{
"patterns": [
"@/abacus-ts/*",
"!@/abacus-ts/ontology",
"!@/abacus-ts/lib",
"!@/abacus-ts/summaryTypes"
]
}
],
"no-return-assign": "off",
"no-return-await": "off",
"no-underscore-dangle": "off",
Expand Down
98 changes: 98 additions & 0 deletions src/abacus-ts/ontology.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { type RootState } from '@/state/_store';
import { getCurrentMarketId } from '@/state/perpetualsSelectors';

import {
getCurrentMarketAccountFills,
selectAccountFills,
selectAccountFillsLoading,
selectAccountOrdersLoading,
selectCurrentMarketOpenOrders,
selectCurrentMarketOrderHistory,
selectOpenOrders,
selectOrderHistory,
selectParentSubaccountOpenPositions,
selectParentSubaccountOpenPositionsLoading,
selectParentSubaccountSummary,
selectParentSubaccountSummaryLoading,
} from './selectors/account';
import {
selectAllAssetsInfo,
selectAllAssetsInfoLoading,
selectCurrentMarketAssetInfo,
} from './selectors/assets';
import {
selectRawIndexerHeightData,
selectRawIndexerHeightDataLoading,
selectRawValidatorHeightData,
selectRawValidatorHeightDataLoading,
} from './selectors/base';
import {
selectAllMarketsInfo,
selectAllMarketsInfoLoading,
selectCurrentMarketInfo,
} from './selectors/markets';

// every leaf is a selector or a paramaterized selector
type NestedSelectors = {
[K: string]:
| NestedSelectors
| ((state: RootState) => any)
| (() => (state: RootState, ...other: any[]) => any);
};

// all data should be accessed via selectors in this file
// no files outside abacus-ts should access anything within abacus-ts except this file
export const BonsaiCore = {
account: {
parentSubaccountSummary: {
data: selectParentSubaccountSummary,
loading: selectParentSubaccountSummaryLoading,
},
parentSubaccountPositions: {
data: selectParentSubaccountOpenPositions,
loading: selectParentSubaccountOpenPositionsLoading,
},
openOrders: {
data: selectOpenOrders,
loading: selectAccountOrdersLoading,
},
orderHistory: {
data: selectOrderHistory,
loading: selectAccountOrdersLoading,
},
fills: {
data: selectAccountFills,
loading: selectAccountFillsLoading,
},
},
markets: {
currentMarketId: getCurrentMarketId,
markets: {
data: selectAllMarketsInfo,
loading: selectAllMarketsInfoLoading,
},
assets: { data: selectAllAssetsInfo, loading: selectAllAssetsInfoLoading },
},
network: {
indexerHeight: {
data: selectRawIndexerHeightData,
loading: selectRawIndexerHeightDataLoading,
},
validatorHeight: {
data: selectRawValidatorHeightData,
loading: selectRawValidatorHeightDataLoading,
},
},
} as const satisfies NestedSelectors;

export const BonsaiHelpers = {
currentMarket: {
marketInfo: selectCurrentMarketInfo,
assetInfo: selectCurrentMarketAssetInfo,
account: {
openOrders: selectCurrentMarketOpenOrders,
orderHistory: selectCurrentMarketOrderHistory,
fills: getCurrentMarketAccountFills,
},
},
} as const satisfies NestedSelectors;
9 changes: 7 additions & 2 deletions src/abacus-ts/selectors/assets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { createAppSelector } from '@/state/appTypes';

import { transformAssetsInfo } from '../calculators/assets';
import { selectRawAssetsData } from './base';
import { selectRawAssets, selectRawAssetsData } from './base';
import { selectCurrentMarketInfo } from './markets';

export const selectAllAssetsInfo = createAppSelector([selectRawAssetsData], (assets) =>
transformAssetsInfo(assets)
);

export const selectAssetInfo = () =>
export const selectAllAssetsInfoLoading = createAppSelector(
[selectRawAssets],
(assets) => assets.status
);

export const createSelectAssetInfo = () =>
createAppSelector(
[selectAllAssetsInfo, (_s, assetId: string) => assetId],
(assets, assetId) => assets?.[assetId]
Expand Down
5 changes: 5 additions & 0 deletions src/abacus-ts/selectors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const selectRawAccountState = (state: RootState) => state.raw.account;
export const selectRawMarkets = (state: RootState) => state.raw.markets.allMarkets;
export const selectRawMarketsData = (state: RootState) => state.raw.markets.allMarkets.data;
export const selectRawAssetsData = (state: RootState) => state.raw.markets.assets.data;
export const selectRawAssets = (state: RootState) => state.raw.markets.assets;

export const selectRawParentSubaccount = (state: RootState) => state.raw.account.parentSubaccount;
export const selectRawParentSubaccountData = (state: RootState) =>
Expand All @@ -31,6 +32,10 @@ export const selectRawIndexerHeightData = (state: RootState) =>
state.raw.heights.indexerHeight.data;
export const selectRawValidatorHeightData = (state: RootState) =>
state.raw.heights.validatorHeight.data;
export const selectRawIndexerHeightDataLoading = (state: RootState) =>
state.raw.heights.indexerHeight.status;
export const selectRawValidatorHeightDataLoading = (state: RootState) =>
state.raw.heights.validatorHeight.status;

export const selectRawFillsRest = (state: RootState) => state.raw.account.fills;
export const selectRawOrdersRest = (state: RootState) => state.raw.account.orders;
Expand Down
5 changes: 4 additions & 1 deletion src/abacus-ts/selectors/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export const selectCurrentMarketInfo = createAppSelector(
(markets, currentMarketId) => (currentMarketId ? markets?.[currentMarketId] : undefined)
);

export const selectMarketsInfoLoading = createAppSelector([selectRawMarkets], mergeLoadableStatus);
export const selectAllMarketsInfoLoading = createAppSelector(
[selectRawMarkets],
mergeLoadableStatus
);
1 change: 1 addition & 0 deletions src/hooks/useInitializePage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef } from 'react';

// eslint-disable-next-line no-restricted-imports
import { IndexerWebsocketManager } from '@/abacus-ts/websocket/lib/indexerWebsocketManager';

import { LocalStorageKey } from '@/constants/localStorage';
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useSeen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react';

import { selectRawIndexerHeightData } from '@/abacus-ts/selectors/base';
import { BonsaiCore } from '@/abacus-ts/ontology';
import { shallowEqual } from 'react-redux';

import { getUserWalletAddress } from '@/state/accountSelectors';
Expand All @@ -14,7 +14,7 @@ export function useViewPanel(
) {
const networkId = useAppSelector(getSelectedNetwork);
const walletId = useAppSelector(getUserWalletAddress);
const height = useAppSelector(selectRawIndexerHeightData);
const height = useAppSelector(BonsaiCore.network.indexerHeight.data);
const lastSetCore = useRef<any[]>([]);

const dispatch = useAppDispatch();
Expand Down
14 changes: 6 additions & 8 deletions src/state/_store.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// eslint-disable-next-line no-restricted-imports
import { storeLifecycles } from '@/abacus-ts/storeLifecycles';
import { Middleware, combineReducers, configureStore } from '@reduxjs/toolkit';
import { persistReducer, persistStore } from 'redux-persist';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import storage from 'redux-persist/lib/storage';

import abacusStateManager from '@/lib/abacus';
import { runFn } from '@/lib/do';
import { testFlags } from '@/lib/testFlags';

import { accountSlice } from './account';
import { accountUiMemorySlice } from './accountUiMemory';
Expand Down Expand Up @@ -98,13 +99,10 @@ export const persistor = persistStore(store);
// Set store so (Abacus & v4-Client) classes can getState and dispatch
abacusStateManager.setStore(store);

if (testFlags.useAbacusTs) {
runFn(async () => {
const { storeLifecycles } = await import('@/abacus-ts/storeLifecycles');
// we ignore the cleanups for now since we want these running forever
storeLifecycles.forEach((fn) => fn(store));
});
}
runFn(async () => {
// we ignore the cleanups for now since we want these running forever
storeLifecycles.forEach((fn) => fn(store));
});

export type RootStore = typeof store;
export type RootState = ReturnType<typeof store.getState>;
Expand Down
6 changes: 3 additions & 3 deletions src/state/accountSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectRawIndexerHeightData } from '@/abacus-ts/selectors/base';
import { BonsaiCore } from '@/abacus-ts/ontology';
import { OrderSide } from '@dydxprotocol/v4-client-js';
import BigNumber from 'bignumber.js';
import { groupBy, sum } from 'lodash';
Expand Down Expand Up @@ -702,7 +702,7 @@ export const createGetUnseenOrdersCount = () =>
createAppSelector(
[
getCurrentAccountMemory,
selectRawIndexerHeightData,
BonsaiCore.network.indexerHeight.data,
getSubaccountOrders,
(state, market: string | undefined) => market,
],
Expand Down Expand Up @@ -734,7 +734,7 @@ export const createGetUnseenFillsCount = () =>
createAppSelector(
[
getCurrentAccountMemory,
selectRawIndexerHeightData,
BonsaiCore.network.indexerHeight.data,
getSubaccountFills,
(state, market: string | undefined) => market,
],
Expand Down
1 change: 1 addition & 0 deletions src/state/raw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Loadable, loadableIdle } from '@/abacus-ts/lib/loadable';
// eslint-disable-next-line no-restricted-imports
import {
AssetInfos,
MarketsData,
Expand Down

0 comments on commit b73decb

Please sign in to comment.