Skip to content

Commit 47d6535

Browse files
authored
fix: lowest epoch calc (#2018)
* works * GO * go * go * add 1 * add bigint * bigint min
1 parent 5968300 commit 47d6535

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

governance/pyth_staking_sdk/src/pyth-staking-client.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
type VoterWeightAction,
5050
type VestingSchedule,
5151
} from "./types";
52+
import { bigintMax, bigintMin } from "./utils/bigint";
5253
import { convertBigIntToBN, convertBNToBigInt } from "./utils/bn";
5354
import { epochToDate, getCurrentEpoch } from "./utils/clock";
5455
import { extractPublisherData } from "./utils/pool";
@@ -680,19 +681,43 @@ export class PythStakingClient {
680681
stakeAccountPositions,
681682
);
682683
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);
689705

690706
const delegationRecords = await Promise.all(
691707
publishers.map(({ pubkey }) =>
692708
this.getDelegationRecord(stakeAccountPositions, pubkey),
693709
),
694710
);
695711

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+
696721
const currentEpoch = await getCurrentEpoch(this.connection);
697722

698723
// Filter out delegationRecord that are up to date
@@ -738,7 +763,7 @@ export class PythStakingClient {
738763
return {
739764
advanceDelegationRecordInstructions,
740765
mergePositionsInstruction,
741-
publishers,
766+
lowestEpoch,
742767
};
743768
}
744769

@@ -776,26 +801,12 @@ export class PythStakingClient {
776801
totalRewards += BigInt("0x" + buffer.toString("hex"));
777802
}
778803

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-
795804
return {
796805
totalRewards,
797806
expiry:
798-
lowestEpoch === undefined ? undefined : epochToDate(lowestEpoch + 52n),
807+
instructions.lowestEpoch === undefined
808+
? undefined
809+
: epochToDate(instructions.lowestEpoch + 53n),
799810
};
800811
}
801812

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const bigintMax = (a: bigint | undefined, b: bigint | undefined) => {
2+
if (a === undefined) {
3+
return b;
4+
}
5+
if (b === undefined) {
6+
return a;
7+
}
8+
return a > b ? a : b;
9+
};
10+
11+
export const bigintMin = (a: bigint | undefined, b: bigint | undefined) => {
12+
if (a === undefined) {
13+
return b;
14+
}
15+
if (b === undefined) {
16+
return a;
17+
}
18+
return a < b ? a : b;
19+
};

0 commit comments

Comments
 (0)