Skip to content

fix(EventUtils): Refine sorting types #1003

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

Merged
merged 1 commit into from
Apr 30, 2025
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk",
"author": "UMA Team",
"version": "4.1.48",
"version": "4.1.49",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand Down
12 changes: 7 additions & 5 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,36 @@ export function spreadEventWithBlockNumber(event: Log): SortableEvent {
};
}

type PartialSortableEvent = Pick<SortableEvent, "blockNumber" | "logIndex">;

// This copies the array and sorts it, returning a new array with the new ordering.
export function sortEventsAscending<T extends SortableEvent>(events: T[]): T[] {
export function sortEventsAscending<T extends PartialSortableEvent>(events: T[]): T[] {
return sortEventsAscendingInPlace([...events]);
}

// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
// Note: this method should only be used in cases where modifications are acceptable.
export function sortEventsAscendingInPlace<T extends SortableEvent>(events: T[]): T[] {
export function sortEventsAscendingInPlace<T extends PartialSortableEvent>(events: T[]): T[] {
return events.sort((ex, ey) =>
ex.blockNumber === ey.blockNumber ? ex.logIndex - ey.logIndex : ex.blockNumber - ey.blockNumber
);
}

// This copies the array and sorts it, returning a new array with the new ordering.
export function sortEventsDescending<T extends SortableEvent>(events: T[]): T[] {
export function sortEventsDescending<T extends PartialSortableEvent>(events: T[]): T[] {
return sortEventsDescendingInPlace([...events]);
}

// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
// Note: this method should only be used in cases where modifications are acceptable.
export function sortEventsDescendingInPlace<T extends SortableEvent>(events: T[]): T[] {
export function sortEventsDescendingInPlace<T extends PartialSortableEvent>(events: T[]): T[] {
return events.sort((ex, ey) =>
ex.blockNumber === ey.blockNumber ? ey.logIndex - ex.logIndex : ey.blockNumber - ex.blockNumber
);
}

// Returns true if ex is older than ey.
export function isEventOlder<T extends SortableEvent>(ex: T, ey: T): boolean {
export function isEventOlder<T extends PartialSortableEvent>(ex: T, ey: T): boolean {
return ex.blockNumber === ey.blockNumber ? ex.logIndex < ey.logIndex : ex.blockNumber < ey.blockNumber;
}

Expand Down