3
0

rewards.ts 3.9 KB

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