Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix
Browse files Browse the repository at this point in the history
tyleroooo committed Jan 13, 2025
1 parent fcb8990 commit 7ebcc9c
Showing 6 changed files with 29 additions and 18 deletions.
20 changes: 11 additions & 9 deletions src/abacus-ts/calculators/markets.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { mapValues } from 'lodash';
import { weakMapMemoize } from 'reselect';

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

import {
getAssetFromMarketId,
@@ -22,9 +22,7 @@ export function calculateAllMarkets(markets: MarketsData | undefined): MarketsIn
return mapValues(markets, calculateMarket);
}

export function getMarketEffectiveInitialMarginForMarket(
market: IndexerPerpetualMarketResponseObject
) {
export function getMarketEffectiveInitialMarginForMarket(market: IndexerWsBaseMarketObject) {
const initialMarginFraction = MaybeBigNumber(market.initialMarginFraction);
const openInterest = MaybeBigNumber(market.openInterest);
const openInterestLowerCap = MaybeBigNumber(market.openInterestLowerCap);
@@ -59,28 +57,32 @@ export function getMarketEffectiveInitialMarginForMarket(
return effectiveIMF;
}

function calculateDerivedMarketDisplayItems(market: IndexerPerpetualMarketResponseObject) {
function calculateDerivedMarketDisplayItems(market: IndexerWsBaseMarketObject) {
return {
assetId: getAssetFromMarketId(market.ticker),
displayableAsset: getDisplayableAssetFromTicker(market.ticker),
displayableTicker: getDisplayableTickerFromMarket(market.ticker),
};
}

function calculateDerivedMarketCore(market: IndexerPerpetualMarketResponseObject) {
function calculateDerivedMarketCore(market: IndexerWsBaseMarketObject) {
return {
effectiveInitialMarginFraction: getMarketEffectiveInitialMarginForMarket(market),
openInterestUSDC: MustBigNumber(market.openInterest).times(market.oraclePrice).toNumber(),
openInterestUSDC: MustBigNumber(market.openInterest)
.times(market.oraclePrice ?? 0)
.toNumber(),
percentChange24h: MustBigNumber(market.oraclePrice).isZero()
? null
: MustBigNumber(market.priceChange24H).div(market.oraclePrice).toNumber(),
: MustBigNumber(market.priceChange24H)
.div(market.oraclePrice ?? 0)
.toNumber(),
stepSizeDecimals: MaybeBigNumber(market.stepSize)?.decimalPlaces() ?? TOKEN_DECIMALS,
tickSizeDecimals: MaybeBigNumber(market.tickSize)?.decimalPlaces() ?? USD_DECIMALS,
};
}

const calculateMarket = weakMapMemoize(
(market: IndexerPerpetualMarketResponseObject): MarketInfo => ({
(market: IndexerWsBaseMarketObject): MarketInfo => ({
...market,
...calculateDerivedMarketDisplayItems(market),
...calculateDerivedMarketCore(market),
6 changes: 3 additions & 3 deletions src/abacus-ts/calculators/subaccount.ts
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@ import { mapValues, orderBy } from 'lodash';

import { NUM_PARENT_SUBACCOUNTS } from '@/constants/account';
import {
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
IndexerPerpetualPositionStatus,
IndexerPositionSide,
} from '@/types/indexer/indexerApiGen';
import { IndexerWsBaseMarketObject } from '@/types/indexer/indexerManual';

import { getAssetFromMarketId } from '@/lib/assetUtils';
import { calc } from '@/lib/do';
@@ -161,7 +161,7 @@ function calculateSubaccountSummaryDerived(core: SubaccountSummaryCore): Subacco
function calculateSubaccountPosition(
subaccountSummary: SubaccountSummary,
position: IndexerPerpetualPositionResponseObject,
market: IndexerPerpetualMarketResponseObject | undefined
market: IndexerWsBaseMarketObject | undefined
): SubaccountPosition {
const bnPosition = getBnPosition(position);
const core = calculateDerivedPositionCore(bnPosition, market);
@@ -190,7 +190,7 @@ function getBnPosition(position: IndexerPerpetualPositionResponseObject): Subacc

function calculateDerivedPositionCore(
position: SubaccountPositionBase,
market: IndexerPerpetualMarketResponseObject | undefined
market: IndexerWsBaseMarketObject | undefined
): SubaccountPositionDerivedCore {
const marginMode = position.subaccountNumber < NUM_PARENT_SUBACCOUNTS ? 'CROSS' : 'ISOLATED';
const effectiveImf =
4 changes: 2 additions & 2 deletions src/abacus-ts/types/rawTypes.ts
Original file line number Diff line number Diff line change
@@ -2,19 +2,19 @@ import { MetadataServiceAssetInfo, MetadataServicePrice } from '@/constants/asse
import {
IndexerAssetPositionResponseObject,
IndexerHistoricalBlockTradingReward,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
IndexerTradeResponseObject,
} from '@/types/indexer/indexerApiGen';
import {
IndexerCompositeFillObject,
IndexerCompositeOrderObject,
IndexerTransferCommonResponseObject,
IndexerWsBaseMarketObject,
} from '@/types/indexer/indexerManual';

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

export type MarketsData = { [marketId: string]: IndexerPerpetualMarketResponseObject };
export type MarketsData = { [marketId: string]: IndexerWsBaseMarketObject };
export type OrdersData = { [orderId: string]: IndexerCompositeOrderObject };

export type OrderbookData = {
4 changes: 2 additions & 2 deletions src/abacus-ts/types/summaryTypes.ts
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@ import {
IndexerAPITimeInForce,
IndexerOrderSide,
IndexerOrderType,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
} from '@/types/indexer/indexerApiGen';
import { IndexerWsBaseMarketObject } from '@/types/indexer/indexerManual';

import { BaseTrade } from './rawTypes';

@@ -23,7 +23,7 @@ type ConvertStringToBigNumber<T, K extends SelectStringProperties<T>> = {
[P in keyof T]: P extends K ? ReplaceBigNumberInUnion<T[P]> : T[P];
};

export type MarketInfo = IndexerPerpetualMarketResponseObject & {
export type MarketInfo = IndexerWsBaseMarketObject & {
assetId: string;
displayableAsset: string;
displayableTicker: string;
4 changes: 2 additions & 2 deletions src/types/indexer/indexerChecks.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import {
IndexerHistoricalBlockTradingRewardsResponse,
IndexerOrderbookResponseObject,
IndexerParentSubaccountTransferResponse,
IndexerPerpetualMarketResponse,
IndexerTradeResponse,
} from './indexerApiGen';
import {
@@ -14,14 +13,15 @@ import {
IndexerWsOrderbookUpdateResponse,
IndexerWsParentSubaccountSubscribedResponse,
IndexerWsParentSubaccountUpdateObject,
IndexerWsPerpetualMarketResponse,
IndexerWsTradesUpdateObject,
} from './indexerManual';

export const isWsParentSubaccountSubscribed =
typia.createAssert<IndexerWsParentSubaccountSubscribedResponse>();
export const isWsParentSubaccountUpdates =
typia.createAssert<IndexerWsParentSubaccountUpdateObject[]>();
export const isWsPerpetualMarketResponse = typia.createAssert<IndexerPerpetualMarketResponse>();
export const isWsPerpetualMarketResponse = typia.createAssert<IndexerWsPerpetualMarketResponse>();
export const isWsMarketUpdateResponses = typia.createAssert<IndexerWsMarketUpdateResponse[]>();
export const isWsOrderbookResponse = typia.createAssert<IndexerOrderbookResponseObject>();
export const isWsOrderbookUpdateResponses =
9 changes: 9 additions & 0 deletions src/types/indexer/indexerManual.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import {
IndexerOrderSide,
IndexerOrderType,
IndexerParentSubaccountResponse,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualMarketStatus,
IndexerPerpetualMarketType,
IndexerPerpetualPositionResponseObject,
@@ -87,6 +88,14 @@ export interface IndexerCompositeMarketObject {
incrementalInitialMarginFraction?: string;
}

// just make oraclePrice optional
export type IndexerWsBaseMarketObject = Omit<IndexerPerpetualMarketResponseObject, 'oraclePrice'> &
Partial<Pick<IndexerPerpetualMarketResponseObject, 'oraclePrice'>>;

export interface IndexerWsPerpetualMarketResponse {
markets: { [key: string]: IndexerWsBaseMarketObject };
}

export interface IndexerWsOrderbookUpdateResponse {
asks?: IndexerWsOrderbookUpdateItem[];
bids?: IndexerWsOrderbookUpdateItem[];

0 comments on commit 7ebcc9c

Please sign in to comment.