@@ -38,9 +38,7 @@ import { distinctBlock, signedTxsEquals, transactionsEquals, txInEquals } from '
3838import { WitnessedTx } from '@cardano-sdk/key-management' ;
3939import { newAndStoredMulticast } from './util/newAndStoredMulticast' ;
4040import chunk from 'lodash/chunk.js' ;
41- import intersectionBy from 'lodash/intersectionBy.js' ;
4241import sortBy from 'lodash/sortBy.js' ;
43- import unionBy from 'lodash/unionBy.js' ;
4442
4543export interface TransactionsTrackerProps {
4644 chainHistoryProvider : ChainHistoryProvider ;
@@ -127,6 +125,7 @@ export const createAddressTransactionsProvider = ({
127125 )
128126 ) ,
129127 combineLatest ( [ addresses$ , storedTransactions$ . pipe ( defaultIfEmpty ( [ ] as Cardano . HydratedTx [ ] ) ) ] ) . pipe (
128+ // eslint-disable-next-line sonarjs/cognitive-complexity
130129 switchMap ( ( [ addresses , storedTransactions ] ) => {
131130 let localTransactions : Cardano . HydratedTx [ ] = [ ...storedTransactions ] ;
132131
@@ -158,28 +157,21 @@ export const createAddressTransactionsProvider = ({
158157 `chainHistoryProvider returned ${ newTransactions . length } transactions` ,
159158 lowerBound !== undefined && `since block ${ lowerBound } `
160159 ) ;
161- const duplicateTransactions =
162- lastStoredTransaction && intersectionBy ( localTransactions , newTransactions , ( tx ) => tx . id ) ;
163-
164- if ( typeof duplicateTransactions !== 'undefined' && duplicateTransactions . length === 0 ) {
165- const rollbackTransactions = localTransactions . filter (
166- ( { blockHeader : { blockNo } } ) => blockNo >= lowerBound
167- ) ;
168-
169- from ( rollbackTransactions )
170- . pipe ( tap ( ( tx ) => logger . debug ( `Transaction ${ tx . id } was rolled back` ) ) )
171- . subscribe ( ( v ) => rollback$ . next ( v ) ) ;
160+ const txRolledBack = lastStoredTransaction && newTransactions [ 0 ] ?. id !== lastStoredTransaction . id ;
161+ if ( txRolledBack ) {
162+ logger . debug ( `Transaction ${ lastStoredTransaction . id } was rolled back` ) ;
163+ rollback$ . next ( lastStoredTransaction ) ;
172164
173165 // Rollback by 1 block, try again in next loop iteration
174- localTransactions = localTransactions . filter ( ( { blockHeader : { blockNo } } ) => blockNo < lowerBound ) ;
166+ localTransactions . pop ( ) ;
175167 } else {
176- const lastLocalTxs = localTransactions . slice ( - newTransactions . length ) ;
177- const areTransactionsSame =
178- lastLocalTxs . length === newTransactions . length &&
179- lastLocalTxs . every ( ( tx , index ) => tx . id === newTransactions [ index ] . id ) ;
168+ if ( lastStoredTransaction ) {
169+ // lastStoredTransaction is also the first in newTransactions
170+ newTransactions . shift ( ) ;
171+ }
180172
181- if ( ! areTransactionsSame ) {
182- localTransactions = unionBy ( localTransactions , newTransactions , ( tx ) => tx . id ) ;
173+ localTransactions = [ ... localTransactions , ... newTransactions ] ;
174+ if ( newTransactions . length > 0 ) {
183175 store . setAll ( localTransactions ) ;
184176 }
185177
0 commit comments