getCouncilData.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Vec, Option } from '@polkadot/types';
  2. import { ApiPromise } from '@polkadot/api';
  3. import { BlockNumber, BalanceOf } from '@polkadot/types/interfaces';
  4. import { Seats, Backer } from '@joystream/types/src/council';
  5. const getCouncilMembers = async (api: ApiPromise) => {
  6. let totalStake = 0;
  7. const activeCouncil = await api.query.council.activeCouncil() as Seats;
  8. const payoutInterval = Number((await api.query.council.payoutInterval() as Option<BlockNumber>).unwrapOr(0));
  9. const amountPerPayout = (await api.query.council.amountPerPayout() as BalanceOf).toNumber();
  10. activeCouncil.map((member) => {
  11. let stakeAmount = 0;
  12. stakeAmount += Number(member.get('stake'));
  13. const backers = member.get('backers') as Vec<Backer>;
  14. if (!backers?.isEmpty) {
  15. backers.forEach((backer) => {
  16. stakeAmount += Number(backer.get('stake'));
  17. });
  18. }
  19. totalStake += stakeAmount;
  20. });
  21. return {
  22. numberOfCouncilMembers: activeCouncil.length,
  23. totalCouncilRewardsPerBlock: (amountPerPayout && payoutInterval) ? (amountPerPayout * activeCouncil.length) / payoutInterval : 0,
  24. totalCouncilStake: totalStake
  25. };
  26. };
  27. const calculateCouncilRewards = async (api: ApiPromise, totalCouncilRewardsPerBlock: number): Promise<number> => {
  28. let weekInBlocks = 100800;
  29. let councilRewardsInOneWeek = 0;
  30. const termDuration = (await api.query.councilElection.newTermDuration() as BlockNumber).toNumber();
  31. const votingPeriod = (await api.query.councilElection.votingPeriod() as BlockNumber).toNumber();
  32. const revealingPeriod = (await api.query.councilElection.revealingPeriod() as BlockNumber).toNumber();
  33. const announcingPeriod = (await api.query.councilElection.announcingPeriod() as BlockNumber).toNumber();
  34. while (weekInBlocks > 0) {
  35. if (weekInBlocks > termDuration) {
  36. councilRewardsInOneWeek += termDuration * totalCouncilRewardsPerBlock;
  37. weekInBlocks -= termDuration;
  38. } else {
  39. councilRewardsInOneWeek += weekInBlocks * totalCouncilRewardsPerBlock;
  40. return councilRewardsInOneWeek;
  41. }
  42. // -----------------------------
  43. if (weekInBlocks > revealingPeriod) {
  44. weekInBlocks -= revealingPeriod;
  45. } else {
  46. return councilRewardsInOneWeek;
  47. }
  48. // -----------------------------
  49. if (weekInBlocks > votingPeriod) {
  50. weekInBlocks -= votingPeriod;
  51. } else {
  52. return councilRewardsInOneWeek;
  53. }
  54. // -----------------------------
  55. if (weekInBlocks > announcingPeriod) {
  56. weekInBlocks -= announcingPeriod;
  57. } else {
  58. return councilRewardsInOneWeek;
  59. }
  60. }
  61. return councilRewardsInOneWeek;
  62. };
  63. export default async (api: ApiPromise) => {
  64. const { numberOfCouncilMembers, totalCouncilRewardsPerBlock, totalCouncilStake } = await getCouncilMembers(api);
  65. const totalCouncilRewardsInOneWeek = await calculateCouncilRewards(api, totalCouncilRewardsPerBlock);
  66. return {
  67. numberOfCouncilMembers,
  68. totalCouncilRewardsInOneWeek,
  69. totalCouncilStake
  70. };
  71. };