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

Add scaffolding for config unpacking #1323

Merged
merged 13 commits into from
Dec 12, 2024
10 changes: 10 additions & 0 deletions api/_exclusivity/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from "ethers";
import { RelayerFillLimit } from "../_types";

export const MAX_MESSAGE_AGE_SECONDS = 300;

Expand All @@ -20,3 +21,12 @@ export const isTimestampValid = (
const currentTime = Math.floor(Date.now() / 1000);
return currentTime - timestamp <= maxAgeSeconds;
};

export async function updateLimits(
relayer: string,
limits: RelayerFillLimit[]
): Promise<void> {
relayer; // todo
limits; // todo
return;
}
30 changes: 22 additions & 8 deletions api/relayer-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import {
getWhiteListedRelayers,
isTimestampValid,
MAX_MESSAGE_AGE_SECONDS,
updateLimits,
} from "./_exclusivity/utils";
import { RelayerConfigUpdate, TypedRelayerConfigUpdateRequest } from "./_types";
import {
RelayerConfigUpdate,
RelayerFillLimitArraySchema,
TypedRelayerConfigUpdateRequest,
} from "./_types";

const handler = async (
request: TypedRelayerConfigUpdateRequest,
Expand All @@ -14,21 +19,30 @@ const handler = async (
if (request.method !== "POST") {
return response.status(405).end(`Method ${request.method} Not Allowed`);
}

const body = request.body as RelayerConfigUpdate;
const { authorization } = request.headers;
const { timestamp } = body;
const { relayerFillLimits, timestamp } = body;
if (!isTimestampValid(timestamp, MAX_MESSAGE_AGE_SECONDS)) {
return response.status(400).json({ message: "Message too old" });
}

if (
!authorization ||
!getWhiteListedRelayers().includes(
getRelayerFromSignature(authorization, JSON.stringify(body))
)
) {
if (!authorization) {
return response.status(401).json({ message: "Unauthorized" });
}
const relayer = getRelayerFromSignature(authorization, JSON.stringify(body));

if (!getWhiteListedRelayers().includes(relayer)) {
return response.status(401).json({ message: "Unauthorized" });
}

if (!RelayerFillLimitArraySchema.is(relayerFillLimits)) {
return response
.status(400)
.json({ message: "Invalid configuration payload" });
}

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

Expand Down
13 changes: 13 additions & 0 deletions test/api/relayer-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ describe("Relayer Config API", () => {
test("POST request with valid timestamp", async () => {
const message = {
timestamp: Date.now() / 1000,
relayerFillLimits: [
{
originChainId: "1",
inputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
destinationChainId: "42161",
outputToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
minOutputAmount: "1",
maxOutputAmount: "2",
balanceMultiplier: "1",
minProfitThreshold: "0.0001",
minExclusivityPeriod: "1",
},
],
};
const messageString = JSON.stringify(message);
const signature = await whitelistedRelayer.signMessage(messageString);
Expand Down
Loading