1
0

getters.ts 5.3 KB

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