From 006939ff455658cf2522a86e7cfeb4de08c7cbe7 Mon Sep 17 00:00:00 2001 From: james-a-morris Date: Wed, 11 Dec 2024 15:54:33 -0600 Subject: [PATCH] feat: allow redis connection for RelayerFillLimit Signed-off-by: james-a-morris --- api/_exclusivity/cache.ts | 93 +++++++++++++++++++++++++++++++++ api/_types/exclusivity.types.ts | 4 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 api/_exclusivity/cache.ts diff --git a/api/_exclusivity/cache.ts b/api/_exclusivity/cache.ts new file mode 100644 index 000000000..2f8f03680 --- /dev/null +++ b/api/_exclusivity/cache.ts @@ -0,0 +1,93 @@ +import { assert, is } from "superstruct"; +import { buildCacheKey, redisCache } from "../_cache"; +import { RelayerFillLimit, RelayerFillLimitArraySchema } from "../_types"; + +/** + * A constant caching prefix that will prepend all exclusivity keys. + */ +const EXCLUSIVITY_CACHE_PREFIX = "exclusivity"; + +/** + * Builds a cache key for relayer fill limit data. + * + * @param {string} relayer - The unique identifier for the relayer. + * @param {number | string} originChainId - The ID of the origin chain. + * @param {number | string} destinationChainId - The ID of the destination chain. + * @param {string} inputToken - The token being input. + * @param {string} outputToken - The token being output. + * @returns {string} A formatted cache key. + */ +function buildRelayerFillLimitCacheKey( + relayer: string, + originChainId: number | string, + destinationChainId: number | string, + inputToken: string, + outputToken: string +): string { + return buildCacheKey( + EXCLUSIVITY_CACHE_PREFIX, + relayer.toLowerCase(), + originChainId, + destinationChainId, + inputToken.toLowerCase(), + outputToken.toLowerCase() + ); +} + +/** + * Retrieves a series of fill limits specified by a relayer. + * + * @param {string} relayer - The unique identifier for the relayer. + * @param {number} originChainId - The ID of the origin chain. + * @param {number} destinationChainId - The ID of the destination chain. + * @param {string} inputToken - The token being input. + * @param {string} outputToken - The token being output. + * @returns {Promise} An array of relayer fill limits or null if none exist. + */ +export async function getCachedRelayerFillLimit( + relayer: string, + originChainId: number, + destinationChainId: number, + inputToken: string, + outputToken: string +): Promise { + const result = await redisCache.get( + buildRelayerFillLimitCacheKey( + relayer, + originChainId, + destinationChainId, + inputToken, + outputToken + ) + ); + return is(result, RelayerFillLimitArraySchema) ? result : null; +} + +/** + * Sets a series of relayer fill limits in the cache. + * + * @param {string} relayer - The unique identifier for the relayer. + * @param {RelayerFillLimit[]} entries - An array of relayer fill limit entries to store. + * @returns {Promise} A promise that resolves when the operation is complete. + */ +export async function setCachedRelayerFillLimit( + relayer: string, + entries: RelayerFillLimit[] +): Promise { + // Confirm what we're about to push to the cache is formatted properly + assert(entries, RelayerFillLimitArraySchema); + await Promise.all( + entries.map((entry) => + redisCache.set( + buildRelayerFillLimitCacheKey( + relayer, + entry.originChainId, + entry.destinationChainId, + entry.inputToken, + entry.outputToken + ), + entry + ) + ) + ); +} diff --git a/api/_types/exclusivity.types.ts b/api/_types/exclusivity.types.ts index d3c4858e2..c0288a6ac 100644 --- a/api/_types/exclusivity.types.ts +++ b/api/_types/exclusivity.types.ts @@ -1,4 +1,4 @@ -import { boolean, Infer, object, optional } from "superstruct"; +import { array, boolean, Infer, object, optional } from "superstruct"; import { positiveFloatStr, positiveIntStr, validAddress } from "../_utils"; export const RelayerFillLimitSchema = object({ @@ -14,6 +14,8 @@ export const RelayerFillLimitSchema = object({ msgFill: optional(boolean()), }); +export const RelayerFillLimitArraySchema = array(RelayerFillLimitSchema); + export type RelayerFillLimit = Infer; // // Example config.