import { Odds } from "@prisma/client"; import type { RedisClient } from "../../config"; import { type Bytes32 } from "../../../core"; import { type SnapShotReadState } from "../types"; /** * Updates the round-robin assignment of a market's price stream to a Redis set. * * This function manages the distribution of market price streams across a fixed number of Redis sets, * using a round-robin strategy to balance load and ensure even distribution. It performs the following steps atomically: * 1. Retrieves the current round-robin index from Redis. * 2. Calculates the next index, wrapping around based on `LE_STREAMS_SETS_MAX_COUNT`. * 3. Adds the given `marketId` to the corresponding Redis set for the next round. * 4. Updates the round-robin index in Redis. * 5. Maps the `marketId` to its assigned set index in a Redis hash for quick lookup. * * All Redis operations are executed in a single pipeline to ensure atomicity and performance. * * @param redis - The Redis client instance used for executing commands. * @param marketId - The unique identifier for the market, represented as a 16-byte value. * @returns A promise that resolves when the operation is complete. * * @remarks * - Assumes that `LE_STREAMS_SETS_MAX_COUNT`, `lastUpdatedStreamSetCountKey`, `marketsBeingWatched`, `PRICE_STREAM_KEY`, * and `MARKET_TO_SET_ID` are defined and accessible in the module scope. * - Ensures that each market is always assigned to a set, and the mapping is kept up-to-date for efficient retrieval. * - Designed for high-throughput environments where market assignments need to be balanced dynamically. */ export declare const updateStreamSetsRoundRobin: (redis: RedisClient, marketId: Bytes32) => Promise; /** * Removes a market's price stream from its associated Redis set and deletes its mapping. * * This function performs the following steps atomically using a Redis pipeline: * 1. Retrieves the set ID associated with the given `marketId` from the `MARKET_TO_SET_ID` hash. * 2. If the mapping does not exist, logs a warning and exits. * 3. Removes the market's price stream key from the corresponding watched set. * 4. Deletes the mapping of `marketId` from the `MARKET_TO_SET_ID` hash. * 5. Executes the pipeline and checks for errors, logging and rethrowing if any occur. * 6. Logs successful removal of the market from the set. * * @param redis - The Redis client instance used for executing commands. * @param marketId - The unique identifier for the market whose stream should be removed. * @returns A promise that resolves when the operation is complete. * @throws If any Redis operation in the pipeline fails, the error is logged and rethrown. * * @remarks * - Ensures atomicity and consistency by using Redis pipelines. * - Designed for use in environments where market-to-set mappings are dynamically managed. * - Logging is performed at both warning and error levels for observability. */ export declare const removeStreamFromSet: (redis: RedisClient, marketId: Bytes32) => Promise; export declare const streamsOfMarketsBeingWatched: (redis: RedisClient, Id: number) => Promise; /** * Adds an array of price objects to a Redis stream for a specific market. * * This function ensures the Redis consumer group for the price stream exists, * creating it if necessary. It then adds each price to the stream using a pipeline * for efficiency, enforcing a maximum stream length. The first entry in the stream * is guaranteed to create the stream if it does not exist. If no prices are provided, * the function logs and exits early. * * @param redis - The Redis client instance used for stream operations. * @param prices - An array of Odds objects to be added to the stream. * @param marketId - The unique identifier for the market, used to derive the stream key. * * @remarks * - The function is idempotent with respect to consumer group creation; it will not throw * if the group already exists. * - Each price is validated against the OddsSchema before being added. * - Uses Redis pipelining for batch efficiency. * - Logs the number of prices added for observability. * * @throws * Throws any unexpected errors encountered during consumer group creation or pipeline execution. */ export declare const addPrices: (redis: RedisClient, prices: Odds[], marketId: Bytes32) => Promise; /** * Deletes the price stream associated with a specific market from Redis. * * This function performs two main operations: * 1. Removes the stream reference from the set of active streams for the given market. * 2. Deletes the actual Redis key that stores the price stream data for the market. * * @param redis - An instance of the Redis client used to perform cache operations. * @param marketId - The unique identifier (Bytes16) of the market whose price stream should be deleted. * @returns A promise that resolves when the stream and its references have been fully removed. * * @remarks * - Use this function when a market is being decommissioned or its price stream is no longer required. * - Ensure that no other process is concurrently writing to the same stream to avoid race conditions. */ export declare const deleteStream: (redis: RedisClient, marketId: Bytes32) => Promise; /** * Retrieves and aggregates all odds data from a Redis stream for a given market. * * This function iteratively reads batches of messages from the specified Redis stream, * deserializes each message into an `Odds` object, and accumulates the results. It continues * fetching until the stream is exhausted or an error occurs. The function also tracks the * snapshot state, marking it as `"INCOMPLETE"` if any error is encountered during the read process. * * @param redis - An instance of the Redis client used to interact with the stream. * @param marketId - The unique identifier for the market whose price stream is being read. * @returns An object containing: * - `odds`: An array of all `Odds` objects retrieved from the stream. * - `snapshotState`: The final state of the snapshot read operation, either `"COMPLETE"` or `"INCOMPLETE"`. * * @remarks * - The function reads the stream in batches to optimize memory usage and performance. * - If the stream contains no messages, an empty array is returned with a `"COMPLETE"` state. * - Errors during stream processing are logged and result in an `"INCOMPLETE"` snapshot state. * - The function assumes that the second field in each stream entry contains a JSON-encoded `Odds` object. * * @throws Never throws; errors are caught internally and reflected in the `snapshotState`. */ export declare const getStreamData: (redis: RedisClient, marketId: Bytes32) => Promise<{ odds: Odds[]; snapshotState: SnapShotReadState; }>; /** * Sets the change-over active marker in Redis. */ export declare const setChangeOverActive: (redis: RedisClient, status: boolean) => Promise; /** * Checks whether the changeover for active liquidation is enabled in Redis. */ export declare const checkChangeOverActive: (redis: RedisClient) => Promise;