Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow redis connection for RelayerFillLimit #1324

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading