@@ -382,7 +382,7 @@ impl BreezSdk {
382
382
let save_res = object_repository
383
383
. save_sync_info ( & CachedSyncInfo {
384
384
offset : next_offset. saturating_sub ( pending_payments) ,
385
- token_offset : cached_sync_info. token_offset ,
385
+ last_synced_token_timestamp : cached_sync_info. last_synced_token_timestamp ,
386
386
} )
387
387
. await ;
388
388
@@ -404,14 +404,18 @@ impl BreezSdk {
404
404
. fetch_sync_info ( )
405
405
. await ?
406
406
. unwrap_or_default ( ) ;
407
- let current_token_offset = cached_sync_info. token_offset ;
407
+ let last_synced_token_timestamp = cached_sync_info. last_synced_token_timestamp ;
408
408
409
409
let our_public_key = self . spark_wallet . get_identity_public_key ( ) ;
410
410
411
+ let mut latest_token_transaction_timestamp = None ;
412
+
411
413
// We'll keep querying in batches until we have all transfers
412
- let mut next_offset = current_token_offset ;
414
+ let mut next_offset = 0 ;
413
415
let mut has_more = true ;
414
- info ! ( "Syncing token payments to storage, offset = {next_offset}" ) ;
416
+ info ! (
417
+ "Syncing token payments to storage, last synced token timestamp = {last_synced_token_timestamp:?}"
418
+ ) ;
415
419
while has_more {
416
420
// Get batch of token transactions starting from current offset
417
421
let token_transactions = self
@@ -420,12 +424,18 @@ impl BreezSdk {
420
424
paging : Some ( PagingFilter :: new (
421
425
Some ( next_offset) ,
422
426
Some ( PAYMENT_SYNC_BATCH_SIZE ) ,
423
- Some ( Order :: Ascending ) ,
427
+ None ,
424
428
) ) ,
425
429
..Default :: default ( )
426
430
} )
427
431
. await ?;
428
432
433
+ // On first iteration, set the latest token transaction timestamp to the first transaction timestamp
434
+ if next_offset == 0 {
435
+ latest_token_transaction_timestamp =
436
+ token_transactions. first ( ) . map ( |tx| tx. created_timestamp ) ;
437
+ }
438
+
429
439
// Get prev out hashes of first input of each token transaction
430
440
// Assumes all inputs of a tx share the same owner public key
431
441
let token_transactions_prevout_hashes = token_transactions
@@ -459,6 +469,13 @@ impl BreezSdk {
459
469
) ;
460
470
// Process transfers in this batch
461
471
for transaction in & token_transactions {
472
+ // Stop syncing if we have reached the last synced token transaction timestamp
473
+ if let Some ( last_synced_token_timestamp) = last_synced_token_timestamp {
474
+ if transaction. created_timestamp <= last_synced_token_timestamp {
475
+ break ;
476
+ }
477
+ }
478
+
462
479
let tx_inputs_are_ours = match & transaction. inputs {
463
480
spark_wallet:: TokenInputs :: Transfer ( token_transfer_input) => {
464
481
let Some ( first_input) = token_transfer_input. outputs_to_spend . first ( )
@@ -504,18 +521,21 @@ impl BreezSdk {
504
521
505
522
// Check if we have more transfers to fetch
506
523
next_offset = next_offset. saturating_add ( u64:: try_from ( token_transactions. len ( ) ) ?) ;
507
- // Update our last processed offset in the storage
524
+ has_more = token_transactions. len ( ) as u64 == PAYMENT_SYNC_BATCH_SIZE ;
525
+ }
526
+
527
+ // Update our last processed transaction timestamp in the storage
528
+ if let Some ( latest_token_transaction_timestamp) = latest_token_transaction_timestamp {
508
529
let save_res = object_repository
509
530
. save_sync_info ( & CachedSyncInfo {
510
531
offset : cached_sync_info. offset ,
511
- token_offset : next_offset ,
532
+ last_synced_token_timestamp : Some ( latest_token_transaction_timestamp ) ,
512
533
} )
513
534
. await ;
514
535
515
536
if let Err ( err) = save_res {
516
- error ! ( "Failed to update last sync token offset : {err:?}" ) ;
537
+ error ! ( "Failed to update last sync token timestamp : {err:?}" ) ;
517
538
}
518
- has_more = token_transactions. len ( ) as u64 == PAYMENT_SYNC_BATCH_SIZE ;
519
539
}
520
540
521
541
Ok ( ( ) )
0 commit comments