Skip to content

Commit

Permalink
Add scaffolding for config unpacking (#1323)
Browse files Browse the repository at this point in the history
* wip

* updates

Signed-off-by: james-a-morris <[email protected]>

* feat(api): add relayer config enpoint

Signed-off-by: Pablo Maldonado <[email protected]>

* feat(api): get relayer address and check whitelist

Signed-off-by: Pablo Maldonado <[email protected]>

* refactor: comments

Signed-off-by: Pablo Maldonado <[email protected]>

* Update api/relayer-config.ts

Co-authored-by: James Morris, MS <[email protected]>

* refactor: organise files

Signed-off-by: Pablo Maldonado <[email protected]>

* refactor: rename func

Signed-off-by: Pablo Maldonado <[email protected]>

* Add scaffolding for config unpacking

* Fixtest

---------

Signed-off-by: james-a-morris <[email protected]>
Signed-off-by: Pablo Maldonado <[email protected]>
Co-authored-by: james-a-morris <[email protected]>
Co-authored-by: Pablo Maldonado <[email protected]>
Co-authored-by: James Morris, MS <[email protected]>
  • Loading branch information
4 people authored Dec 12, 2024
1 parent 79b1d5d commit 470e309
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
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

0 comments on commit 470e309

Please sign in to comment.