-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Real-time incoming transactions are sorted incorrectly (#1720)
* Real-time incoming transactions are sorted incorrectly Fixes #1681 * fix checks workflow
- Loading branch information
Showing
5 changed files
with
127 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { Transaction } from 'types/api/transaction'; | ||
|
||
import sortTxs, { sortTxsFromSocket } from './sortTxs'; | ||
|
||
describe('sortTxs', () => { | ||
it('should sort transactions by value in descending order', () => { | ||
const txs = [ | ||
{ value: '42' }, | ||
{ value: '11' }, | ||
{ value: '24' }, | ||
] as Array<Transaction>; | ||
const result = txs.sort(sortTxs('value-desc')); | ||
expect(result).toEqual([ | ||
{ value: '42' }, | ||
{ value: '24' }, | ||
{ value: '11' }, | ||
]); | ||
}); | ||
|
||
it('should sort transactions by value in ascending order', () => { | ||
const txs = [ | ||
{ value: '42' }, | ||
{ value: '11' }, | ||
{ value: '24' }, | ||
] as Array<Transaction>; | ||
const result = txs.sort(sortTxs('value-asc')); | ||
expect(result).toEqual([ | ||
{ value: '11' }, | ||
{ value: '24' }, | ||
{ value: '42' }, | ||
]); | ||
}); | ||
|
||
it('should sort transactions by fee in descending order', () => { | ||
const txs = [ | ||
{ fee: { value: '42' } }, | ||
{ fee: { value: '11' } }, | ||
{ fee: { value: '24' } }, | ||
] as Array<Transaction>; | ||
const result = txs.sort(sortTxs('fee-desc')); | ||
expect(result).toEqual([ | ||
{ fee: { value: '42' } }, | ||
{ fee: { value: '24' } }, | ||
{ fee: { value: '11' } }, | ||
]); | ||
}); | ||
|
||
it('should sort transactions by fee in ascending order', () => { | ||
const txs = [ | ||
{ fee: { value: '42' } }, | ||
{ fee: { value: '11' } }, | ||
{ fee: { value: '24' } }, | ||
] as Array<Transaction>; | ||
const result = txs.sort(sortTxs('fee-asc')); | ||
expect(result).toEqual([ | ||
{ fee: { value: '11' } }, | ||
{ fee: { value: '24' } }, | ||
{ fee: { value: '42' } }, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('sortTxsFromSocket', () => { | ||
it('should sort transaction by age in ascending order if sorting is not provided', () => { | ||
const txs = [ | ||
{ timestamp: '2022-11-01T12:33:00Z' }, | ||
{ timestamp: '2022-11-01T12:00:00Z' }, | ||
{ timestamp: null }, | ||
{ timestamp: '2022-11-03T03:03:00Z' }, | ||
] as Array<Transaction>; | ||
const result = txs.sort(sortTxsFromSocket(undefined)); | ||
expect(result).toEqual([ | ||
{ timestamp: null }, | ||
{ timestamp: '2022-11-03T03:03:00Z' }, | ||
{ timestamp: '2022-11-01T12:33:00Z' }, | ||
{ timestamp: '2022-11-01T12:00:00Z' }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { Transaction, TransactionsSortingValue } from 'types/api/transaction'; | ||
|
||
import compareBns from 'lib/bigint/compareBns'; | ||
|
||
export default function sortTxs(sorting: TransactionsSortingValue | undefined) { | ||
return function sortingFn(tx1: Transaction, tx2: Transaction) { | ||
switch (sorting) { | ||
case 'value-desc': | ||
return compareBns(tx2.value, tx1.value); | ||
case 'value-asc': | ||
return compareBns(tx1.value, tx2.value); | ||
case 'fee-desc': | ||
return compareBns(tx2.fee.value || 0, tx1.fee.value || 0); | ||
case 'fee-asc': | ||
return compareBns(tx1.fee.value || 0, tx2.fee.value || 0); | ||
default: | ||
return 0; | ||
} | ||
}; | ||
} | ||
|
||
export function sortTxsFromSocket(sorting: TransactionsSortingValue | undefined) { | ||
if (sorting) { | ||
return sortTxs(sorting); | ||
} | ||
|
||
return function sortingFn(tx1: Transaction, tx2: Transaction) { | ||
if (!tx1.timestamp) { | ||
return -1; | ||
} | ||
|
||
if (!tx2.timestamp) { | ||
return 1; | ||
} | ||
|
||
return tx2.timestamp.localeCompare(tx1.timestamp); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters