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

Commit

Permalink
Calculate the collator's power by its staked
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Dec 14, 2023
1 parent 2cf74ba commit 69fad80
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/hooks/use-collator-power.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,27 @@ export const useCollatorPower = (
}

const nominators = collatorNominators[cur] || [];
const power = nominators.reduce((acc, cur) => {
const ledger = parsedLedgers[cur];
const deposits = parsedDeposits[cur] || [];

if (ledger) {
const stakedDeposit = deposits
.filter(({ id }) => ledger.stakedDeposits?.includes(id))
.reduce((acc, cur) => acc + BigInt(cur.value), 0n);
return (
acc +
stakingToPower(
BigInt(ledger.stakedRing) + stakedDeposit,
BigInt(ledger.stakedKton),
ringPool,
ktonPool
)
);
}
return acc;
}, 0n);
const { stakedDeposit, stakedRing, stakedKton } = nominators.reduce(
(acc, cur) => {
const ledger = parsedLedgers[cur];
const deposits = parsedDeposits[cur] || [];

if (ledger) {
const stakedDeposit = deposits
.filter(({ id }) => ledger.stakedDeposits?.includes(id))
.reduce((acc, cur) => acc + BigInt(cur.value), 0n);

return {
stakedDeposit: acc.stakedDeposit + stakedDeposit,
stakedRing: acc.stakedRing + BigInt(ledger.stakedRing),
stakedKton: acc.stakedKton + BigInt(ledger.stakedKton),
};
}
return acc;
},
{ stakedDeposit: 0n, stakedRing: 0n, stakedKton: 0n }
);
const power = stakingToPower(stakedRing + stakedDeposit, stakedKton, ringPool, ktonPool);

const commission = collatorCommission[cur] || "0.00%";
return { ...acc, [cur]: commissionWeightedPower(power, commission) };
Expand Down

0 comments on commit 69fad80

Please sign in to comment.