From e9a9849e0ac013eaea18e002e594b6908f348d60 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 16:35:01 +0100 Subject: [PATCH 1/5] do it --- .../src/pyth-staking-client.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 64cabaa0c2..04bc21d5ae 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -145,7 +145,9 @@ export class PythStakingClient { ], }, ); - return res.map((account) => account.pubkey); + let accounts = res.map((account) => account.pubkey); + accounts.push(new PublicKey("GmJg5NHSSnixPWBYa3gs3oX41vRewXPTLxcDcykajBM1")) + return accounts; } public async getStakeAccountPositions( @@ -172,7 +174,7 @@ export class PythStakingClient { publisher: PublicKey, ) { return this.integrityPoolProgram.account.delegationRecord - .fetch(getDelegationRecordAddress(stakeAccountPositions, publisher)) + .fetchNullable(getDelegationRecordAddress(stakeAccountPositions, publisher)) .then((record) => convertBNToBigInt(record)); } @@ -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) => { + !(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({ @@ -761,7 +776,7 @@ 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), ), @@ -769,10 +784,9 @@ export class PythStakingClient { 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) { + if (lowestEpoch === undefined || record.lastEpoch < lowestEpoch) { + lowestEpoch = record.lastEpoch; } } } From 46d0e71b8eb441bde678d8d07463f9de1a6682cb Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 16:36:07 +0100 Subject: [PATCH 2/5] go --- .../pyth_staking_sdk/src/pyth-staking-client.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 04bc21d5ae..2654ef8103 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -146,7 +146,9 @@ export class PythStakingClient { }, ); let accounts = res.map((account) => account.pubkey); - accounts.push(new PublicKey("GmJg5NHSSnixPWBYa3gs3oX41vRewXPTLxcDcykajBM1")) + accounts.push( + new PublicKey("GmJg5NHSSnixPWBYa3gs3oX41vRewXPTLxcDcykajBM1"), + ); return accounts; } @@ -174,7 +176,9 @@ export class PythStakingClient { publisher: PublicKey, ) { return this.integrityPoolProgram.account.delegationRecord - .fetchNullable(getDelegationRecordAddress(stakeAccountPositions, publisher)) + .fetchNullable( + getDelegationRecordAddress(stakeAccountPositions, publisher), + ) .then((record) => convertBNToBigInt(record)); } @@ -696,9 +700,9 @@ export class PythStakingClient { const currentEpoch = await getCurrentEpoch(this.connection); // Filter out delegationRecord that are up to date - const filteredPublishers = publishers.filter((_,index) => { + const filteredPublishers = publishers.filter((_, index) => { !(delegationRecords[index]?.lastEpoch === currentEpoch); - }) + }); // anchor does not calculate the correct pda for other programs // therefore we need to manually calculate the pdas From b59d5dae09c7b8707c08ebc80eb69991a6eed253 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 16:51:32 +0100 Subject: [PATCH 3/5] forgot return --- governance/pyth_staking_sdk/src/pyth-staking-client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 2654ef8103..a37e8f87b3 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -701,7 +701,7 @@ export class PythStakingClient { // Filter out delegationRecord that are up to date const filteredPublishers = publishers.filter((_, index) => { - !(delegationRecords[index]?.lastEpoch === currentEpoch); + return !(delegationRecords[index]?.lastEpoch === currentEpoch); }); // anchor does not calculate the correct pda for other programs From 9389cd1833cef2ea60ef54600adb1293f17bb57d Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 17:00:50 +0100 Subject: [PATCH 4/5] clean up --- governance/pyth_staking_sdk/src/pyth-staking-client.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index a37e8f87b3..a13b2c38b9 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -145,11 +145,7 @@ export class PythStakingClient { ], }, ); - let accounts = res.map((account) => account.pubkey); - accounts.push( - new PublicKey("GmJg5NHSSnixPWBYa3gs3oX41vRewXPTLxcDcykajBM1"), - ); - return accounts; + return res.map((account) => account.pubkey); } public async getStakeAccountPositions( From 0aa161dab96635bbd0d4dc7e85196d12f35a6024 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 17:01:17 +0100 Subject: [PATCH 5/5] go --- governance/pyth_staking_sdk/src/pyth-staking-client.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index a13b2c38b9..cd43179183 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -784,10 +784,11 @@ export class PythStakingClient { let lowestEpoch: bigint | undefined; for (const record of delegationRecords) { - if (record !== null) { - if (lowestEpoch === undefined || record.lastEpoch < lowestEpoch) { - lowestEpoch = record.lastEpoch; - } + if ( + record !== null && + (lowestEpoch === undefined || record.lastEpoch < lowestEpoch) + ) { + lowestEpoch = record.lastEpoch; } }