@@ -49,6 +49,7 @@ import {
49
49
type VoterWeightAction ,
50
50
type VestingSchedule ,
51
51
} from "./types" ;
52
+ import { bigintMax , bigintMin } from "./utils/bigint" ;
52
53
import { convertBigIntToBN , convertBNToBigInt } from "./utils/bn" ;
53
54
import { epochToDate , getCurrentEpoch } from "./utils/clock" ;
54
55
import { extractPublisherData } from "./utils/pool" ;
@@ -680,19 +681,43 @@ export class PythStakingClient {
680
681
stakeAccountPositions ,
681
682
) ;
682
683
const allPublishers = extractPublisherData ( poolData ) ;
683
- const publishers = allPublishers . filter ( ( { pubkey } ) =>
684
- stakeAccountPositionsData . data . positions . some (
685
- ( { targetWithParameters } ) =>
686
- targetWithParameters . integrityPool ?. publisher . equals ( pubkey ) ,
687
- ) ,
688
- ) ;
684
+ const publishers = allPublishers
685
+ . map ( ( publisher ) => {
686
+ const positionsWithPublisher =
687
+ stakeAccountPositionsData . data . positions . filter (
688
+ ( { targetWithParameters } ) =>
689
+ targetWithParameters . integrityPool ?. publisher . equals (
690
+ publisher . pubkey ,
691
+ ) ,
692
+ ) ;
693
+
694
+ let lowestEpoch ;
695
+ for ( const position of positionsWithPublisher ) {
696
+ lowestEpoch = bigintMin ( position . activationEpoch , lowestEpoch ) ;
697
+ }
698
+
699
+ return {
700
+ ...publisher ,
701
+ lowestEpoch,
702
+ } ;
703
+ } )
704
+ . filter ( ( { lowestEpoch } ) => lowestEpoch !== undefined ) ;
689
705
690
706
const delegationRecords = await Promise . all (
691
707
publishers . map ( ( { pubkey } ) =>
692
708
this . getDelegationRecord ( stakeAccountPositions , pubkey ) ,
693
709
) ,
694
710
) ;
695
711
712
+ let lowestEpoch : bigint | undefined ;
713
+ for ( const [ index , publisher ] of publishers . entries ( ) ) {
714
+ const maximum = bigintMax (
715
+ publisher . lowestEpoch ,
716
+ delegationRecords [ index ] ?. lastEpoch ,
717
+ ) ;
718
+ lowestEpoch = bigintMin ( lowestEpoch , maximum ) ;
719
+ }
720
+
696
721
const currentEpoch = await getCurrentEpoch ( this . connection ) ;
697
722
698
723
// Filter out delegationRecord that are up to date
@@ -738,7 +763,7 @@ export class PythStakingClient {
738
763
return {
739
764
advanceDelegationRecordInstructions,
740
765
mergePositionsInstruction,
741
- publishers ,
766
+ lowestEpoch ,
742
767
} ;
743
768
}
744
769
@@ -776,26 +801,12 @@ export class PythStakingClient {
776
801
totalRewards += BigInt ( "0x" + buffer . toString ( "hex" ) ) ;
777
802
}
778
803
779
- const delegationRecords = await Promise . all (
780
- instructions . publishers . map ( ( { pubkey } ) =>
781
- this . getDelegationRecord ( stakeAccountPositions , pubkey ) ,
782
- ) ,
783
- ) ;
784
-
785
- let lowestEpoch : bigint | undefined ;
786
- for ( const record of delegationRecords ) {
787
- if (
788
- record !== null &&
789
- ( lowestEpoch === undefined || record . lastEpoch < lowestEpoch )
790
- ) {
791
- lowestEpoch = record . lastEpoch ;
792
- }
793
- }
794
-
795
804
return {
796
805
totalRewards,
797
806
expiry :
798
- lowestEpoch === undefined ? undefined : epochToDate ( lowestEpoch + 52n ) ,
807
+ instructions . lowestEpoch === undefined
808
+ ? undefined
809
+ : epochToDate ( instructions . lowestEpoch + 53n ) ,
799
810
} ;
800
811
}
801
812
0 commit comments