rewards.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { ApiPromise } from "@polkadot/api";
  2. // types
  3. import { Bounty, CacheEvent, WorkerReward } from "./types";
  4. import { AccountId, Balance } from "@polkadot/types/interfaces";
  5. import { Hash } from "@polkadot/types/interfaces";
  6. import { Membership } from "@joystream/types/members";
  7. import { Mint, MintId } from "@joystream/types/mint";
  8. import { Stake } from "@joystream/types/stake";
  9. import { WorkerOf } from "@joystream/types/augment/all";
  10. import {
  11. RewardRelationship,
  12. RewardRelationshipId,
  13. } from "@joystream/types/recurring-rewards";
  14. // lib
  15. import { getPercent, getTotalMinted } from "./";
  16. import {
  17. getBlock,
  18. getBlockHash,
  19. getMint,
  20. getNextWorker,
  21. getWorker,
  22. getWorkerReward,
  23. getStake,
  24. getValidators,
  25. getValidatorCount,
  26. } from "./api";
  27. export const filterMethods = {
  28. getBurnedTokens: ({ section, method }: CacheEvent) =>
  29. section === "balances" && method === "Transfer",
  30. newValidatorsRewards: ({ section, method }: CacheEvent) =>
  31. section === "staking" && method === "Reward",
  32. };
  33. export const getWorkerRewards = async (
  34. api: ApiPromise,
  35. group: string,
  36. hash: Hash
  37. ): Promise<WorkerReward[]> => {
  38. let workers = Array<WorkerReward>();
  39. const nextWorkerId = await getNextWorker(api, group, hash);
  40. for (let id = 0; id < nextWorkerId; ++id) {
  41. const worker: WorkerOf = await getWorker(api, group, hash, id);
  42. // TODO workers fired before the end will be missed out
  43. if (!worker.is_active) continue;
  44. let stake: Stake, reward: RewardRelationship;
  45. if (worker.role_stake_profile.isSome) {
  46. const roleStakeProfile = worker.role_stake_profile.unwrap();
  47. stake = await getStake(api, roleStakeProfile.stake_id);
  48. }
  49. if (worker.reward_relationship.isSome) {
  50. // TODO changing salaries are not reflected
  51. const rewardId: RewardRelationshipId = worker.reward_relationship.unwrap();
  52. reward = await getWorkerReward(api, hash, rewardId);
  53. }
  54. workers.push({ id, stake, reward });
  55. }
  56. return workers;
  57. };
  58. export const getBurnedTokens = (
  59. burnAddress: string,
  60. blocks: [number, CacheEvent[]][]
  61. ): number => {
  62. let tokensBurned = 0;
  63. blocks.forEach(([key, transfers]) =>
  64. transfers.forEach((transfer) => {
  65. let receiver = transfer.data[1] as AccountId;
  66. let amount = transfer.data[2] as Balance;
  67. if (receiver.toString() === burnAddress) tokensBurned = Number(amount);
  68. })
  69. );
  70. return tokensBurned;
  71. };
  72. export const getValidatorsRewards = (
  73. blocks: [number, CacheEvent[]][]
  74. ): number => {
  75. let newValidatorRewards = 0;
  76. blocks.forEach(([key, validatorRewards]) =>
  77. validatorRewards.forEach(
  78. (reward: CacheEvent) => (newValidatorRewards += Number(reward.data[1]))
  79. )
  80. );
  81. return newValidatorRewards;
  82. };
  83. export const getActiveValidators = async (
  84. api: ApiPromise,
  85. hash: Hash,
  86. searchPreviousBlocks: boolean = false
  87. ): Promise<AccountId[]> => {
  88. const block = await getBlock(api, hash);
  89. let currentBlockNr = block.block.header.number.toNumber();
  90. let activeValidators: AccountId[];
  91. do {
  92. const hash: Hash = await getBlockHash(api, currentBlockNr);
  93. const validators: AccountId[] = await getValidators(api, hash);
  94. if (validators.length) {
  95. let max = await getValidatorCount(api, hash);
  96. activeValidators = validators.slice(0, max);
  97. }
  98. if (searchPreviousBlocks) --currentBlockNr;
  99. else ++currentBlockNr;
  100. } while (activeValidators == undefined);
  101. return activeValidators;
  102. };