getters.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 id.toNumber() - 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(
  43. account
  44. );
  45. const handle: string = await memberHandle(api, id);
  46. return handle === "joystream_storage_member" ? "joystream" : handle;
  47. };
  48. // forum
  49. export const categoryById = async (api: Api, id: number): Promise<Category> => {
  50. const category: Category = await api.query.forum.categoryById(id);
  51. return category;
  52. };
  53. export const currentPostId = async (api: Api): Promise<number> => {
  54. const postId: PostId = await api.query.forum.nextPostId();
  55. return postId.toNumber() - 1;
  56. };
  57. export const currentThreadId = async (api: Api): Promise<number> => {
  58. const threadId: ThreadId = await api.query.forum.nextThreadId();
  59. return threadId.toNumber() - 1;
  60. };
  61. export const currentCategoryId = async (api: Api): Promise<number> => {
  62. const categoryId: CategoryId = await api.query.forum.nextCategoryId();
  63. return categoryId.toNumber() - 1;
  64. };
  65. // proposals
  66. export const proposalCount = async (api: Api): Promise<number> => {
  67. const proposalCount: any = await api.query.proposalsEngine.proposalCount();
  68. return proposalCount.toJSON() || 0;
  69. };
  70. export const activeProposalCount = async (api: Api): Promise<number> => {
  71. const proposalCount: number = await api.query.proposalsEngine.activeProposalCount();
  72. return proposalCount || 0;
  73. };
  74. export const pendingProposals = async (api: Api): Promise<ProposalArray> => {
  75. const pending: ProposalArray = await api.query.proposalsEngine.pendingExecutionProposalIds(
  76. await activeProposalCount(api)
  77. );
  78. //const pending: ProposalArray = pendingProposals.toJSON();
  79. if (pending.length) console.debug("pending proposals", pending);
  80. return pending;
  81. };
  82. export const activeProposals = async (api: Api): Promise<ProposalArray> => {
  83. const active: ProposalArray = await api.query.proposalsEngine.activeProposalIds(
  84. await activeProposalCount(api)
  85. );
  86. //const active: ProposalArray = result.toJSON();
  87. if (active.length) console.debug("active proposals", active);
  88. return active;
  89. };
  90. const getProposalType = async (api: Api, id: number): Promise<string> => {
  91. const details: ProposalDetailsOf = await api.query.proposalsCodex.proposalDetailsByProposalId(
  92. id
  93. );
  94. const [type]: string[] = Object.getOwnPropertyNames(details.toJSON());
  95. return type;
  96. };
  97. const isExecuted = (proposalStatus : any) => {
  98. if (!proposalStatus) return null
  99. if (proposalStatus.Approved) return proposalStatus.Approved.toJSON()
  100. return proposalStatus.toJSON()
  101. }
  102. export const proposalDetail = async (
  103. api: Api,
  104. id: number
  105. ): Promise<ProposalDetail> => {
  106. const proposal: Proposal = await api.query.proposalsEngine.proposals(id);
  107. const status: { [key: string]: any } = proposal.status;
  108. const stage: string = status.isActive ? "Active" : "Finalized";
  109. const { finalizedAt, proposalStatus } = status[`as${stage}`];
  110. const result: string = proposalStatus
  111. ? (proposalStatus.isApproved && "Approved") ||
  112. (proposalStatus.isCanceled && "Canceled") ||
  113. (proposalStatus.isExpired && "Expired") ||
  114. (proposalStatus.isRejected && "Rejected") ||
  115. (proposalStatus.isSlashed && "Slashed") ||
  116. (proposalStatus.isVetoed && "Vetoed")
  117. : "Pending";
  118. const executed = isExecuted(proposalStatus)
  119. const { description, parameters, proposerId, votingResults } = proposal;
  120. const author: string = await memberHandle(api, proposerId);
  121. const title: string = proposal.title.toString();
  122. const type: string = await getProposalType(api, id);
  123. // TODO catch ExecutionFailed
  124. const args: string[] = [String(id), title, type, stage, result, author];
  125. const message: string = formatProposalMessage(args);
  126. const createdAt: number = proposal.createdAt.toNumber();
  127. return {
  128. id,
  129. title,
  130. createdAt,
  131. finalizedAt,
  132. parameters,
  133. message,
  134. stage,
  135. result,
  136. executed,
  137. description,
  138. votes: votingResults,
  139. type,
  140. author,
  141. authorId: Number(proposerId)
  142. };
  143. };
  144. // storage providers
  145. export const providerStatus = async (domain: string): Promise<boolean> => {
  146. try {
  147. const res = await fetch(`https://${domain}:5001/api/v0/version`);
  148. return res.status >= 400 ? false : true;
  149. } catch (e) {
  150. return false;
  151. }
  152. };
  153. export const nextOpeningId = async (api: Api): Promise<number> => {
  154. const id = await api.query.storageWorkingGroup.nextOpeningId();
  155. return id.toJSON();
  156. };
  157. export const nextWorkerId = async (api: Api): Promise<number> => {
  158. const id = await api.query.storageWorkingGroup.nextWorkerId();
  159. return id.toJSON();
  160. };