@@ -40,6 +40,8 @@ import { newAndStoredMulticast } from './util/newAndStoredMulticast';
40
40
import chunk from 'lodash/chunk.js' ;
41
41
import sortBy from 'lodash/sortBy.js' ;
42
42
43
+ const ONE_MONTH_BLOCK_TIME = 21_600 * 6 ;
44
+
43
45
export interface TransactionsTrackerProps {
44
46
chainHistoryProvider : ChainHistoryProvider ;
45
47
addresses$ : Observable < Cardano . PaymentAddress [ ] > ;
@@ -140,7 +142,7 @@ const allTransactionsByAddresses = async (
140
142
141
143
startAt += PAGE_SIZE ;
142
144
response = [ ...response , ...pageResults ] ;
143
- } while ( pageResults . length == = PAGE_SIZE ) ;
145
+ } while ( pageResults . length > = PAGE_SIZE ) ;
144
146
} else {
145
147
const txes = await chainHistoryProvider . transactionsByAddresses ( {
146
148
addresses : addressGroup ,
@@ -201,6 +203,43 @@ export const revertLastBlock = (
201
203
return deduplicateSortedArray ( result , txEquals ) ;
202
204
} ;
203
205
206
+ /**
207
+ * Fetches the last `historicalTransactionsFetchLimit` transactions for a set of addresses.
208
+ * If there is a single address, it returns the most recent ones up to `historicalTransactionsFetchLimit`. If there
209
+ * are more than one address, it returns all transaction from all addresses, one month back from the most recent transaction.
210
+ *
211
+ * @param {ChainHistoryProvider } chainHistoryProvider - The chain history provider used to fetch transaction history.
212
+ * @param {Cardano.PaymentAddress[] } addresses - A list of Cardano payment addresses to fetch transactions for.
213
+ * @param {number } historicalTransactionsFetchLimit - The maximum number of transactions to fetch in the initial pass.
214
+ * @returns {Promise<Cardano.HydratedTx[]> } A promise that resolves to a list of hydrated transactions from the given addresses.
215
+ */
216
+ const fetchInitialTransactions = async (
217
+ chainHistoryProvider : ChainHistoryProvider ,
218
+ addresses : Cardano . PaymentAddress [ ] ,
219
+ historicalTransactionsFetchLimit : number
220
+ ) : Promise < Cardano . HydratedTx [ ] > => {
221
+ const firstPassTxs = await allTransactionsByAddresses ( chainHistoryProvider , {
222
+ addresses,
223
+ filterBy : { limit : historicalTransactionsFetchLimit , type : 'tip' }
224
+ } ) ;
225
+
226
+ if ( firstPassTxs . length === 0 ) {
227
+ return [ ] ;
228
+ }
229
+
230
+ if ( addresses . length === 1 ) {
231
+ return firstPassTxs ;
232
+ }
233
+
234
+ const highBlockNo = Cardano . BlockNo ( Math . max ( ...firstPassTxs . map ( ( tx ) => tx . blockHeader . blockNo ) ) ) ;
235
+ const onMonthBack = Cardano . BlockNo ( Math . max ( highBlockNo - ONE_MONTH_BLOCK_TIME , 0 ) ) ;
236
+
237
+ return await allTransactionsByAddresses ( chainHistoryProvider , {
238
+ addresses,
239
+ filterBy : { blockRange : { lowerBound : onMonthBack } , type : 'blockRange' }
240
+ } ) ;
241
+ } ;
242
+
204
243
const findIntersectionAndUpdateTxStore = ( {
205
244
chainHistoryProvider,
206
245
historicalTransactionsFetchLimit,
@@ -245,12 +284,12 @@ const findIntersectionAndUpdateTxStore = ({
245
284
) ;
246
285
247
286
const lowerBound = lastStoredTransaction ?. blockHeader . blockNo ;
248
- const newTransactions = await allTransactionsByAddresses ( chainHistoryProvider , {
249
- addresses,
250
- filterBy : lowerBound
251
- ? { blockRange : { lowerBound } , type : 'blockRange' }
252
- : { limit : historicalTransactionsFetchLimit , type : 'tip ' }
253
- } ) ;
287
+ const newTransactions = await ( lowerBound === undefined
288
+ ? fetchInitialTransactions ( chainHistoryProvider , addresses , historicalTransactionsFetchLimit )
289
+ : allTransactionsByAddresses ( chainHistoryProvider , {
290
+ addresses ,
291
+ filterBy : { blockRange : { lowerBound } , type : 'blockRange ' }
292
+ } ) ) ;
254
293
255
294
logger . debug (
256
295
`chainHistoryProvider returned ${ newTransactions . length } transactions` ,
0 commit comments