Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Adapt AuthoredBlockCount and ExposureCache{X} #68

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading