getCouncilData.ts 2.8 KB

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