Skip to content

Commit

Permalink
feat: allow redis connection for RelayerFillLimit
Browse files Browse the repository at this point in the history
Signed-off-by: james-a-morris <[email protected]>
  • Loading branch information
james-a-morris committed Dec 11, 2024
1 parent 1e2a32d commit 006939f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
93 changes: 93 additions & 0 deletions api/_exclusivity/cache.ts
Original file line number Diff line number Diff line change
@@ -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<RelayerFillLimit[] | null>} 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<RelayerFillLimit[] | null> {
const result = await redisCache.get<RelayerFillLimit[]>(
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<void>} A promise that resolves when the operation is complete.
*/
export async function setCachedRelayerFillLimit(
relayer: string,
entries: RelayerFillLimit[]
): Promise<void> {
// 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
)
)
);
}
4 changes: 3 additions & 1 deletion api/_types/exclusivity.types.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -14,6 +14,8 @@ export const RelayerFillLimitSchema = object({
msgFill: optional(boolean()),
});

export const RelayerFillLimitArraySchema = array(RelayerFillLimitSchema);

export type RelayerFillLimit = Infer<typeof RelayerFillLimitSchema>;

// // Example config.
Expand Down

0 comments on commit 006939f

Please sign in to comment.