slot staleness for vamm orders remove from cache

This commit is contained in:
Nour Alharithi
2024-06-17 09:56:53 -07:00
parent 9c9a9f9fa8
commit 8a72808c85
3 changed files with 49 additions and 45 deletions

View File

@@ -1,21 +1,20 @@
import {
BN,
DLOBSubscriber,
DLOBSubscriptionConfig,
DriftEnv,
L2OrderBookGenerator,
MarketType,
PositionDirection,
groupL2,
isVariant,
} from '@drift-labs/sdk';
import { RedisClient } from '@drift/common';
import { logger } from '../utils/logger';
import {
SubscriberLookup,
addMarketSlotToResponse,
addOracletoResponse,
l2WithBNToStrings,
parsePositiveIntArray,
} from '../utils/utils';
import { setHealthStatus, HEALTH_STATUS } from '../core/metrics';
@@ -26,23 +25,24 @@ type wsMarketArgs = {
depth: number;
includeVamm: boolean;
numVammOrders?: number;
grouping?: number;
fallbackL2Generators?: L2OrderBookGenerator[];
updateOnChange?: boolean;
};
require('dotenv').config();
const PERP_MAKRET_STALENESS_THRESHOLD = 30 * 60 * 1000;
const SPOT_MAKRET_STALENESS_THRESHOLD = 60 * 60 * 1000;
const STALE_ORACLE_REMOVE_VAMM_THRESHOLD = 100;
const PERP_MARKETS_TO_SKIP_SLOT_CHECK = {
'mainnet-beta': [17, 25],
devnet: [17, 21, 23],
};
const SPOT_MARKETS_TO_SKIP_SLOT_CHECK = {
'mainnet-beta': [6, 8, 10, 15],
devnet: [],
};
const PERP_MARKETS_TO_SKIP_SLOT_CHECK =
process.env.PERP_MARKETS_TO_SKIP_SLOT_CHECK !== undefined
? parsePositiveIntArray(process.env.PERP_MARKETS_TO_SKIP_SLOT_CHECK)
: [];
const SPOT_MARKETS_TO_SKIP_SLOT_CHECK =
process.env.SPOT_MARKETS_TO_SKIP_SLOT_CHECK !== undefined
? parsePositiveIntArray(process.env.SPOT_MARKETS_TO_SKIP_SLOT_CHECK)
: [];
export type wsMarketInfo = {
marketIndex: number;
@@ -59,9 +59,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
Map<number, { slot: number; ts: number }>
>;
public skipSlotStalenessCheckMarketsPerp: number[];
public skipSlotStalenessCheckMarketsSpot: number[];
constructor(
config: DLOBSubscriptionConfig & {
env: DriftEnv;
@@ -85,11 +82,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
this.lastMarketSlotMap.set(MarketType.SPOT, new Map());
this.lastMarketSlotMap.set(MarketType.PERP, new Map());
this.skipSlotStalenessCheckMarketsPerp =
PERP_MARKETS_TO_SKIP_SLOT_CHECK[config.env];
this.skipSlotStalenessCheckMarketsSpot =
SPOT_MARKETS_TO_SKIP_SLOT_CHECK[config.env];
for (const market of config.perpMarketInfos) {
const perpMarket = this.driftClient.getPerpMarketAccount(
market.marketIndex
@@ -138,9 +130,25 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
getL2AndSendMsg(marketArgs: wsMarketArgs): void {
const clientPrefix = this.redisClient.forceGetClient().options.keyPrefix;
const grouping = marketArgs.grouping;
const { marketName, ...l2FuncArgs } = marketArgs;
const l2 = this.getL2(l2FuncArgs);
const marketType = isVariant(marketArgs.marketType, 'perp')
? 'perp'
: 'spot';
// Test for oracle staleness to know whether to include vamm
const dlobSlot = this.slotSource.getSlot();
const oracleSlot =
marketType === 'perp'
? this.driftClient.getOracleDataForPerpMarket(marketArgs.marketIndex)
.slot
: this.driftClient.getOracleDataForSpotMarket(marketArgs.marketIndex)
.slot;
let includeVamm = marketArgs.includeVamm;
if (dlobSlot - oracleSlot > STALE_ORACLE_REMOVE_VAMM_THRESHOLD) {
logger.info('Oracle is stale, removing vamm orders');
includeVamm = false;
}
const l2 = this.getL2({ ...l2FuncArgs, includeVamm });
const slot = l2.slot;
const lastMarketSlotAndTime = this.lastMarketSlotMap
.get(marketArgs.marketType)
@@ -154,18 +162,8 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
if (slot) {
delete l2.slot;
}
const marketType = isVariant(marketArgs.marketType, 'perp')
? 'perp'
: 'spot';
let l2Formatted: any;
if (grouping) {
const groupingBN = new BN(grouping);
l2Formatted = l2WithBNToStrings(
groupL2(l2, groupingBN, marketArgs.depth)
);
} else {
l2Formatted = l2WithBNToStrings(l2);
}
const l2Formatted = l2WithBNToStrings(l2);
if (marketArgs.updateOnChange) {
if (
@@ -200,13 +198,9 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
// Check if slot diffs are too large for oracle
const skipSlotCheck =
(marketType === 'perp' &&
this.skipSlotStalenessCheckMarketsPerp.includes(
marketArgs.marketIndex
)) ||
PERP_MARKETS_TO_SKIP_SLOT_CHECK.includes(marketArgs.marketIndex)) ||
(marketType === 'spot' &&
this.skipSlotStalenessCheckMarketsSpot.includes(
marketArgs.marketIndex
));
SPOT_MARKETS_TO_SKIP_SLOT_CHECK.includes(marketArgs.marketIndex));
if (
Math.abs(slot - parseInt(l2Formatted['oracleData']['slot'])) >
this.killSwitchSlotDiffThreshold &&

View File

@@ -141,7 +141,9 @@ async function main() {
try {
redisChannel = getRedisChannelFromMessage(parsedMessage);
} catch (error) {
const requestChannel = sanitiseChannelForClient(parsedMessage?.channel);
const requestChannel = sanitiseChannelForClient(
parsedMessage?.channel
);
if (requestChannel) {
ws.send(
JSON.stringify({
@@ -214,7 +216,9 @@ async function main() {
try {
redisChannel = getRedisChannelFromMessage(parsedMessage);
} catch (error) {
const requestChannel = sanitiseChannelForClient(parsedMessage?.channel);
const requestChannel = sanitiseChannelForClient(
parsedMessage?.channel
);
if (requestChannel) {
console.log('Error unsubscribing from channel:', error.message);
ws.send(

View File

@@ -414,7 +414,7 @@
kuler "^2.0.0"
"@drift-labs/sdk@file:./drift-common/protocol/sdk", "@drift-labs/sdk@file:drift-common/protocol/sdk":
version "2.82.0-beta.16"
version "2.84.0-beta.1"
dependencies:
"@coral-xyz/anchor" "0.28.1-beta.2"
"@ellipsis-labs/phoenix-sdk" "^1.4.2"
@@ -429,7 +429,7 @@
"@drift/common@file:./drift-common/common-ts":
version "1.0.0"
dependencies:
"@drift-labs/sdk" "file:../Library/Caches/Yarn/v6/npm-@drift-common-1.0.0-886bdde9-d98f-4a7b-8526-a3c8414e8bbc-1715733801694/node_modules/@drift/protocol/sdk"
"@drift-labs/sdk" "file:../../Library/Caches/Yarn/v6/npm-@drift-common-1.0.0-e79f07a6-e675-4831-af0e-f2c460b62255-1718639556768/node_modules/@drift/protocol/sdk"
"@jest/globals" "^29.3.1"
"@slack/web-api" "^6.4.0"
"@solana/spl-token" "^0.3.8"
@@ -443,6 +443,7 @@
rxjs "^7.8.1"
tiny-invariant "^1.3.1"
tweetnacl "^1.0.3"
typescript "^5.4.5"
"@ellipsis-labs/phoenix-sdk@^1.4.2":
version "1.4.2"
@@ -5759,6 +5760,11 @@ typescript@4.5.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8"
integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg==
typescript@^5.4.5:
version "5.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.5.tgz#42ccef2c571fdbd0f6718b1d1f5e6e5ef006f611"
integrity sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==
uid2@0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/uid2/-/uid2-0.0.3.tgz#483126e11774df2f71b8b639dcd799c376162b82"