Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bonsai-core): market oracle price can be null #1427

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/abacus-ts/calculators/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/abacus-ts/calculators/subaccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 2 additions & 2 deletions src/abacus-ts/types/rawTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/abacus-ts/types/summaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
IndexerAPITimeInForce,
IndexerOrderSide,
IndexerOrderType,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualPositionResponseObject,
} from '@/types/indexer/indexerApiGen';
import { IndexerWsBaseMarketObject } from '@/types/indexer/indexerManual';

import { BaseTrade } from './rawTypes';

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/types/indexer/indexerChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
IndexerHistoricalBlockTradingRewardsResponse,
IndexerOrderbookResponseObject,
IndexerParentSubaccountTransferResponse,
IndexerPerpetualMarketResponse,
IndexerTradeResponse,
} from './indexerApiGen';
import {
Expand All @@ -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 =
Expand Down
9 changes: 9 additions & 0 deletions src/types/indexer/indexerManual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IndexerOrderSide,
IndexerOrderType,
IndexerParentSubaccountResponse,
IndexerPerpetualMarketResponseObject,
IndexerPerpetualMarketStatus,
IndexerPerpetualMarketType,
IndexerPerpetualPositionResponseObject,
Expand Down Expand Up @@ -87,6 +88,14 @@ export interface IndexerCompositeMarketObject {
incrementalInitialMarginFraction?: string;
}

// just make oraclePrice optional
export type IndexerWsBaseMarketObject = Omit<IndexerPerpetualMarketResponseObject, 'oraclePrice'> &
Partial<Pick<IndexerPerpetualMarketResponseObject, 'oraclePrice'>>;
Comment on lines +92 to +93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't oraclePrice null being reflected on the original IndexerPerpetualMarketResponseObject?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swagger types wrong, I guess. They aren't particularly tied to what's actually happening at runtime sadly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe we could change the backend type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: it's too annoying


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

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