Skip to content

Commit

Permalink
Update format (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxrl authored Dec 12, 2024
1 parent b6d0b7b commit 8745c89
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
18 changes: 13 additions & 5 deletions api/_exclusivity/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ export async function getCachedRelayerFillLimit(
* Sets a series of relayer fill limits in the cache.
*
* @param {string} relayer - The unique identifier for the relayer.
* @param {originChainId} originChainId - The origin chain of the route.
* @param {inputToken} inputToken - The input token on the origin chain.
* @param {destinationChainId} destinationChainId - The destination chain of the route.
* @param {outputToken} outputToken - The output token on the destination chain.
* @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,
originChainId: number,
inputToken: string,
destinationChainId: number,
outputToken: string,
entries: RelayerFillLimit[]
): Promise<void> {
// Confirm what we're about to push to the cache is formatted properly
Expand All @@ -81,13 +89,13 @@ export async function setCachedRelayerFillLimit(
redisCache.set(
buildRelayerFillLimitCacheKey(
relayer,
entry.originChainId,
entry.destinationChainId,
entry.inputToken,
entry.outputToken
originChainId,
destinationChainId,
inputToken,
outputToken
),
entry,
120
600
)
)
);
Expand Down
8 changes: 8 additions & 0 deletions api/_exclusivity/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const isTimestampValid = (

export async function updateLimits(
relayer: string,
originChainId: number,
inputToken: string,
destinationChainId: number,
outputToken: string,
limits: RelayerFillLimit[]
): Promise<void> {
const sortedLimits = limits
Expand All @@ -70,6 +74,10 @@ export async function updateLimits(

await setCachedRelayerFillLimit(
relayer,
originChainId,
inputToken,
destinationChainId,
outputToken,
sortedLimits.map(({ minOutputAmount, maxOutputAmount, ...rest }) => ({
minOutputAmount: minOutputAmount.toString(), // @todo: Less bodge
maxOutputAmount: maxOutputAmount.toString(), // @todo: Less bodge
Expand Down
15 changes: 13 additions & 2 deletions api/_types/exclusivity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
} from "../_utils";
import { TypedVercelRequest } from "./generic.types";

export const RelayerFillLimitSchema = object({
export const RelayerRoute = object({
originChainId: positiveIntStr(),
destinationChainId: positiveIntStr(),
inputToken: validAddress(),
destinationChainId: positiveIntStr(),
outputToken: validAddress(),
});

export const RelayerFillLimitSchema = object({
minOutputAmount: positiveIntStr(),
maxOutputAmount: positiveIntStr(),
minProfitThreshold: positiveFloatStr(),
Expand All @@ -21,11 +24,19 @@ export const RelayerFillLimitSchema = object({
});

export const RelayerFillLimitArraySchema = array(RelayerFillLimitSchema);
export const RelayerRouteUpdateSchema = object({
RelayerRoute,
RelayerFillLimitArraySchema,
});

export type RelayerFillLimit = Infer<typeof RelayerFillLimitSchema>;

export type RelayerConfigUpdate = {
timestamp: number;
originChainId: string;
inputToken: string;
destinationChainId: string;
outputToken: string;
relayerFillLimits: RelayerFillLimit[];
};

Expand Down
18 changes: 16 additions & 2 deletions api/relayer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ const handlePost = async (
) => {
const body = request.body as RelayerConfigUpdate;
const { authorization } = request.headers;
const { relayerFillLimits, timestamp } = body;
const {
originChainId,
destinationChainId,
inputToken,
outputToken,
relayerFillLimits,
timestamp,
} = body;

if (!isTimestampValid(timestamp, MAX_MESSAGE_AGE_SECONDS)) {
return response.status(400).json({ message: "Message too old" });
Expand All @@ -81,7 +88,14 @@ const handlePost = async (
.json({ message: "Invalid configuration payload" });
}

await updateLimits(relayer, relayerFillLimits);
await updateLimits(
relayer,
Number(originChainId),
inputToken,
Number(destinationChainId),
outputToken,
relayerFillLimits
);
return response.status(200).json({ message: "POST request received" });
};

Expand Down
20 changes: 12 additions & 8 deletions test/api/relayer-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ describe("Relayer Config API", () => {
test("POST request with valid timestamp", async () => {
const message: RelayerConfigUpdate = {
timestamp: Math.floor(Date.now() / 1000),
originChainId: "1",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
destinationChainId: "42161",
outputToken: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
relayerFillLimits: [
{
originChainId: "1",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
destinationChainId: "42161",
outputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
minOutputAmount: "1",
maxOutputAmount: "2",
balanceMultiplier: "1",
Expand Down Expand Up @@ -67,6 +67,10 @@ describe("Relayer Config API", () => {
test("POST request with invalid timestamp", async () => {
const message: RelayerConfigUpdate = {
timestamp: Math.floor(Date.now() / 1000) - MAX_MESSAGE_AGE_SECONDS - 1,
originChainId: "1",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
destinationChainId: "10",
outputToken: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
relayerFillLimits: [],
};
const signature = await whitelistedRelayer.signMessage(
Expand All @@ -90,6 +94,10 @@ describe("Relayer Config API", () => {
test("POST request with invalid signature", async () => {
const message: RelayerConfigUpdate = {
timestamp: Math.floor(Date.now() / 1000),
originChainId: "1",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
destinationChainId: "10",
outputToken: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
relayerFillLimits: [],
};
const signature = await unauthorizedRelayer.signMessage(
Expand Down Expand Up @@ -133,10 +141,6 @@ describe("Relayer Config API", () => {
// Mock getLimits to return some test data
const limits: RelayerFillLimit[] = [
{
originChainId: "1",
destinationChainId: "42161",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
outputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
minOutputAmount: "1",
maxOutputAmount: "2",
balanceMultiplier: "1",
Expand Down

0 comments on commit 8745c89

Please sign in to comment.