Skip to content

Commit

Permalink
Adapt AuthoredBlockCount and ExposureCache{X} (#68)
Browse files Browse the repository at this point in the history
* Adapt darwiniaStaking.authoredBlocksCount

* Adapt darwiniaStaking.exposureCacheX
  • Loading branch information
JayJay1024 authored Nov 22, 2023
1 parent 7cadfde commit 9044e6f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
53 changes: 34 additions & 19 deletions src/hooks/use-collator-last-session-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
26 changes: 24 additions & 2 deletions src/hooks/use-collator-power.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });

Expand Down

0 comments on commit 9044e6f

Please sign in to comment.