Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tina/deposit-route-lo…
Browse files Browse the repository at this point in the history
…ading
  • Loading branch information
tinaszheng committed Jan 10, 2025
2 parents e34eb67 + 3604152 commit 91ddab3
Show file tree
Hide file tree
Showing 56 changed files with 939 additions and 377 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"^./polyfills$",
"^react$",
"<THIRD_PARTY_MODULES>",
"^@/(constants|abi)(.*)$",
"^@/(constants|abi|types)(.*)$",
"^@/(hooks|contexts)(.*)$",
"^@/(styles|icons)(.*)$",
"^@/(components|views|pages|layout)(.*)$",
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/calculators/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { weakMapMemoize } from 'reselect';

import { getTickSizeDecimalsFromPrice } from '@/lib/numbers';

import { AssetInfo, AssetInfos } from '../rawTypes';
import { AssetInfo, AssetInfos } from '../types/rawTypes';

export const parseAssetInfo = weakMapMemoize((assetInfo: AssetInfo, assetId: string) => ({
assetId,
Expand Down
3 changes: 2 additions & 1 deletion src/abacus-ts/calculators/blockRewards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IndexerHistoricalBlockTradingReward } from '@/types/indexer/indexerApiGen';
import { keyBy, maxBy } from 'lodash';

import { IndexerHistoricalBlockTradingReward } from '@/types/indexer/indexerApiGen';

import { MustBigNumber } from '@/lib/numbers';

import { Loadable } from '../lib/loadable';
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/calculators/fills.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexerCompositeFillObject } from '@/types/indexer/indexerManual';
import { keyBy, maxBy, orderBy } from 'lodash';

import { EMPTY_ARR } from '@/constants/objects';
import { IndexerCompositeFillObject } from '@/types/indexer/indexerManual';

import { MustBigNumber } from '@/lib/numbers';

Expand Down
6 changes: 3 additions & 3 deletions src/abacus-ts/calculators/markets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IndexerPerpetualMarketResponseObject } from '@/types/indexer/indexerApiGen';
import BigNumber from 'bignumber.js';
import { mapValues } from 'lodash';
import { weakMapMemoize } from 'reselect';

import { TOKEN_DECIMALS, USD_DECIMALS } from '@/constants/numbers';
import { IndexerPerpetualMarketResponseObject } from '@/types/indexer/indexerApiGen';

