Skip to content

Commit e5e30e6

Browse files
authored
fix: advance only necessary records (#2016)
* do it * go * forgot return * clean up * go
1 parent 53521aa commit e5e30e6

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ export class PythStakingClient {
172172
publisher: PublicKey,
173173
) {
174174
return this.integrityPoolProgram.account.delegationRecord
175-
.fetch(getDelegationRecordAddress(stakeAccountPositions, publisher))
175+
.fetchNullable(
176+
getDelegationRecordAddress(stakeAccountPositions, publisher),
177+
)
176178
.then((record) => convertBNToBigInt(record));
177179
}
178180

@@ -685,10 +687,23 @@ export class PythStakingClient {
685687
),
686688
);
687689

690+
const delegationRecords = await Promise.all(
691+
publishers.map(({ pubkey }) =>
692+
this.getDelegationRecord(stakeAccountPositions, pubkey),
693+
),
694+
);
695+
696+
const currentEpoch = await getCurrentEpoch(this.connection);
697+
698+
// Filter out delegationRecord that are up to date
699+
const filteredPublishers = publishers.filter((_, index) => {
700+
return !(delegationRecords[index]?.lastEpoch === currentEpoch);
701+
});
702+
688703
// anchor does not calculate the correct pda for other programs
689704
// therefore we need to manually calculate the pdas
690705
const advanceDelegationRecordInstructions = await Promise.all(
691-
publishers.map(({ pubkey, stakeAccount }) =>
706+
filteredPublishers.map(({ pubkey, stakeAccount }) =>
692707
this.integrityPoolProgram.methods
693708
.advanceDelegationRecord()
694709
.accountsPartial({
@@ -761,19 +776,19 @@ export class PythStakingClient {
761776
totalRewards += BigInt("0x" + buffer.toString("hex"));
762777
}
763778

764-
const delegationRecords = await Promise.allSettled(
779+
const delegationRecords = await Promise.all(
765780
instructions.publishers.map(({ pubkey }) =>
766781
this.getDelegationRecord(stakeAccountPositions, pubkey),
767782
),
768783
);
769784

770785
let lowestEpoch: bigint | undefined;
771786
for (const record of delegationRecords) {
772-
if (record.status === "fulfilled") {
773-
const { lastEpoch } = record.value;
774-
if (lowestEpoch === undefined || lastEpoch < lowestEpoch) {
775-
lowestEpoch = lastEpoch;
776-
}
787+
if (
788+
record !== null &&
789+
(lowestEpoch === undefined || record.lastEpoch < lowestEpoch)
790+
) {
791+
lowestEpoch = record.lastEpoch;
777792
}
778793
}
779794

0 commit comments

Comments
 (0)