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

Update format #1331

Merged
merged 5 commits into from
Dec 12, 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
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
Loading