Skip to content

Commit

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

const lowestEpoch = positionsWithPublisher.reduce<bigint| undefined>((min, position) => (min === undefined || position.activationEpoch < min) ? position.activationEpoch : min, undefined);
return {
...publisher,
lowestEpoch
}
const publishers = allPublishers
.map((publisher) => {
const positionsWithPublisher =
stakeAccountPositionsData.data.positions.filter(
({ targetWithParameters }) =>
targetWithParameters.integrityPool?.publisher.equals(
publisher.pubkey,
),
);

let lowestEpoch;
for (const position of positionsWithPublisher) {
if (
lowestEpoch === undefined ||
position.activationEpoch < lowestEpoch
) {
lowestEpoch = position.activationEpoch;
}
}

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

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

const bigintMax = (a : bigint | undefined, b: bigint | undefined) => {
if (a === undefined) {
return b;
}
if (b === undefined) {
return a;
let lowestEpoch: bigint | undefined;
for (const [index, publisher] of publishers.entries()) {
const maximum = bigintMax(
publisher.lowestEpoch,
delegationRecords[index]?.lastEpoch,
);
if (
lowestEpoch === undefined ||
(maximum !== undefined && maximum < lowestEpoch)
) {
lowestEpoch = maximum;
}
return a > b ? a : b;
}

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

const currentEpoch = await getCurrentEpoch(this.connection);

// Filter out delegationRecord that are up to date
Expand Down Expand Up @@ -803,7 +814,9 @@ export class PythStakingClient {
return {
totalRewards,
expiry:
instructions.lowestEpoch === undefined ? undefined : epochToDate(instructions.lowestEpoch + 52n),
instructions.lowestEpoch === undefined
? undefined
: epochToDate(instructions.lowestEpoch + 52n),
};
}

Expand Down

0 comments on commit f79dc81

Please sign in to comment.