working-groups.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { WsProvider, ApiPromise } from "@polkadot/api";
  2. import { types } from "@joystream/types";
  3. import { Worker, WorkerId } from "@joystream/types/working-group";
  4. import { Null, Vec } from "@polkadot/types";
  5. import { Curator, CuratorApplication, CuratorApplicationId, CuratorId, CuratorInduction } from "@joystream/types/content-working-group";
  6. import { RewardRelationship } from "@joystream/types/recurring-rewards";
  7. import { Stake, StakingStatus, Staked } from "@joystream/types/stake";
  8. interface WorkingGroupStake {
  9. stakeId: number,
  10. roleStake: number,
  11. }
  12. interface WorkingGroupReward {
  13. rewardRelationshipId: number,
  14. rewardSize: number,
  15. rewardInterval: number,
  16. rewardPerWeek: number,
  17. totalEarned: number,
  18. totalMissed: number,
  19. }
  20. interface ContentCurator {
  21. curatorId: number,
  22. memberId: number,
  23. roleAccount: string,
  24. applicationId: number,
  25. stakeProfile?: WorkingGroupStake,
  26. rewardRelationship?: WorkingGroupReward,
  27. }
  28. interface StorageProvider {
  29. workerId: number,
  30. memberId: number,
  31. roleAccount: string,
  32. stakeProfile?: WorkingGroupStake,
  33. rewardRelationship?: WorkingGroupReward,
  34. }
  35. async function main() {
  36. // Initialise the provider to connect to the local node
  37. const provider = new WsProvider('ws://127.0.0.1:9944');
  38. /*
  39. If you want to play around on our staging network, go ahead and connect to this staging network instead.
  40. const provider = new WsProvider('wss://alexandria-testing-1.joystream.app/staging/rpc:9944');
  41. There's a bunch of tokens on the account: 5HdYzMVpJv3c4omqwKKr7SpBgzrdRRYBwoNVhJB2Y8xhUbfK,
  42. with seed: "emotion soul hole loan journey what sport inject dwarf cherry ankle lesson"
  43. please transfer (what you need only) to your own account, and don't test runtime upgrades :D
  44. */
  45. // Create the API and wait until ready
  46. const api = await ApiPromise.create({ provider, types })
  47. const storageProviders: StorageProvider[] = []
  48. const contentCurators: ContentCurator[] = []
  49. // get all active storage workers
  50. const storageWorkerKeys = await api.query.storageWorkingGroup.workerById.keys()
  51. const storageWorkerIds = storageWorkerKeys.map(({ args: [workerId]}) => workerId) as Vec<WorkerId>
  52. storageWorkerIds.sort((a,b) => a.toNumber()-b.toNumber())
  53. console.log('all storageWorkerIds:', storageWorkerIds.join(', '));
  54. for (let key of storageWorkerIds) {
  55. const worker = await api.query.storageWorkingGroup.workerById(key) as Worker
  56. //console.log("worker",worker.toHuman())
  57. const storageProvider: StorageProvider = {
  58. workerId: key.toNumber(),
  59. memberId: worker.member_id.toNumber(),
  60. roleAccount: worker.role_account_id.toString(),
  61. }
  62. if (worker.reward_relationship.isSome) {
  63. const rewardRelationshipId = worker.reward_relationship.unwrap()
  64. const rewardRelationship = await api.query.recurringRewards.rewardRelationships(rewardRelationshipId) as RewardRelationship
  65. //console.log("rewardRelationship",rewardRelationship.toHuman())
  66. let rewardInterval = 0
  67. let rewardPerWeek = 0
  68. if (!(rewardRelationship.payout_interval.value instanceof Null)) {
  69. rewardInterval = rewardRelationship.payout_interval.unwrap().toNumber()
  70. rewardPerWeek = rewardRelationship.amount_per_payout.toNumber()*100800/rewardInterval
  71. }
  72. storageProvider.rewardRelationship = {
  73. rewardRelationshipId: rewardRelationshipId.toNumber(),
  74. rewardSize: rewardRelationship.amount_per_payout.toNumber(),
  75. rewardInterval,
  76. rewardPerWeek,
  77. totalEarned: rewardRelationship.total_reward_received.toNumber(),
  78. totalMissed: rewardRelationship.total_reward_missed.toNumber(),
  79. }
  80. }
  81. if (worker.role_stake_profile.isSome && !(worker.role_stake_profile instanceof Null)) {
  82. const stake = worker.role_stake_profile.unwrap()
  83. const workerStake = await api.query.stake.stakes(stake.stake_id) as Stake
  84. //console.log("workerStake",workerStake.toHuman())
  85. const stakingStatus = (workerStake.staking_status as StakingStatus).value
  86. if (stakingStatus instanceof Staked) {
  87. storageProvider.stakeProfile = {
  88. stakeId: stake.stake_id.toNumber(),
  89. roleStake: stakingStatus.staked_amount.toNumber(),
  90. }
  91. }
  92. }
  93. storageProviders.push(storageProvider)
  94. }
  95. // get all active content curators
  96. const contentCuratorKeys = await api.query.contentWorkingGroup.curatorById.keys()
  97. const contentCuratorIds = contentCuratorKeys.map(({ args: [workerId]}) => workerId) as Vec<CuratorId>
  98. contentCuratorIds.sort((a,b) => a.toNumber()-b.toNumber())
  99. console.log('all contentCuratorIds:', contentCuratorIds.join(', '));
  100. for (let key of contentCuratorIds) {
  101. const curator = await api.query.contentWorkingGroup.curatorById(key) as Curator
  102. // filter out inactive
  103. if (curator.is_active) {
  104. const curatorApplicationId = (curator.induction as CuratorInduction).curator_application_id as CuratorApplicationId
  105. const applicationId = await api.query.contentWorkingGroup.curatorApplicationById(curatorApplicationId) as CuratorApplication
  106. const contentCurator: ContentCurator = {
  107. curatorId: key.toNumber(),
  108. memberId: applicationId.member_id.toNumber(),
  109. roleAccount: curator.role_account.toString(),
  110. applicationId: applicationId.application_id.toNumber(),
  111. }
  112. if (curator.reward_relationship.isSome) {
  113. const rewardRelationshipId = curator.reward_relationship.unwrap()
  114. const rewardRelationship = await api.query.recurringRewards.rewardRelationships(rewardRelationshipId) as RewardRelationship
  115. //console.log("rewardRelationship",rewardRelationship.toHuman())
  116. let rewardInterval = 0
  117. let rewardPerWeek = 0
  118. if (!(rewardRelationship.payout_interval.value instanceof Null)) {
  119. rewardInterval = rewardRelationship.payout_interval.unwrap().toNumber()
  120. rewardPerWeek = rewardRelationship.amount_per_payout.toNumber()*100800/rewardInterval
  121. }
  122. contentCurator.rewardRelationship = {
  123. rewardRelationshipId: rewardRelationshipId.toNumber(),
  124. rewardSize: rewardRelationship.amount_per_payout.toNumber(),
  125. rewardInterval,
  126. rewardPerWeek,
  127. totalEarned: rewardRelationship.total_reward_received.toNumber(),
  128. totalMissed: rewardRelationship.total_reward_missed.toNumber(),
  129. }
  130. }
  131. if (curator.role_stake_profile.isSome && !(curator.role_stake_profile instanceof Null)) {
  132. const stake = curator.role_stake_profile.unwrap()
  133. const workerStake = await api.query.stake.stakes(stake.stake_id) as Stake
  134. //console.log("workerStake",workerStake.toHuman())
  135. const stakingStatus = (workerStake.staking_status as StakingStatus).value
  136. if (stakingStatus instanceof Staked) {
  137. contentCurator.stakeProfile = {
  138. stakeId: stake.stake_id.toNumber(),
  139. roleStake: stakingStatus.staked_amount.toNumber(),
  140. }
  141. }
  142. }
  143. contentCurators.push(contentCurator)
  144. }
  145. }
  146. console.log("storageProviders",JSON.stringify(storageProviders, null, 4))
  147. console.log("contentCurators",JSON.stringify(contentCurators, null, 4))
  148. api.disconnect()
  149. }
  150. main()