import {
getAssetFromMarketId,
Expand All @@ -12,8 +12,8 @@ import {
} from '@/lib/assetUtils';
import { MaybeBigNumber, MustBigNumber } from '@/lib/numbers';

import { MarketsData } from '../rawTypes';
import { MarketInfo, MarketsInfo } from '../summaryTypes';
import { MarketsData } from '../types/rawTypes';
import { MarketInfo, MarketsInfo } from '../types/summaryTypes';

export function calculateAllMarkets(markets: MarketsData | undefined): MarketsInfo | undefined {
if (markets == null) {
Expand Down
8 changes: 4 additions & 4 deletions src/abacus-ts/calculators/orders.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { IndexerBestEffortOpenedStatus, IndexerOrderStatus } from '@/types/indexer/indexerApiGen';
import { IndexerCompositeOrderObject } from '@/types/indexer/indexerManual';
import { HeightResponse } from '@dydxprotocol/v4-client-js';
import { mapValues, maxBy, orderBy } from 'lodash';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';
import { IndexerBestEffortOpenedStatus, IndexerOrderStatus } from '@/types/indexer/indexerApiGen';
import { IndexerCompositeOrderObject } from '@/types/indexer/indexerManual';

import { assertNever } from '@/lib/assertNever';
import { getDisplayableTickerFromMarket } from '@/lib/assetUtils';
import { mapIfPresent } from '@/lib/do';
import { MaybeBigNumber, MustBigNumber } from '@/lib/numbers';

import { mergeObjects } from '../lib/mergeObjects';
import { OrdersData } from '../rawTypes';
import { OrderStatus, SubaccountOrder } from '../summaryTypes';
import { OrdersData } from '../types/rawTypes';
import { OrderStatus, SubaccountOrder } from '../types/summaryTypes';

export function calculateOpenOrders(orders: SubaccountOrder[]) {
return orders.filter(
Expand Down
12 changes: 6 additions & 6 deletions src/abacus-ts/calculators/subaccount.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import BigNumber from 'bignumber.js';
import { mapValues, orderBy } from 'lodash';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';
import {
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
IndexerPerpetualPositionStatus,
IndexerPositionSide,
} from '@/types/indexer/indexerApiGen';
import BigNumber from 'bignumber.js';
import { mapValues, orderBy } from 'lodash';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';

import { getAssetFromMarketId } from '@/lib/assetUtils';
import { calc } from '@/lib/do';
import { BIG_NUMBERS, MaybeBigNumber, MustBigNumber, ToBigNumber } from '@/lib/numbers';
import { isPresent } from '@/lib/typeUtils';

import { ChildSubaccountData, MarketsData, ParentSubaccountData } from '../rawTypes';
import { ChildSubaccountData, MarketsData, ParentSubaccountData } from '../types/rawTypes';
import {
GroupedSubaccountSummary,
SubaccountPosition,
Expand All @@ -24,7 +24,7 @@ import {
SubaccountSummary,
SubaccountSummaryCore,
SubaccountSummaryDerived,
} from '../summaryTypes';
} from '../types/summaryTypes';
import { getMarketEffectiveInitialMarginForMarket } from './markets';

export function calculateParentSubaccountPositions(
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/calculators/transfers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexerTransferCommonResponseObject } from '@/types/indexer/indexerManual';
import { keyBy, maxBy } from 'lodash';

import { EMPTY_ARR } from '@/constants/objects';
import { IndexerTransferCommonResponseObject } from '@/types/indexer/indexerManual';

import { MustBigNumber } from '@/lib/numbers';

Expand Down
20 changes: 16 additions & 4 deletions src/abacus-ts/lib/resourceCacheManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logAbacusTsError } from '../logs';

type CacheEntry<T> = {
resource: T;
count: number;
Expand All @@ -14,7 +16,7 @@ export class ResourceCacheManager<T, U> {
constructor(
private options: {
constructor: (key: U) => T;
destroyer: (resource: NoInfer<T>) => void;
destroyer: (resource: NoInfer<T>, key: NoInfer<U>) => void;
keySerializer: (key: NoInfer<U>) => string;
destroyDelayMs?: number;
}
Expand Down Expand Up @@ -42,7 +44,14 @@ export class ResourceCacheManager<T, U> {
markDone(key: U): void {
const serializedKey = this.options.keySerializer(key);
const entry = this.cache[serializedKey];
if (!entry) return;
if (!entry) {
logAbacusTsError('ResourceCacheManager', 'tried to mark done unknown key', key);
return;
}
if (entry.count <= 0) {
logAbacusTsError('ResourceCacheManager', 'tried to mark done key with no subscribers', key);
entry.count = 1;
}

entry.count -= 1;

Expand All @@ -55,9 +64,12 @@ export class ResourceCacheManager<T, U> {
const delay = this.options.destroyDelayMs ?? 1000;
entry.destroyTimeout = setTimeout(() => {
const latestVal = this.cache[serializedKey];
if (!latestVal) return;
if (!latestVal) {
logAbacusTsError('ResourceCacheManager', 'could not find resource to destroy', key);
return;
}

this.options.destroyer(latestVal.resource);
this.options.destroyer(latestVal.resource, key);
delete this.cache[serializedKey];
}, delay);
}
Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/rest/funding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexerHistoricalFundingResponse } from '@/types/indexer/indexerApiGen';
import { useQuery } from '@tanstack/react-query';

import { timeUnits } from '@/constants/time';
import { IndexerHistoricalFundingResponse } from '@/types/indexer/indexerApiGen';

import { useAppSelector } from '@/state/appTypes';
import { getCurrentMarketIdIfTradeable } from '@/state/perpetualsSelectors';
Expand Down
3 changes: 2 additions & 1 deletion src/abacus-ts/rest/orders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isParentSubaccountOrders } from '@/types/indexer/indexerChecks';
import { keyBy } from 'lodash';

import { isParentSubaccountOrders } from '@/types/indexer/indexerChecks';

import { type RootStore } from '@/state/_store';
import { setAccountOrdersRaw } from '@/state/raw';

Expand Down
2 changes: 1 addition & 1 deletion src/abacus-ts/selectors/account.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IndexerPerpetualPositionStatus } from '@/types/indexer/indexerApiGen';
import { pick } from 'lodash';
import { shallowEqual } from 'react-redux';

import { EMPTY_ARR } from '@/constants/objects';
import { IndexerPerpetualPositionStatus } from '@/types/indexer/indexerApiGen';

import { createAppSelector } from '@/state/appTypes';
import { getCurrentMarketId } from '@/state/perpetualsSelectors';
Expand Down
32 changes: 32 additions & 0 deletions src/abacus-ts/types/operationTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ofType, unionize, UnionOf } from 'unionize';

import {
IndexerAssetPositionResponseObject,
IndexerPerpetualPositionResponseObject,
} from '@/types/indexer/indexerApiGen';

export const SubaccountOperations = unionize(
{
AddPerpetualPosition: ofType<{
subaccountNumber: string;
market: string;
position: Omit<IndexerPerpetualPositionResponseObject, 'market' | 'subaccountNumber'>;
}>(),
ModifyPerpetualPosition: ofType<{
subaccountNumber: string;
market: string;
changes: Partial<Omit<IndexerPerpetualPositionResponseObject, 'market' | 'subaccountNumber'>>;
}>(),
ModifyUsdcAssetPosition: ofType<{
subaccountNumber: string;
changes: Partial<Pick<IndexerAssetPositionResponseObject, 'size' | 'side'>>;
}>(),
},
{ tag: 'operation' as const, value: 'payload' as const }
);

export type SubaccountOperation = UnionOf<typeof SubaccountOperations>;

export interface SubaccountBatchedOperations {
operations: SubaccountOperation[];
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MetadataServiceAssetInfo, MetadataServicePrice } from '@/constants/assetMetadata';
import {
IndexerAssetPositionResponseObject,
IndexerHistoricalBlockTradingReward,
Expand All @@ -11,8 +12,6 @@ import {
IndexerTransferCommonResponseObject,
} from '@/types/indexer/indexerManual';

import { MetadataServiceAssetInfo, MetadataServicePrice } from '@/constants/assetMetadata';

import { PartialBy } from '@/lib/typeUtils';

export type MarketsData = { [marketId: string]: IndexerPerpetualMarketResponseObject };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type BigNumber } from 'bignumber.js';

import {
IndexerAPITimeInForce,
IndexerOrderSide,
IndexerOrderType,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
} from '@/types/indexer/indexerApiGen';
import { type BigNumber } from 'bignumber.js';

import { BaseTrade } from './rawTypes';

Expand Down
48 changes: 48 additions & 0 deletions src/abacus-ts/websocket/lib/indexerValueManagerHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ResourceCacheManager } from '@/abacus-ts/lib/resourceCacheManager';
import stableStringify from 'fast-json-stable-stringify';

import { IndexerWebsocket } from './indexerWebsocket';
import { IndexerWebsocketManager } from './indexerWebsocketManager';
import { WebsocketDerivedValue } from './websocketDerivedValue';

// this is set to just above the websocket subscribe timeout because of race conditions in the indexer backend
const DESTROY_DELAY_MS = 21000;

type WebsocketValueCreator<Args, ReturnType> = (
websocket: IndexerWebsocket,
args: Args
) => WebsocketDerivedValue<ReturnType>;

export function makeWsValueManager<Args, ReturnType>(
creator: WebsocketValueCreator<Args, ReturnType>
): ResourceCacheManager<WebsocketDerivedValue<ReturnType>, Args & { wsUrl: string }> {
return new ResourceCacheManager({
constructor: (allArgs: Args & { wsUrl: string }) =>
creator(IndexerWebsocketManager.use(allArgs.wsUrl), allArgs),

destroyer: (instance, { wsUrl }) => {
instance.teardown();
IndexerWebsocketManager.markDone(wsUrl);
},

// take care - extra properties on the key will cause divergent behavior
// (cache misses, unexpected new object creation, marking incorrect objects as done, etc)
// only ever pass the exact key type for correct behavior
keySerializer: (allArgs) => stableStringify(allArgs),

destroyDelayMs: DESTROY_DELAY_MS,
});
}

export function subscribeToWsValue<Args, ReturnType>(
manager: ResourceCacheManager<WebsocketDerivedValue<ReturnType>, Args & { wsUrl: string }>,
args: NoInfer<Args> & { wsUrl: string },
handleChange: (val: NoInfer<ReturnType>) => void
): () => void {
const value = manager.use(args);
const unsub = value.subscribe(handleChange);
return () => {
unsub();
manager.markDone(args);
};
}
Loading

0 comments on commit 91ddab3

Please sign in to comment.