getters.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { formatProposalMessage } from './announcements'
  2. import fetch from 'node-fetch'
  3. //types
  4. import { Api, ProposalArray, ProposalDetail } from '../../types'
  5. import {
  6. ChannelId,
  7. PostId,
  8. ProposalDetailsOf,
  9. ThreadId,
  10. } from '@joystream/types/augment'
  11. import { Category, CategoryId } from '@joystream/types/forum'
  12. import { MemberId, Membership } from '@joystream/types/members'
  13. import { Proposal } from '@joystream/types/proposals'
  14. import { AccountId } from '@polkadot/types/interfaces'
  15. // channel
  16. export const currentChannelId = async (api: Api): Promise<number> => {
  17. const id: ChannelId = await api.query.contentWorkingGroup.nextChannelId()
  18. return Number(id) - 1
  19. }
  20. // members
  21. export const membership = async (
  22. api: Api,
  23. id: MemberId | number
  24. ): Promise<Membership> => {
  25. return await api.query.members.membershipById(id)
  26. }
  27. export const memberHandle = async (api: Api, id: MemberId): Promise<string> => {
  28. const member: Membership = await membership(api, id)
  29. return member.handle.toJSON()
  30. }
  31. export const memberIdByAccount = async (
  32. api: Api,
  33. account: AccountId | string
  34. ): Promise<MemberId | number> => {
  35. const ids = await api.query.members.memberIdsByRootAccountId(account)
  36. return ids.length ? ids[0] : 0
  37. }
  38. export const memberHandleByAccount = async (
  39. api: Api,
  40. account: AccountId | string
  41. ): Promise<string> => {
  42. const id: MemberId = await api.query.members.memberIdsByRootAccountId(account)
  43. const handle: string = await memberHandle(api, id)
  44. return handle === 'joystream_storage_member' ? 'joystream' : handle
  45. }
  46. // forum
  47. export const categoryById = async (api: Api, id: number): Promise<Category> => {
  48. const category: Category = await api.query.forum.categoryById(id)
  49. return category
  50. }
  51. export const currentPostId = async (api: Api): Promise<number> => {
  52. const postId: PostId = await api.query.forum.nextPostId()
  53. return Number(postId) - 1
  54. }
  55. export const currentThreadId = async (api: Api): Promise<number> => {
  56. const threadId: ThreadId = await api.query.forum.nextThreadId()
  57. return Number(threadId) - 1
  58. }
  59. export const currentCategoryId = async (api: Api): Promise<number> => {
  60. const categoryId: CategoryId = await api.query.forum.nextCategoryId()
  61. return Number(categoryId) - 1
  62. }
  63. // proposals
  64. export const proposalCount = async (api: Api): Promise<number> => {
  65. const proposalCount: any = await api.query.proposalsEngine.proposalCount()
  66. return proposalCount.toJSON() || 0
  67. }
  68. export const activeProposalCount = async (api: Api): Promise<number> => {
  69. const proposalCount: number = await api.query.proposalsEngine.activeProposalCount()
  70. return proposalCount || 0
  71. }
  72. export const pendingProposals = async (api: Api): Promise<ProposalArray> => {
  73. const pending: ProposalArray = await api.query.proposalsEngine.pendingExecutionProposalIds(
  74. await activeProposalCount(api)
  75. )
  76. //const pending: ProposalArray = pendingProposals.toJSON();
  77. if (pending.length) console.debug('pending proposals', pending)
  78. return pending
  79. }
  80. export const activeProposals = async (api: Api): Promise<ProposalArray> => {
  81. const active: ProposalArray = await api.query.proposalsEngine.activeProposalIds(
  82. await activeProposalCount(api)
  83. )
  84. //const active: ProposalArray = result.toJSON();
  85. if (active.length) console.debug('active proposals', active)
  86. return active
  87. }
  88. const getProposalType = async (api: Api, id: number): Promise<string> => {
  89. const details: ProposalDetailsOf = await api.query.proposalsCodex.proposalDetailsByProposalId(
  90. id
  91. )
  92. const [type]: string[] = Object.getOwnPropertyNames(details.toJSON())
  93. return type
  94. }
  95. export const proposalDetail = async (
  96. api: Api,
  97. id: number
  98. ): Promise<ProposalDetail> => {
  99. const proposal: Proposal = await api.query.proposalsEngine.proposals(id)
  100. const status: { [key: string]: any } = proposal.status
  101. const stage: string = status.isActive ? 'Active' : 'Finalized'
  102. const { finalizedAt, proposalStatus } = status[`as${stage}`]
  103. const result: string = proposalStatus
  104. ? (proposalStatus.isApproved && 'Approved') ||
  105. (proposalStatus.isCanceled && 'Canceled') ||
  106. (proposalStatus.isExpired && 'Expired') ||
  107. (proposalStatus.isRejected && 'Rejected') ||
  108. (proposalStatus.isSlashed && 'Slashed') ||
  109. (proposalStatus.isVetoed && 'Vetoed')
  110. : 'Pending'
  111. const exec = proposalStatus ? proposalStatus['Approved'] : null
  112. const { description, parameters, proposerId, votingResults } = proposal
  113. const author: string = await memberHandle(api, proposerId)
  114. const title: string = proposal.title.toString()
  115. const type: string = await getProposalType(api, id)
  116. const args: string[] = [String(id), title, type, stage, result, author]
  117. const message: string = formatProposalMessage(args)
  118. const createdAt: number = Number(proposal.createdAt)
  119. return {
  120. id,
  121. title,
  122. createdAt,
  123. finalizedAt,
  124. parameters: JSON.stringify(parameters),
  125. message,
  126. stage,
  127. result,
  128. exec,
  129. description,
  130. votes: votingResults,
  131. type,
  132. author,
  133. authorId: Number(proposerId),
  134. }
  135. }
  136. // storage providers
  137. export const providerStatus = async (domain: string): Promise<boolean> => {
  138. try {
  139. const res = await fetch(`https://${domain}:5001/api/v0/version`)
  140. return res.status >= 400 ? false : true
  141. } catch (e) {
  142. return false
  143. }
  144. }
  145. export const nextOpeningId = async (api: Api): Promise<number> => {
  146. const id = await api.query.storageWorkingGroup.nextOpeningId()
  147. return id.toJSON()
  148. }
  149. export const nextWorkerId = async (api: Api): Promise<number> => {
  150. const id = await api.query.storageWorkingGroup.nextWorkerId()
  151. return id.toJSON()
  152. }