Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Oct 9, 2024
1 parent e5e30e6 commit 22a3f7f
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
type TargetAccount,
type VoterWeightAction,
type VestingSchedule,
Position,
} from "./types";
import { convertBigIntToBN, convertBNToBigInt } from "./utils/bn";
import { epochToDate, getCurrentEpoch } from "./utils/clock";
Expand Down Expand Up @@ -680,11 +681,20 @@ export class PythStakingClient {
stakeAccountPositions,
);
const allPublishers = extractPublisherData(poolData);
const publishers = allPublishers.filter(({ pubkey }) =>
stakeAccountPositionsData.data.positions.some(
const publishers = allPublishers.map((publisher) => {
const positionsWithPublisher = stakeAccountPositionsData.data.positions.filter(
({ targetWithParameters }) =>
targetWithParameters.integrityPool?.publisher.equals(pubkey),
),
targetWithParameters.integrityPool?.publisher.equals(publisher.pubkey),
)

const lowestEpoch = positionsWithPublisher.reduce((min, position) => (min === undefined || position.activationEpoch < min) ? position.activationEpoch : min, <bigint| undefined>undefined);
return {
...publisher,
lowestEpoch
}

}).filter(
({ lowestEpoch }) => lowestEpoch !== undefined
);

const delegationRecords = await Promise.all(
Expand All @@ -693,6 +703,21 @@ export class PythStakingClient {
),
);

const max = (a : bigint | undefined, b: bigint | undefined) => {
if (a === undefined) {
return b;
}
if (b === undefined) {
return a;
}
return a > b ? a : b;
}

const lowestEpoch = publishers.reduce((min, publisher, index) => {
const maximum = max(publisher.lowestEpoch, delegationRecords[index]?.lastEpoch);
return (min === undefined || (maximum !== undefined && maximum < min)) ? publisher.lowestEpoch : min, <bigint| undefined>undefined
}, <bigint| undefined>undefined);

const currentEpoch = await getCurrentEpoch(this.connection);

// Filter out delegationRecord that are up to date
Expand Down Expand Up @@ -738,7 +763,7 @@ export class PythStakingClient {
return {
advanceDelegationRecordInstructions,
mergePositionsInstruction,
publishers,
lowestEpoch,
};
}

Expand Down Expand Up @@ -776,26 +801,10 @@ export class PythStakingClient {
totalRewards += BigInt("0x" + buffer.toString("hex"));
}

const delegationRecords = await Promise.all(
instructions.publishers.map(({ pubkey }) =>
this.getDelegationRecord(stakeAccountPositions, pubkey),
),
);

let lowestEpoch: bigint | undefined;
for (const record of delegationRecords) {
if (
record !== null &&
(lowestEpoch === undefined || record.lastEpoch < lowestEpoch)
) {
lowestEpoch = record.lastEpoch;
}
}

return {
totalRewards,
expiry:
lowestEpoch === undefined ? undefined : epochToDate(lowestEpoch + 52n),
instructions.lowestEpoch === undefined ? undefined : epochToDate(instructions.lowestEpoch + 52n),
};
}

Expand Down

0 comments on commit 22a3f7f

Please sign in to comment.