Skip to content

Commit 69de106

Browse files
authored
refactor(EventUtils): Skip transactionIndex check in event sorting (#991)
logIndex is unique per block, so there's no need to consider transactionIndex when sorting.
1 parent db197b0 commit 69de106

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

src/utils/EventUtils.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,9 @@ export function sortEventsAscending<T extends SortableEvent>(events: T[]): T[] {
222222
// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
223223
// Note: this method should only be used in cases where modifications are acceptable.
224224
export function sortEventsAscendingInPlace<T extends SortableEvent>(events: T[]): T[] {
225-
return events.sort((ex, ey) => {
226-
if (ex.blockNumber !== ey.blockNumber) {
227-
return ex.blockNumber - ey.blockNumber;
228-
}
229-
if (ex.transactionIndex !== ey.transactionIndex) {
230-
return ex.transactionIndex - ey.transactionIndex;
231-
}
232-
return ex.logIndex - ey.logIndex;
233-
});
225+
return events.sort((ex, ey) =>
226+
ex.blockNumber === ey.blockNumber ? ex.logIndex - ey.logIndex : ex.blockNumber - ey.blockNumber
227+
);
234228
}
235229

236230
// This copies the array and sorts it, returning a new array with the new ordering.
@@ -241,26 +235,14 @@ export function sortEventsDescending<T extends SortableEvent>(events: T[]): T[]
241235
// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
242236
// Note: this method should only be used in cases where modifications are acceptable.
243237
export function sortEventsDescendingInPlace<T extends SortableEvent>(events: T[]): T[] {
244-
return events.sort((ex, ey) => {
245-
if (ex.blockNumber !== ey.blockNumber) {
246-
return ey.blockNumber - ex.blockNumber;
247-
}
248-
if (ex.transactionIndex !== ey.transactionIndex) {
249-
return ey.transactionIndex - ex.transactionIndex;
250-
}
251-
return ey.logIndex - ex.logIndex;
252-
});
238+
return events.sort((ex, ey) =>
239+
ex.blockNumber === ey.blockNumber ? ey.logIndex - ex.logIndex : ey.blockNumber - ex.blockNumber
240+
);
253241
}
254242

255243
// Returns true if ex is older than ey.
256244
export function isEventOlder<T extends SortableEvent>(ex: T, ey: T): boolean {
257-
if (ex.blockNumber !== ey.blockNumber) {
258-
return ex.blockNumber < ey.blockNumber;
259-
}
260-
if (ex.transactionIndex !== ey.transactionIndex) {
261-
return ex.transactionIndex < ey.transactionIndex;
262-
}
263-
return ex.logIndex < ey.logIndex;
245+
return ex.blockNumber === ey.blockNumber ? ex.logIndex < ey.logIndex : ex.blockNumber < ey.blockNumber;
264246
}
265247

266248
export function getTransactionHashes(events: SortableEvent[]): string[] {

0 commit comments

Comments
 (0)