From 22a3f7f925c1f4242ca341f705c83a32445844b7 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 18:31:58 +0100 Subject: [PATCH 1/7] works --- .../src/pyth-staking-client.ts | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index cd43179183..82f16705ca 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -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"; @@ -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, undefined); + return { + ...publisher, + lowestEpoch + } + + }).filter( + ({ lowestEpoch }) => lowestEpoch !== undefined ); const delegationRecords = await Promise.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, undefined + }, undefined); + const currentEpoch = await getCurrentEpoch(this.connection); // Filter out delegationRecord that are up to date @@ -738,7 +763,7 @@ export class PythStakingClient { return { advanceDelegationRecordInstructions, mergePositionsInstruction, - publishers, + lowestEpoch, }; } @@ -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), }; } From e38039b230ecaed155798f10c3460ea38f379db9 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 18:35:28 +0100 Subject: [PATCH 2/7] GO --- governance/pyth_staking_sdk/src/pyth-staking-client.ts | 9 ++++----- 1 file changed, 4 insertions(+), 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 82f16705ca..91deb68d77 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -48,7 +48,6 @@ import { type TargetAccount, type VoterWeightAction, type VestingSchedule, - Position, } from "./types"; import { convertBigIntToBN, convertBNToBigInt } from "./utils/bn"; import { epochToDate, getCurrentEpoch } from "./utils/clock"; @@ -687,7 +686,7 @@ export class PythStakingClient { targetWithParameters.integrityPool?.publisher.equals(publisher.pubkey), ) - const lowestEpoch = positionsWithPublisher.reduce((min, position) => (min === undefined || position.activationEpoch < min) ? position.activationEpoch : min, undefined); + const lowestEpoch = positionsWithPublisher.reduce((min, position) => (min === undefined || position.activationEpoch < min) ? position.activationEpoch : min, undefined); return { ...publisher, lowestEpoch @@ -713,10 +712,10 @@ export class PythStakingClient { return a > b ? a : b; } - const lowestEpoch = publishers.reduce((min, publisher, index) => { + 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, undefined - }, undefined); + return (min === undefined || (maximum !== undefined && maximum < min)) ? publisher.lowestEpoch : min, undefined as bigint| undefined + }, undefined); const currentEpoch = await getCurrentEpoch(this.connection); From bbba8b23a5f452b6d14bbe5dc0c3a81f1d04e250 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 18:44:18 +0100 Subject: [PATCH 3/7] go --- governance/pyth_staking_sdk/src/pyth-staking-client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 91deb68d77..855c53357b 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -702,7 +702,7 @@ export class PythStakingClient { ), ); - const max = (a : bigint | undefined, b: bigint | undefined) => { + const bigintMax = (a : bigint | undefined, b: bigint | undefined) => { if (a === undefined) { return b; } @@ -713,8 +713,8 @@ export class PythStakingClient { } 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, undefined as bigint| undefined + const maximum = bigintMax(publisher.lowestEpoch, delegationRecords[index]?.lastEpoch); + return (min === undefined || (maximum !== undefined && maximum < min)) ? maximum : min; }, undefined); const currentEpoch = await getCurrentEpoch(this.connection); From f79dc81d8cd12eeb90a3959f50714ee15fa20303 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 18:59:28 +0100 Subject: [PATCH 4/7] go --- .../src/pyth-staking-client.ts | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 855c53357b..58608f6d2f 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -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"; @@ -680,21 +681,32 @@ 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((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 }) => @@ -702,21 +714,20 @@ export class PythStakingClient { ), ); - 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((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 @@ -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), }; } From 6c931f5e4f86bec521b6c3da7d56645f40261af4 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 19:22:34 +0100 Subject: [PATCH 5/7] add 1 --- 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 58608f6d2f..55380f513f 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -816,7 +816,7 @@ export class PythStakingClient { expiry: instructions.lowestEpoch === undefined ? undefined - : epochToDate(instructions.lowestEpoch + 52n), + : epochToDate(instructions.lowestEpoch + 53n), }; } From 7d5093ee1099cab1fed1ffdbc9b349f6d0733922 Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Wed, 9 Oct 2024 19:26:26 +0100 Subject: [PATCH 6/7] add bigint --- governance/pyth_staking_sdk/src/utils/bigint.ts | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 governance/pyth_staking_sdk/src/utils/bigint.ts diff --git a/governance/pyth_staking_sdk/src/utils/bigint.ts b/governance/pyth_staking_sdk/src/utils/bigint.ts new file mode 100644 index 0000000000..bc44ae6915 --- /dev/null +++ b/governance/pyth_staking_sdk/src/utils/bigint.ts @@ -0,0 +1,9 @@ +export const bigintMax = (a: bigint | undefined, b: bigint | undefined) => { + if (a === undefined) { + return b; + } + if (b === undefined) { + return a; + } + return a > b ? a : b; +}; From b572f67b65075f31e434cff7deed610c8e41584a Mon Sep 17 00:00:00 2001 From: Guillermo Bescos Date: Thu, 10 Oct 2024 19:44:53 +0100 Subject: [PATCH 7/7] bigint min --- .../pyth_staking_sdk/src/pyth-staking-client.ts | 16 +++------------- governance/pyth_staking_sdk/src/utils/bigint.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 55380f513f..8edc2c08bd 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -49,7 +49,7 @@ import { type VoterWeightAction, type VestingSchedule, } from "./types"; -import { bigintMax } from "./utils/bigint"; +import { bigintMax, bigintMin } from "./utils/bigint"; import { convertBigIntToBN, convertBNToBigInt } from "./utils/bn"; import { epochToDate, getCurrentEpoch } from "./utils/clock"; import { extractPublisherData } from "./utils/pool"; @@ -693,12 +693,7 @@ export class PythStakingClient { let lowestEpoch; for (const position of positionsWithPublisher) { - if ( - lowestEpoch === undefined || - position.activationEpoch < lowestEpoch - ) { - lowestEpoch = position.activationEpoch; - } + lowestEpoch = bigintMin(position.activationEpoch, lowestEpoch); } return { @@ -720,12 +715,7 @@ export class PythStakingClient { publisher.lowestEpoch, delegationRecords[index]?.lastEpoch, ); - if ( - lowestEpoch === undefined || - (maximum !== undefined && maximum < lowestEpoch) - ) { - lowestEpoch = maximum; - } + lowestEpoch = bigintMin(lowestEpoch, maximum); } const currentEpoch = await getCurrentEpoch(this.connection); diff --git a/governance/pyth_staking_sdk/src/utils/bigint.ts b/governance/pyth_staking_sdk/src/utils/bigint.ts index bc44ae6915..0e4cbb8831 100644 --- a/governance/pyth_staking_sdk/src/utils/bigint.ts +++ b/governance/pyth_staking_sdk/src/utils/bigint.ts @@ -7,3 +7,13 @@ export const bigintMax = (a: bigint | undefined, b: bigint | undefined) => { } return a > b ? a : b; }; + +export const bigintMin = (a: bigint | undefined, b: bigint | undefined) => { + if (a === undefined) { + return b; + } + if (b === undefined) { + return a; + } + return a < b ? a : b; +};