From 9044e6fa1c8ca76c8ab1c985713cb3a5bc1f2c2f Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 22 Nov 2023 12:00:17 +0800 Subject: [PATCH] Adapt AuthoredBlockCount and ExposureCache{X} (#68) * Adapt darwiniaStaking.authoredBlocksCount * Adapt darwiniaStaking.exposureCacheX --- src/hooks/use-collator-last-session-blocks.ts | 53 ++++++++++++------- src/hooks/use-collator-power.ts | 26 ++++++++- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/hooks/use-collator-last-session-blocks.ts b/src/hooks/use-collator-last-session-blocks.ts index 4746eef..bddf945 100644 --- a/src/hooks/use-collator-last-session-blocks.ts +++ b/src/hooks/use-collator-last-session-blocks.ts @@ -15,27 +15,42 @@ export const useCollatorLastSessionBlocks = (defaultValue: DefaultValue) => { useEffect(() => { let unsub = () => undefined; - polkadotApi?.query.darwiniaStaking - .rewardPoints((points: Codec) => { - const [_, collatorPoints] = points.toJSON() as [number, { [address: string]: number }]; // [totalPoint, { collator: collatorPoint }] - const staticNumber = 20; // this staticNumber = 20 was given by the backend - - setCollatorLastSessionBlocks( - Object.keys(collatorPoints).reduce((acc, cur) => { - const collatorPoint = collatorPoints[cur]; - const blocks = collatorPoint / staticNumber; - return { ...acc, [cur]: blocks }; - }, {}) - ); - }) - .then((_unsub) => { - unsub = _unsub as unknown as typeof unsub; - }) - .catch(console.error) - .finally(() => setIsCollatorLastSessionBlocksInitialized(true)); + const handle = (points: Codec) => { + const [_, collatorPoints] = points.toJSON() as [number, { [address: string]: number }]; // [totalPoint, { collator: collatorPoint }] + const staticNumber = 20; // this staticNumber = 20 was given by the backend + + setCollatorLastSessionBlocks( + Object.keys(collatorPoints).reduce((acc, cur) => { + const collatorPoint = collatorPoints[cur]; + const blocks = collatorPoint / staticNumber; + return { ...acc, [cur]: blocks }; + }, {}) + ); + }; + + if (polkadotApi?.query.darwiniaStaking.rewardPoints) { + polkadotApi.query.darwiniaStaking + .rewardPoints(handle) + .then((_unsub) => { + unsub = _unsub as unknown as typeof unsub; + }) + .catch(console.error) + .finally(() => setIsCollatorLastSessionBlocksInitialized(true)); + } else if (polkadotApi?.query.darwiniaStaking.authoredBlocksCount) { + polkadotApi.query.darwiniaStaking + .authoredBlocksCount(handle) + .then((_unsub) => { + unsub = _unsub as unknown as typeof unsub; + }) + .catch(console.error) + .finally(() => setIsCollatorLastSessionBlocksInitialized(true)); + } else { + setCollatorLastSessionBlocks(defaultValue.collatorLastSessionBlocks); + setIsCollatorLastSessionBlocksInitialized(false); + } return () => unsub(); - }, [polkadotApi]); + }, [polkadotApi, defaultValue]); return { collatorLastSessionBlocks, isCollatorLastSessionBlocksInitialized }; }; diff --git a/src/hooks/use-collator-power.ts b/src/hooks/use-collator-power.ts index 3199f9f..a96a157 100644 --- a/src/hooks/use-collator-power.ts +++ b/src/hooks/use-collator-power.ts @@ -17,11 +17,31 @@ interface ExposuresJson { total: string; } +interface ExposuresJsonCache { + nominators: { who: string; vote: string }[]; + vote: string; +} + interface DefaultValue { collatorPower: { [collator: string]: bigint | undefined }; isCollatorPowerInitialized: boolean; } +function isExposuresJsonCache(data: any): data is ExposuresJsonCache { + return data.vote; +} + +function formatExposuresData(data: unknown) { + if (isExposuresJsonCache(data)) { + return { + total: data.vote, + nominators: data.nominators.map(({ who, vote }) => ({ who, value: vote })), + } as ExposuresJson; + } else { + return data as ExposuresJson; + } +} + export const useCollatorPower = ( collatorNominators: { [collator: string]: string[] | undefined }, ringPool: bigint, @@ -37,14 +57,16 @@ export const useCollatorPower = ( if (polkadotApi) { sub$$ = forkJoin([ - polkadotApi.query.darwiniaStaking.exposures.entries(), + polkadotApi.query.darwiniaStaking.exposures + ? polkadotApi.query.darwiniaStaking.exposures.entries() + : polkadotApi.query.darwiniaStaking.exposureCache1.entries(), polkadotApi.query.darwiniaStaking.ledgers.entries(), polkadotApi.query.deposit.deposits.entries(), ]).subscribe({ next: ([exposures, ledgers, deposits]) => { const parsedExposures = exposures.reduce((acc, cur) => { const address = (cur[0].toHuman() as string[])[0]; - const data = cur[1].toJSON() as unknown as ExposuresJson; + const data = formatExposuresData(cur[1].toJSON() as unknown); return { ...acc, [address]: data }; }, {} as { [address: string]: ExposuresJson | undefined });