rewards.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { ApiPromise } from "@polkadot/api";
  2. // types
  3. import { Bounty, CacheEvent, WorkerReward, SpendingProposal } 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 {
  9. Proposal,
  10. ProposalId,
  11. SpendingParams,
  12. } from "@joystream/types/proposals";
  13. import { ProposalDetails, ProposalOf } from "@joystream/types/augment/types";
  14. import { Stake } from "@joystream/types/stake";
  15. import {
  16. RewardRelationship,
  17. RewardRelationshipId,
  18. } from "@joystream/types/recurring-rewards";
  19. // lib
  20. import { getPercent, getTotalMinted } from "./";
  21. import {
  22. getBlock,
  23. getBlockHash,
  24. getMint,
  25. getNextWorker,
  26. getMember,
  27. getWorker,
  28. getWorkerReward,
  29. getProposalInfo,
  30. getProposalDetails,
  31. getStake,
  32. getValidators,
  33. getValidatorCount,
  34. } from "./api";
  35. import { WorkerOf } from "@joystream/types/augment-codec/all";
  36. import { ProposalDetailsOf } from "@joystream/types/augment/types";
  37. export const filterMethods = {
  38. getBurnedTokens: ({ section, method }: CacheEvent) =>
  39. section === "balances" && method === "Transfer",
  40. newValidatorsRewards: ({ section, method }: CacheEvent) =>
  41. section === "staking" && method === "Reward",
  42. finalizedSpendingProposals: ({ section, method }: CacheEvent) =>
  43. section === "proposalsEngine" && method === "ProposalStatusUpdated",
  44. sudoSetBalance: ({ section, method }: CacheEvent) =>
  45. section === "balances" && method === "BalanceSet",
  46. proposalStatusUpdated: ({ section, method }: CacheEvent) =>
  47. section === "proposalsEngine" && method === "ProposalStatusUpdated",
  48. };
  49. export const getWorkerRewards = async (
  50. api: ApiPromise,
  51. group: string,
  52. hash: Hash
  53. ): Promise<WorkerReward[]> => {
  54. let workers = Array<WorkerReward>();
  55. const nextWorkerId = await getNextWorker(api, group, hash);
  56. for (let id = 0; id < nextWorkerId; ++id) {
  57. const worker: WorkerOf = await getWorker(api, group, hash, id);
  58. const account = worker.role_account_id;
  59. const memberId = worker.member_id;
  60. const member: Membership = await getMember(api, memberId, hash);
  61. const handle = member ? String(member.handle) : account.toString();
  62. const status = worker.is_active ? `active` : `inactive`;
  63. let stake: Stake;
  64. let reward: RewardRelationship;
  65. if (worker.role_stake_profile.isSome) {
  66. const roleStakeProfile = worker.role_stake_profile.unwrap();
  67. stake = await getStake(api, roleStakeProfile.stake_id);
  68. }
  69. if (worker.reward_relationship.isSome) {
  70. // TODO changing salaries are not reflected
  71. const rewardId: RewardRelationshipId =
  72. worker.reward_relationship.unwrap();
  73. reward = await getWorkerReward(api, hash, rewardId);
  74. }
  75. workers.push({ id, stake, reward, status, handle, account, memberId });
  76. }
  77. return workers;
  78. };
  79. export const getWorkerRow = (
  80. worker: WorkerReward,
  81. earnedStart: number
  82. ): string => {
  83. const mtjoy = (mtjoy: number): string => (mtjoy / 1000000).toFixed(1);
  84. const { id, memberId, account, handle, status, reward } = worker;
  85. const earnedEnd = Number(reward.total_reward_received.toBigInt());
  86. if (!earnedEnd) return ``;
  87. const totalEarned = mtjoy(earnedEnd);
  88. const earnedTerm = mtjoy(earnedEnd - earnedStart);
  89. const amount = Number(reward.amount_per_payout.toBigInt());
  90. const rewardPerBlock = (amount / Number(reward.payout_interval)).toFixed();
  91. const url = `https://pioneer.joystreamstats.live/#/members/${handle}`; // TODO
  92. // TODO compare earning to term start and show difference
  93. return `| ${id} | [@${handle}](${url}) | ${status} | ${rewardPerBlock} | ${earnedTerm} | ${totalEarned} |\n`;
  94. };
  95. export const getBurnedTokens = (
  96. burnAddress: string,
  97. blocks: [number, CacheEvent[]][]
  98. ): number => {
  99. let tokensBurned = 0;
  100. blocks.forEach(([key, transfers]) =>
  101. transfers.forEach((transfer) => {
  102. let receiver = transfer.data[1] as AccountId;
  103. let amount = transfer.data[2] as Balance;
  104. if (receiver.toString() === burnAddress) tokensBurned += Number(amount);
  105. })
  106. );
  107. return tokensBurned;
  108. };
  109. export const getFinalizedSpendingProposals = async (
  110. api: ApiPromise,
  111. blocks: [number, CacheEvent[]][]
  112. ): Promise<SpendingProposal[]> => {
  113. const selectedEvents: CacheEvent[] = [];
  114. blocks.map(([key, proposalEvents]) =>
  115. proposalEvents.map((proposalEvent) => selectedEvents.push(proposalEvent))
  116. );
  117. let spendingProposals: SpendingProposal[] = [];
  118. for (const proposalEvent of selectedEvents) {
  119. let statusUpdateData = proposalEvent.data[1] as any;
  120. const finalizedAt = statusUpdateData.finalized.finalizedAt;
  121. if (!(statusUpdateData.finalized && finalizedAt)) continue;
  122. const proposalId = proposalEvent.data[0] as ProposalId;
  123. const proposalInfo: ProposalOf = await getProposalInfo(api, proposalId);
  124. const finalizedData = proposalInfo.status.asFinalized;
  125. const details: ProposalDetailsOf = await getProposalDetails(
  126. api,
  127. proposalId
  128. );
  129. if (finalizedData.proposalStatus.isApproved && details.isSpending) {
  130. let approvedData = finalizedData.proposalStatus.asApproved;
  131. if (!approvedData.isExecuted) continue;
  132. if (!spendingProposals.some((proposal) => proposal.id === +proposalId)) {
  133. const title = proposalInfo.title.toString();
  134. const amount = +details.asSpending[0];
  135. spendingProposals.push({ id: +proposalId, title, amount });
  136. }
  137. }
  138. }
  139. return spendingProposals;
  140. };
  141. export const getValidatorsRewards = (
  142. blocks: [number, CacheEvent[]][]
  143. ): number => {
  144. let newValidatorRewards = 0;
  145. blocks.forEach(([key, validatorRewards]) =>
  146. validatorRewards.forEach(
  147. (reward: CacheEvent) => (newValidatorRewards += Number(reward.data[1]))
  148. )
  149. );
  150. return newValidatorRewards;
  151. };
  152. export const getActiveValidators = async (
  153. api: ApiPromise,
  154. hash: Hash,
  155. searchPreviousBlocks: boolean = false
  156. ): Promise<AccountId[]> => {
  157. const block = await getBlock(api, hash);
  158. let currentBlockNr = block.block.header.number.toNumber();
  159. let activeValidators: AccountId[];
  160. do {
  161. const hash: Hash = await getBlockHash(api, currentBlockNr);
  162. const validators: AccountId[] = await getValidators(api, hash);
  163. if (validators.length) {
  164. let max = await getValidatorCount(api, hash);
  165. activeValidators = validators.slice(0, max);
  166. }
  167. if (searchPreviousBlocks) --currentBlockNr;
  168. else ++currentBlockNr;
  169. } while (!activeValidators);
  170. return activeValidators;
  171. };