Skip to content

Commit

Permalink
feat(bonsai-core): funding query (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu authored Jan 8, 2025
1 parent 7cd3afe commit 95cd904
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/abacus-ts/ontology.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type RootState } from '@/state/_store';
import { getCurrentMarketId } from '@/state/perpetualsSelectors';

import { useCurrentMarketHistoricalFunding } from './rest/funding';
import {
getCurrentMarketAccountFills,
selectAccountFills,
Expand Down Expand Up @@ -99,5 +100,6 @@ export const BonsaiHelpers = {
} as const satisfies NestedSelectors;

export const BonsaiHooks = {
useCurrentMarketHistoricalFunding,
useCurrentMarketLiveTrades: useCurrentMarketTradesValue,
} as const;
32 changes: 32 additions & 0 deletions src/abacus-ts/rest/funding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IndexerHistoricalFundingResponse } from '@/types/indexer/indexerApiGen';
import { useQuery } from '@tanstack/react-query';

import { timeUnits } from '@/constants/time';

import { useAppSelector } from '@/state/appTypes';
import { getCurrentMarketIdIfTradeable } from '@/state/perpetualsSelectors';

import { useIndexerClient } from './lib/useIndexer';

export const useCurrentMarketHistoricalFunding = () => {
const { indexerClient, key: indexerKey } = useIndexerClient();
const currentMarketId = useAppSelector(getCurrentMarketIdIfTradeable);

return useQuery({
enabled: Boolean(currentMarketId) && Boolean(indexerClient),
queryKey: ['historicalFunding', currentMarketId, indexerKey],
queryFn: async () => {
if (!currentMarketId) {
throw new Error('Invalid marketId found');
} else if (!indexerClient) {
throw new Error('Indexer client not found');
}

const result: IndexerHistoricalFundingResponse =
await indexerClient.markets.getPerpetualMarketHistoricalFunding(currentMarketId);
return result.historicalFunding;
},
refetchInterval: timeUnits.hour,
staleTime: timeUnits.hour,
});
};
1 change: 1 addition & 0 deletions src/abacus-ts/rest/lib/compositeClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function makeCompositeClient({
(async () => {
const networkOptimizer = new NetworkOptimizer();
const indexerUrl = networkConfig.endpoints.indexers[0];

if (indexerUrl == null) {
throw new Error('No indexer urls found');
}
Expand Down
3 changes: 2 additions & 1 deletion src/abacus-ts/websocket/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { timeUnits } from '@/constants/time';

import { type RootStore } from '@/state/_store';
import { createAppSelector } from '@/state/appTypes';
import { getCurrentMarketIdIfTradeable } from '@/state/perpetualsSelectors';
import { setOrderbookRaw } from '@/state/raw';

import { isTruthy } from '@/lib/isTruthy';
Expand Down Expand Up @@ -66,7 +67,7 @@ function orderbookWebsocketValue(
}

const selectMarketAndWsInfo = createAppSelector(
[selectWebsocketUrl, (state) => state.perpetuals.currentMarketIdIfTradeable],
[selectWebsocketUrl, getCurrentMarketIdIfTradeable],
(wsUrl, currentMarketId) => ({ wsUrl, currentMarketId })
);

Expand Down
4 changes: 2 additions & 2 deletions src/abacus-ts/websocket/trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DydxNetwork } from '@/constants/networks';

import { getSelectedNetwork } from '@/state/appSelectors';
import { useAppSelector } from '@/state/appTypes';
import { getCurrentMarketId } from '@/state/perpetualsSelectors';
import { getCurrentMarketIdIfTradeable } from '@/state/perpetualsSelectors';

import { mergeById } from '@/lib/mergeById';

Expand Down Expand Up @@ -67,7 +67,7 @@ export const TradeValuesManager = new ResourceCacheManager({

export function useCurrentMarketTradesValue() {
const selectedNetwork = useAppSelector(getSelectedNetwork);
const currentMarketId = useAppSelector(getCurrentMarketId);
const currentMarketId = useAppSelector(getCurrentMarketIdIfTradeable);
// useSyncExternalStore is better but the API doesn't fit this use case very well
const [trades, setTrades] = useState<Loadable<TradesData>>(loadableIdle());

Expand Down
6 changes: 6 additions & 0 deletions src/state/perpetualsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export const getMarketFilter = (state: RootState) => state.perpetuals.marketFilt
*/
export const getCurrentMarketId = (state: RootState) => state.perpetuals.currentMarketId;

/**
* @returns marketId of the market the user is currently viewing if it is tradeable (Internal)
*/
export const getCurrentMarketIdIfTradeable = (state: RootState) =>
state.perpetuals.currentMarketIdIfTradeable;

/**
* @returns displayId of the currentMarket the user is viewing (Render)
*/
Expand Down

0 comments on commit 95cd904

Please sign in to comment.