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 basic sorting/parsing #1328

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from 2 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
34 changes: 34 additions & 0 deletions api/_exclusivity/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ethers } from "ethers";
import { bnZero } from "../../src/utils/sdk";
import { RelayerFillLimit } from "../_types";

export const MAX_MESSAGE_AGE_SECONDS = 300;

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

export async function updateLimits(
relayer: string,
limits: RelayerFillLimit[]
): Promise<void> {
const sortedLimits = limits
.map(({ minOutputAmount, maxOutputAmount, ...rest }) => ({
minOutputAmount: ethers.BigNumber.from(minOutputAmount),
maxOutputAmount: ethers.BigNumber.from(maxOutputAmount),
...rest,
}))
.sort(({ minOutputAmount: minA }, { minOutputAmount: minB }) =>
minA.sub(minB).gte(bnZero) ? 1 : -1
);

const sorted = sortedLimits
.slice(1)
.every(({ minOutputAmount, maxOutputAmount }, idx) => {
const { maxOutputAmount: prevMax } = sortedLimits[idx];
return maxOutputAmount.gt(minOutputAmount) && minOutputAmount.gt(prevMax);
});

if (!sorted) {
throw new Error("Relayer limits are overlapping");
}

// todo: Push each limit entry to the backend cache/db.
// The config types need to be reverted to strings as numbers.
relayer;

return;
}
Loading