@@ -10,8 +10,9 @@ import * as _ from "lodash";
10
10
import * as Sequelize from "sequelize" ;
11
11
import { Transaction } from "sequelize" ;
12
12
import * as Exception from "../../exception" ;
13
+ import { txPagination } from "../../routers/pagination" ;
13
14
import models from "../index" ;
14
- import { TransactionInstance } from "../transaction" ;
15
+ import { TransactionAttribute , TransactionInstance } from "../transaction" ;
15
16
import { createAddressLog } from "./addressLog" ;
16
17
import { updateAssetScheme } from "./assetscheme" ;
17
18
import { createChangeAssetScheme } from "./changeAssetScheme" ;
@@ -395,17 +396,45 @@ async function getHashesByPlatformAddress(params: {
395
396
address : string ;
396
397
page : number ;
397
398
itemsPerPage : number ;
399
+ firstEvaluatedKey : [ number , number ] | null ;
400
+ lastEvaluatedKey : [ number , number ] | null ;
398
401
} ) : Promise < string [ ] > {
399
- const { address, page, itemsPerPage } = params ;
402
+ const {
403
+ address,
404
+ page,
405
+ itemsPerPage,
406
+ firstEvaluatedKey,
407
+ lastEvaluatedKey
408
+ } = params ;
409
+
410
+ const whereCond : any [ ] = [
411
+ {
412
+ address
413
+ }
414
+ ] ;
415
+ if ( firstEvaluatedKey || lastEvaluatedKey ) {
416
+ whereCond . push (
417
+ txPagination . where ( {
418
+ firstEvaluatedKey,
419
+ lastEvaluatedKey
420
+ } )
421
+ ) ;
422
+ }
400
423
try {
401
424
return models . AddressLog . findAll ( {
402
425
attributes : [ "transactionHash" ] ,
403
426
where : {
404
- address
427
+ [ Sequelize . Op . and ] : whereCond
405
428
} ,
406
- order : [ [ "blockNumber" , "DESC" ] , [ "transactionIndex" , "DESC" ] ] ,
429
+ order : txPagination . orderby ( {
430
+ firstEvaluatedKey,
431
+ lastEvaluatedKey
432
+ } ) ,
407
433
limit : itemsPerPage ,
408
- offset : ( page - 1 ) * itemsPerPage
434
+ offset :
435
+ firstEvaluatedKey || lastEvaluatedKey
436
+ ? 0
437
+ : ( page - 1 ) * itemsPerPage
409
438
} ) . map ( r => r . get ( "transactionHash" ) ) ;
410
439
} catch ( err ) {
411
440
console . error ( err ) ;
@@ -418,18 +447,48 @@ async function getHashesByAssetAddress(params: {
418
447
assetType ?: string | null ;
419
448
page : number ;
420
449
itemsPerPage : number ;
450
+ firstEvaluatedKey : [ number , number ] | null ;
451
+ lastEvaluatedKey : [ number , number ] | null ;
421
452
} ) : Promise < string [ ] > {
422
- const { address, assetType, page, itemsPerPage } = params ;
453
+ const {
454
+ address,
455
+ assetType,
456
+ page,
457
+ itemsPerPage,
458
+ firstEvaluatedKey,
459
+ lastEvaluatedKey
460
+ } = params ;
461
+
462
+ const whereCond : any [ ] = [
463
+ {
464
+ address,
465
+ ...( assetType && { assetType } )
466
+ }
467
+ ] ;
468
+ if ( firstEvaluatedKey || lastEvaluatedKey ) {
469
+ whereCond . push (
470
+ txPagination . where ( {
471
+ firstEvaluatedKey,
472
+ lastEvaluatedKey
473
+ } )
474
+ ) ;
475
+ }
476
+
423
477
try {
424
478
return models . AssetAddressLog . findAll ( {
425
479
attributes : [ "transactionHash" ] ,
426
480
where : {
427
- address,
428
- ...( assetType && { assetType } )
481
+ [ Sequelize . Op . and ] : whereCond
429
482
} ,
430
- order : [ [ "blockNumber" , "DESC" ] , [ "transactionIndex" , "DESC" ] ] ,
483
+ order : txPagination . orderby ( {
484
+ firstEvaluatedKey,
485
+ lastEvaluatedKey
486
+ } ) ,
431
487
limit : itemsPerPage ,
432
- offset : ( page - 1 ) * itemsPerPage
488
+ offset :
489
+ firstEvaluatedKey || lastEvaluatedKey
490
+ ? 0
491
+ : ( page - 1 ) * itemsPerPage
433
492
} ) . map ( r => r . get ( "transactionHash" ) ) ;
434
493
} catch ( err ) {
435
494
console . error ( err ) ;
@@ -441,17 +500,46 @@ async function getHashesByAssetType(params: {
441
500
assetType : string ;
442
501
page : number ;
443
502
itemsPerPage : number ;
503
+ firstEvaluatedKey : [ number , number ] | null ;
504
+ lastEvaluatedKey : [ number , number ] | null ;
444
505
} ) : Promise < string [ ] > {
445
- const { assetType, page, itemsPerPage } = params ;
506
+ const {
507
+ assetType,
508
+ page,
509
+ itemsPerPage,
510
+ firstEvaluatedKey,
511
+ lastEvaluatedKey
512
+ } = params ;
513
+
514
+ const whereCond : any [ ] = [
515
+ {
516
+ assetType
517
+ }
518
+ ] ;
519
+ if ( firstEvaluatedKey || lastEvaluatedKey ) {
520
+ whereCond . push (
521
+ txPagination . where ( {
522
+ firstEvaluatedKey,
523
+ lastEvaluatedKey
524
+ } )
525
+ ) ;
526
+ }
527
+
446
528
try {
447
529
return models . AssetTypeLog . findAll ( {
448
530
attributes : [ "transactionHash" ] ,
449
531
where : {
450
- assetType
532
+ [ Sequelize . Op . and ] : whereCond
451
533
} ,
452
- order : [ [ "blockNumber" , "DESC" ] , [ "transactionIndex" , "DESC" ] ] ,
534
+ order : txPagination . orderby ( {
535
+ firstEvaluatedKey,
536
+ lastEvaluatedKey
537
+ } ) ,
453
538
limit : itemsPerPage ,
454
- offset : ( page - 1 ) * itemsPerPage
539
+ offset :
540
+ firstEvaluatedKey || lastEvaluatedKey
541
+ ? 0
542
+ : ( page - 1 ) * itemsPerPage
455
543
} ) . map ( r => r . get ( "transactionHash" ) ) ;
456
544
} catch ( err ) {
457
545
console . error ( err ) ;
@@ -464,38 +552,86 @@ async function getHashes(params: {
464
552
assetType ?: string | null ;
465
553
page : number ;
466
554
itemsPerPage : number ;
555
+ firstEvaluatedKey : [ number , number ] | null ;
556
+ lastEvaluatedKey : [ number , number ] | null ;
467
557
type ?: string [ ] | null ;
468
558
includePending ?: boolean | null ;
469
559
} ) : Promise < string [ ] > {
470
- const { address, assetType, page, itemsPerPage } = params ;
560
+ const {
561
+ address,
562
+ assetType,
563
+ page,
564
+ itemsPerPage,
565
+ firstEvaluatedKey,
566
+ lastEvaluatedKey
567
+ } = params ;
471
568
if ( address != null && assetType != null ) {
472
569
return getHashesByAssetAddress ( {
473
570
address,
474
571
assetType,
475
572
page,
476
- itemsPerPage
573
+ itemsPerPage,
574
+ firstEvaluatedKey,
575
+ lastEvaluatedKey
477
576
} ) ;
478
577
} else if ( address != null ) {
479
578
if ( AssetAddress . check ( address ) ) {
480
- return getHashesByAssetAddress ( { address, page, itemsPerPage } ) ;
579
+ return getHashesByAssetAddress ( {
580
+ address,
581
+ page,
582
+ itemsPerPage,
583
+ firstEvaluatedKey,
584
+ lastEvaluatedKey
585
+ } ) ;
481
586
} else if ( PlatformAddress . check ( address ) ) {
482
- return getHashesByPlatformAddress ( { address, page, itemsPerPage } ) ;
587
+ return getHashesByPlatformAddress ( {
588
+ address,
589
+ page,
590
+ itemsPerPage,
591
+ firstEvaluatedKey,
592
+ lastEvaluatedKey
593
+ } ) ;
483
594
}
484
595
throw Error ( `Invalid address: ${ address } ` ) ;
485
596
} else if ( assetType != null ) {
486
- return getHashesByAssetType ( { assetType, page, itemsPerPage } ) ;
597
+ return getHashesByAssetType ( {
598
+ assetType,
599
+ page,
600
+ itemsPerPage,
601
+ firstEvaluatedKey,
602
+ lastEvaluatedKey
603
+ } ) ;
487
604
}
488
- return models . Transaction . findAll ( {
489
- attributes : [ "hash" ] ,
490
- where : {
605
+ const whereCond : any [ ] = [
606
+ {
491
607
...( params . type != null
492
608
? { type : { [ Sequelize . Op . in ] : params . type } }
493
609
: { } ) ,
494
610
...( params . includePending !== true ? { isPending : false } : { } )
611
+ }
612
+ ] ;
613
+ if ( firstEvaluatedKey || lastEvaluatedKey ) {
614
+ whereCond . push (
615
+ txPagination . where ( {
616
+ firstEvaluatedKey,
617
+ lastEvaluatedKey
618
+ } )
619
+ ) ;
620
+ }
621
+ return models . Transaction . findAll ( {
622
+ attributes : [ "hash" ] ,
623
+ where : {
624
+ [ Sequelize . Op . and ] : whereCond
495
625
} ,
496
- order : [ [ "blockNumber" , "DESC" ] , [ "transactionIndex" , "DESC" ] ] ,
626
+ order : txPagination . orderby ( {
627
+ firstEvaluatedKey,
628
+ lastEvaluatedKey
629
+ } ) ,
497
630
limit : itemsPerPage ,
498
- offset : ( page - 1 ) * itemsPerPage
631
+ offset :
632
+ firstEvaluatedKey || lastEvaluatedKey
633
+ ? 0
634
+ : ( page - 1 ) * itemsPerPage
499
635
} ) . map ( result => result . get ( "hash" ) ) ;
500
636
}
501
637
@@ -505,11 +641,13 @@ export async function getTransactions(params: {
505
641
type ?: string [ ] | null ;
506
642
page : number ;
507
643
itemsPerPage : number ;
644
+ firstEvaluatedKey : [ number , number ] | null ;
645
+ lastEvaluatedKey : [ number , number ] | null ;
508
646
includePending ?: boolean | null ;
509
647
onlyConfirmed ?: boolean | null ;
510
648
confirmThreshold ?: number | null ;
511
649
} ) {
512
- const { itemsPerPage } = params ;
650
+ const { itemsPerPage, firstEvaluatedKey , lastEvaluatedKey } = params ;
513
651
try {
514
652
// TODO: Querying twice will waste IO bandwidth and take longer time as long as the response time
515
653
// Find a way to merge these queries.
@@ -524,7 +662,10 @@ export async function getTransactions(params: {
524
662
where : {
525
663
hash : hashes
526
664
} ,
527
- order : [ [ "blockNumber" , "DESC" ] , [ "transactionIndex" , "DESC" ] ] ,
665
+ order : txPagination . orderby ( {
666
+ firstEvaluatedKey,
667
+ lastEvaluatedKey
668
+ } ) ,
528
669
include : [ ...fullIncludeArray ]
529
670
} ) ;
530
671
} catch ( err ) {
@@ -670,3 +811,7 @@ export async function getRegularKeyOwnerByPublicKey(
670
811
}
671
812
return tx . get ( "signer" ) ;
672
813
}
814
+
815
+ export function createTxEvaluatedKey ( tx : TransactionAttribute ) {
816
+ return JSON . stringify ( [ tx . blockNumber , tx . transactionIndex ] ) ;
817
+ }
0 commit comments