Commit d833ceb5 authored by tom goriunov's avatar tom goriunov Committed by GitHub

bugfix: Real-time incoming transactions are sorted incorrectly (#1720)

* Real-time incoming transactions are sorted incorrectly

Fixes #1681

* fix checks workflow
parent 7b8d8d5a
...@@ -95,6 +95,8 @@ jobs: ...@@ -95,6 +95,8 @@ jobs:
steps: steps:
- name: Checkout repo - name: Checkout repo
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup node - name: Setup node
uses: actions/setup-node@v4 uses: actions/setup-node@v4
...@@ -115,7 +117,7 @@ jobs: ...@@ -115,7 +117,7 @@ jobs:
run: yarn --frozen-lockfile --ignore-optional run: yarn --frozen-lockfile --ignore-optional
- name: Run Jest - name: Run Jest
run: yarn test:jest --onlyChanged=${{ github.event_name == 'pull_request' }} --passWithNoTests run: yarn test:jest ${{ github.event_name == 'pull_request' && '--changedSince=origin/main' || '' }} --passWithNoTests
pw_affected_tests: pw_affected_tests:
name: Resolve affected Playwright tests name: Resolve affected Playwright tests
......
...@@ -20,6 +20,7 @@ import Pagination from 'ui/shared/pagination/Pagination'; ...@@ -20,6 +20,7 @@ import Pagination from 'ui/shared/pagination/Pagination';
import useQueryWithPages from 'ui/shared/pagination/useQueryWithPages'; import useQueryWithPages from 'ui/shared/pagination/useQueryWithPages';
import getSortParamsFromValue from 'ui/shared/sort/getSortParamsFromValue'; import getSortParamsFromValue from 'ui/shared/sort/getSortParamsFromValue';
import getSortValueFromQuery from 'ui/shared/sort/getSortValueFromQuery'; import getSortValueFromQuery from 'ui/shared/sort/getSortValueFromQuery';
import { sortTxsFromSocket } from 'ui/txs/sortTxs';
import TxsWithAPISorting from 'ui/txs/TxsWithAPISorting'; import TxsWithAPISorting from 'ui/txs/TxsWithAPISorting';
import { SORT_OPTIONS } from 'ui/txs/useTxsSort'; import { SORT_OPTIONS } from 'ui/txs/useTxsSort';
...@@ -85,7 +86,7 @@ const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT }: Props) => { ...@@ -85,7 +86,7 @@ const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT }: Props) => {
addressTxsQuery.onFilterChange({ filter: newVal }); addressTxsQuery.onFilterChange({ filter: newVal });
}, [ addressTxsQuery ]); }, [ addressTxsQuery ]);
const handleNewSocketMessage: SocketMessage.AddressTxs['handler'] = (payload) => { const handleNewSocketMessage: SocketMessage.AddressTxs['handler'] = React.useCallback((payload) => {
setSocketAlert(''); setSocketAlert('');
queryClient.setQueryData( queryClient.setQueryData(
...@@ -123,10 +124,10 @@ const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT }: Props) => { ...@@ -123,10 +124,10 @@ const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT }: Props) => {
items: [ items: [
...newItems, ...newItems,
...prevData.items, ...prevData.items,
], ].sort(sortTxsFromSocket(sort)),
}; };
}); });
}; }, [ currentAddress, filterValue, overloadCount, queryClient, sort ]);
const handleSocketClose = React.useCallback(() => { const handleSocketClose = React.useCallback(() => {
setSocketAlert('Connection is lost. Please refresh the page to load new transactions.'); setSocketAlert('Connection is lost. Please refresh the page to load new transactions.');
......
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' },
]);
});
});
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);
};
}
import type { UseQueryResult } from '@tanstack/react-query'; import type { UseQueryResult } from '@tanstack/react-query';
import React from 'react'; import React from 'react';
import type { Transaction, TransactionsSortingValue, TxsResponse } from 'types/api/transaction'; import type { TransactionsSortingValue, TxsResponse } from 'types/api/transaction';
import type { ResourceError } from 'lib/api/resources'; import type { ResourceError } from 'lib/api/resources';
import compareBns from 'lib/bigint/compareBns';
import * as cookies from 'lib/cookies'; import * as cookies from 'lib/cookies';
import type { Option } from 'ui/shared/sort/Sort'; import type { Option } from 'ui/shared/sort/Sort';
import sortTxs from './sortTxs';
export const SORT_OPTIONS: Array<Option<TransactionsSortingValue>> = [ export const SORT_OPTIONS: Array<Option<TransactionsSortingValue>> = [
{ title: 'Default', id: undefined }, { title: 'Default', id: undefined },
{ title: 'Value ascending', id: 'value-asc' }, { title: 'Value ascending', id: 'value-asc' },
...@@ -23,21 +24,6 @@ type HookResult = UseQueryResult<TxsResponse, ResourceError<unknown>> & { ...@@ -23,21 +24,6 @@ type HookResult = UseQueryResult<TxsResponse, ResourceError<unknown>> & {
setSortByValue: (value: SortingValue) => void; setSortByValue: (value: SortingValue) => void;
} }
const sortTxs = (sorting: SortingValue) => (tx1: Transaction, tx2: Transaction) => {
switch (sorting) {
case 'value-desc':
return compareBns(tx1.value, tx2.value);
case 'value-asc':
return compareBns(tx2.value, tx1.value);
case 'fee-desc':
return compareBns(tx1.fee.value || 0, tx2.fee.value || 0);
case 'fee-asc':
return compareBns(tx2.fee.value || 0, tx1.fee.value || 0);
default:
return 0;
}
};
export default function useTxsSort( export default function useTxsSort(
queryResult: UseQueryResult<TxsResponse, ResourceError<unknown>>, queryResult: UseQueryResult<TxsResponse, ResourceError<unknown>>,
): HookResult { ): HookResult {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment