Skip to content

fix: advance only necessary records #2016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ export class PythStakingClient {
publisher: PublicKey,
) {
return this.integrityPoolProgram.account.delegationRecord
.fetch(getDelegationRecordAddress(stakeAccountPositions, publisher))
.fetchNullable(
getDelegationRecordAddress(stakeAccountPositions, publisher),
)
.then((record) => convertBNToBigInt(record));
}

Expand Down Expand Up @@ -685,10 +687,23 @@ export class PythStakingClient {
),
);

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

const currentEpoch = await getCurrentEpoch(this.connection);

// Filter out delegationRecord that are up to date
const filteredPublishers = publishers.filter((_, index) => {
return !(delegationRecords[index]?.lastEpoch === currentEpoch);
});

// anchor does not calculate the correct pda for other programs
// therefore we need to manually calculate the pdas
const advanceDelegationRecordInstructions = await Promise.all(
publishers.map(({ pubkey, stakeAccount }) =>
filteredPublishers.map(({ pubkey, stakeAccount }) =>
this.integrityPoolProgram.methods
.advanceDelegationRecord()
.accountsPartial({
Expand Down Expand Up @@ -761,19 +776,19 @@ export class PythStakingClient {
totalRewards += BigInt("0x" + buffer.toString("hex"));
}

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

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

Expand Down
Loading