Skip to content

Commit

Permalink
Merge pull request #313 from drift-labs/nour/no-market-fix
Browse files Browse the repository at this point in the history
no market fix
  • Loading branch information
NourAlharithi authored Dec 10, 2024
2 parents 8cae888 + 91c125f commit e15b8d7
Showing 1 changed file with 73 additions and 24 deletions.
97 changes: 73 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,29 +546,48 @@ const main = async (): Promise<void> => {
selectMostRecentBySlot
);
const depthToUse = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
redisL2['bids'] = redisL2['bids']?.slice(0, depthToUse);
redisL2['asks'] = redisL2['asks']?.slice(0, depthToUse);
let cacheMiss = true;
if (redisL2) {
l2Formatted = JSON.stringify(redisL2);
}

if (l2Formatted) {
cacheHitCounter.add(1, {
miss: false,
path: req.baseUrl + req.path,
});
res.writeHead(200);
res.end(l2Formatted);
return;
cacheMiss = false;
redisL2['bids'] = redisL2['bids']?.slice(0, depthToUse);
redisL2['asks'] = redisL2['asks']?.slice(0, depthToUse);
l2Formatted = redisL2;
} else {
cacheHitCounter.add(1, {
miss: true,
path: req.baseUrl + req.path,
});
res.writeHead(500);
res.end('No dlob data found');
return;
console.log(
`No L2 found for ${getVariant(
normedMarketType
)} market ${normedMarketIndex}`
);
const oracleData = isSpot
? driftClient.getOracleDataForSpotMarket(normedMarketIndex)
: driftClient.getOracleDataForPerpMarket(normedMarketIndex);
l2Formatted = {
bids: [],
asks: [],
marketType: normedMarketType,
marketIndex: normedMarketIndex,
marketName: undefined,
slot: dlobProvider.getSlot(),
oracle: oracleData.price.toNumber(),
oracleData: {
price: oracleData.price.toNumber(),
slot: oracleData.slot.toNumber(),
confidence: oracleData.confidence.toNumber(),
hasSufficientNumberOfDataPoints: true,
twap: oracleData.twap.toNumber(),
twapConfidence: oracleData.twapConfidence.toNumber(),
},
ts: Date.now(),
marketSlot: dlobProvider.getSlot(),
};
}
cacheHitCounter.add(1, {
miss: cacheMiss,
path: req.baseUrl + req.path,
});
res.writeHead(200);
res.end(JSON.stringify(l2Formatted));
return;
} catch (err) {
next(err);
}
Expand Down Expand Up @@ -636,20 +655,50 @@ const main = async (): Promise<void> => {
selectMostRecentBySlot
);
const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
redisL2['bids'] = redisL2['bids']?.slice(0, depth);
redisL2['asks'] = redisL2['asks']?.slice(0, depth);
let cacheMiss = true;
if (redisL2) {
cacheMiss = false;
redisL2['bids'] = redisL2['bids']?.slice(0, depth);
redisL2['asks'] = redisL2['asks']?.slice(0, depth);
l2Formatted = redisL2;
} else {
console.log(
`No L2 found for ${getVariant(
normedMarketType
)} market ${normedMarketIndex}`
);
const oracleData = isSpot
? driftClient.getOracleDataForSpotMarket(normedMarketIndex)
: driftClient.getOracleDataForPerpMarket(normedMarketIndex);
l2Formatted = {
bids: [],
asks: [],
marketType: normedMarketType,
marketIndex: normedMarketIndex,
marketName: undefined,
slot: dlobProvider.getSlot(),
oracle: oracleData.price.toNumber(),
oracleData: {
price: oracleData.price.toNumber(),
slot: oracleData.slot.toNumber(),
confidence: oracleData.confidence.toNumber(),
hasSufficientNumberOfDataPoints: true,
twap: oracleData.twap.toNumber(),
twapConfidence: oracleData.twapConfidence.toNumber(),
},
ts: Date.now(),
marketSlot: dlobProvider.getSlot(),
};
}

if (l2Formatted) {
cacheHitCounter.add(1, {
miss: false,
miss: cacheMiss,
path: req.baseUrl + req.path,
});
} else {
cacheHitCounter.add(1, {
miss: true,
miss: cacheMiss,
path: req.baseUrl + req.path,
});
}
Expand Down

0 comments on commit e15b8d7

Please sign in to comment.