-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
indexer/packages/redis/src/caches/orderbook-mid-prices-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Callback, RedisClient } from 'redis'; | ||
|
||
import { | ||
addMarketPriceScript, | ||
getMarketMedianScript, | ||
} from './scripts'; | ||
|
||
// Cache of orderbook prices for each clob pair | ||
// Each price is cached for a 5 second window and in a ZSET | ||
export const ORDERBOOK_MID_PRICES_CACHE_KEY_PREFIX: string = 'v4/orderbook_mid_prices/'; | ||
|
||
function getOrderbookMidPriceCacheKey(ticker: string): string { | ||
return `${ORDERBOOK_MID_PRICES_CACHE_KEY_PREFIX}${ticker}`; | ||
} | ||
|
||
// Adds a price to the market prices cache | ||
export async function setPrice( | ||
client: RedisClient, | ||
ticker: string, | ||
price: string, | ||
): Promise<void> { | ||
// Number of keys for the lua script. | ||
const numKeys: number = 1; | ||
|
||
let evalAsync: ( | ||
marketCacheKey: string, | ||
) => Promise<void> = (marketCacheKey) => { | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
const callback: Callback<void> = ( | ||
err: Error | null, | ||
) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
}; | ||
|
||
const nowSeconds = Math.floor(Date.now() / 1000); // Current time in seconds | ||
client.evalsha( | ||
addMarketPriceScript.hash, | ||
numKeys, | ||
marketCacheKey, | ||
price, | ||
nowSeconds, | ||
callback, | ||
); | ||
|
||
}); | ||
}; | ||
evalAsync = evalAsync.bind(client); | ||
|
||
return evalAsync( | ||
getOrderbookMidPriceCacheKey(ticker), | ||
); | ||
} | ||
|
||
export async function getMedianPrice(client: RedisClient, ticker: string): Promise<string | null> { | ||
let evalAsync: ( | ||
marketCacheKey: string, | ||
) => Promise<string> = ( | ||
marketCacheKey, | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
const callback: Callback<string> = ( | ||
err: Error | null, | ||
results: string, | ||
) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(results); | ||
}; | ||
|
||
client.evalsha( | ||
getMarketMedianScript.hash, | ||
1, | ||
marketCacheKey, | ||
callback, | ||
); | ||
}); | ||
}; | ||
evalAsync = evalAsync.bind(client); | ||
|
||
return evalAsync( | ||
getOrderbookMidPriceCacheKey(ticker), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- Key for the ZSET storing price data | ||
local priceCacheKey = KEYS[1] | ||
-- Price to be added | ||
local price = tonumber(ARGV[1]) | ||
-- Current timestamp | ||
local nowSeconds = tonumber(ARGV[2]) | ||
-- Time window (5 seconds) | ||
local fiveSeconds = 5 | ||
|
||
-- 1. Add the price to the sorted set (score is the current timestamp) | ||
redis.call("zadd", priceCacheKey, nowSeconds, price) | ||
|
||
-- 2. Remove any entries older than 5 seconds | ||
local cutoffTime = nowSeconds - fiveSeconds | ||
redis.call("zremrangebyscore", priceCacheKey, "-inf", cutoffTime) | ||
|
||
return true |
23 changes: 23 additions & 0 deletions
23
indexer/packages/redis/src/scripts/get_market_median_price.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- Key for the sorted set storing price data | ||
local priceCacheKey = KEYS[1] | ||
|
||
-- Get all the prices from the sorted set (ascending order) | ||
local prices = redis.call('zrange', priceCacheKey, 0, -1) | ||
|
||
-- If no prices are found, return nil | ||
if #prices == 0 then | ||
return nil | ||
end | ||
|
||
-- Calculate the middle index | ||
local middle = math.floor(#prices / 2) | ||
|
||
-- Calculate median | ||
if #prices % 2 == 0 then | ||
-- If even, return the average of the two middle elements | ||
local median = (tonumber(prices[middle]) + tonumber(prices[middle + 1])) / 2 | ||
return tostring(median) | ||
else | ||
-- If odd, return the middle element | ||
return prices[middle + 1] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
indexer/services/roundtable/src/tasks/cache-orderbook-mid-prices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
logger, | ||
} from '@dydxprotocol-indexer/base'; | ||
import { | ||
PerpetualMarketFromDatabase, | ||
PerpetualMarketTable, | ||
} from '@dydxprotocol-indexer/postgres'; | ||
import { | ||
OrderbookMidPricesCache, | ||
OrderbookLevelsCache, | ||
} from '@dydxprotocol-indexer/redis'; | ||
|
||
import { redisClient } from '../helpers/redis'; | ||
|
||
/** | ||
* Instrument data on the orderbook to be used for analytics. | ||
*/ | ||
export default async function runTask(): Promise<void> { | ||
const markets: PerpetualMarketFromDatabase[] = await PerpetualMarketTable.findAll({}, []); | ||
|
||
for (const market of markets) { | ||
try { | ||
const price = await OrderbookLevelsCache.getOrderBookMidPrice(market.ticker, redisClient); | ||
if (price) { | ||
await OrderbookMidPricesCache.setPrice(redisClient, market.ticker, price); | ||
} else { | ||
logger.info({ | ||
at: 'cache-orderbook-mid-prices#runTask', | ||
message: `undefined price for ${market.ticker}`, | ||
}); | ||
} | ||
} catch (error) { | ||
logger.error({ | ||
at: 'cache-orderbook-mid-prices#runTask', | ||
message: error.msg, | ||
error, | ||
}); | ||
} | ||
} | ||
} |