3
0

api.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { ApiPromise } from "@polkadot/api";
  2. import { Option, u32, Vec } from "@polkadot/types";
  3. import type { Codec, Observable } from "@polkadot/types/types";
  4. import {
  5. AccountId,
  6. Balance,
  7. BalanceOf,
  8. BlockNumber,
  9. EraIndex,
  10. EventRecord,
  11. Hash,
  12. Moment,
  13. } from "@polkadot/types/interfaces";
  14. import { SignedBlock } from "@polkadot/types/interfaces/runtime";
  15. import { PostId, ThreadId } from "@joystream/types/common";
  16. import { CategoryId } from "@joystream/types/forum";
  17. import { ElectionStake, SealedVote, Seats } from "@joystream/types/council";
  18. import { Mint, MintId } from "@joystream/types/mint";
  19. import { MemberId, Membership } from "@joystream/types/members";
  20. import { WorkerId } from "@joystream/types/working-group";
  21. import { Stake, StakeId } from "@joystream/types/stake";
  22. import {
  23. RewardRelationship,
  24. RewardRelationshipId,
  25. } from "@joystream/types/recurring-rewards";
  26. import { Entity, EntityId } from "@joystream/types/content-directory";
  27. import { ContentId, DataObject } from "@joystream/types/media";
  28. import { SpendingParams } from "@joystream/types/proposals";
  29. import { ProposalId, WorkerOf } from "@joystream/types/augment-codec/all";
  30. import { ProposalDetails, ProposalOf } from "@joystream/types/augment/types";
  31. // blocks
  32. export const getBlock = (api: ApiPromise, hash: Hash): Promise<SignedBlock> =>
  33. api.rpc.chain.getBlock(hash);
  34. export const getBlockHash = (api: ApiPromise, block: number): Promise<Hash> =>
  35. api.rpc.chain.getBlockHash(block);
  36. export const getTimestamp = (api: ApiPromise, hash: Hash): Promise<Moment> =>
  37. api.query.timestamp.now.at(hash);
  38. export const getIssuance = (api: ApiPromise, hash: Hash): Promise<Balance> =>
  39. api.query.balances.totalIssuance.at(hash);
  40. export const getEvents = (
  41. api: ApiPromise,
  42. hash: Hash
  43. ): Promise<Vec<EventRecord>> => api.query.system.events.at(hash);
  44. export const getEra = (
  45. api: ApiPromise,
  46. hash: Hash
  47. ): Promise<Option<EraIndex>> => api.query.staking.currentEra.at(hash);
  48. export const getEraStake = async (
  49. api: ApiPromise,
  50. hash: Hash,
  51. era: EraIndex
  52. ): Promise<number> =>
  53. (await api.query.staking.erasTotalStake.at(hash, era)).toNumber();
  54. // council
  55. export const getCouncil = (api: ApiPromise, hash: Hash): Promise<Seats> =>
  56. api.query.council.activeCouncil.at(hash);
  57. export const getCouncilRound = async (
  58. api: ApiPromise,
  59. hash: Hash
  60. ): Promise<number> =>
  61. ((await api.query.councilElection.round.at(hash)) as u32).toNumber();
  62. export const getCouncilSize = async (
  63. api: ApiPromise,
  64. hash: Hash
  65. ): Promise<number> =>
  66. ((await api.query.councilElection.councilSize.at(hash)) as u32).toNumber();
  67. export const getCouncilApplicants = (
  68. api: ApiPromise,
  69. hash: Hash
  70. ): Promise<Vec<AccountId>> => api.query.councilElection.applicants.at(hash);
  71. export const getCouncilApplicantStakes = (
  72. api: ApiPromise,
  73. hash: Hash,
  74. applicant: AccountId
  75. ): Promise<ElectionStake> =>
  76. api.query.councilElection.applicantStakes.at(hash, applicant);
  77. export const getCouncilCommitments = (
  78. api: ApiPromise,
  79. hash: Hash
  80. ): Promise<Vec<Hash>> => api.query.councilElection.commitments.at(hash);
  81. export const getCouncilPayoutInterval = (
  82. api: ApiPromise,
  83. hash: Hash
  84. ): Promise<Option<BlockNumber>> => api.query.council.payoutInterval.at(hash);
  85. export const getCouncilPayout = (
  86. api: ApiPromise,
  87. hash: Hash
  88. ): Promise<BalanceOf> => api.query.council.amountPerPayout.at(hash);
  89. const periods = [
  90. "announcingPeriod",
  91. "votingPeriod",
  92. "revealingPeriod",
  93. "newTermDuration",
  94. ];
  95. export const getCouncilPeriods = (
  96. api: ApiPromise,
  97. hash: Hash
  98. ): Promise<number>[] =>
  99. periods.map(async (period: string) =>
  100. ((await api.query.councilElection[period].at(
  101. hash
  102. )) as BlockNumber).toNumber()
  103. );
  104. // working groups
  105. export const getNextWorker = async (
  106. api: ApiPromise,
  107. group: string,
  108. hash: Hash
  109. ): Promise<number> =>
  110. ((await api.query[group].nextWorkerId.at(hash)) as WorkerId).toNumber();
  111. export const getWorker = (
  112. api: ApiPromise,
  113. group: string,
  114. hash: Hash,
  115. id: number
  116. ): Promise<WorkerOf> => api.query[group].workerById.at(hash, id);
  117. export const getWorkers = (
  118. api: ApiPromise,
  119. group: string,
  120. hash: Hash
  121. ): Promise<number> => api.query[group].activeWorkerCount.at(hash);
  122. export const getStake = async (
  123. api: ApiPromise,
  124. id: StakeId | number
  125. ): Promise<Stake> => (await api.query.stake.stakes(id)) as Stake;
  126. export const getWorkerReward = (
  127. api: ApiPromise,
  128. hash: Hash,
  129. id: RewardRelationshipId | number
  130. ): Promise<RewardRelationship> =>
  131. api.query.recurringRewards.rewardRelationships.at(hash, id);
  132. // mints
  133. export const getCouncilMint = (api: ApiPromise, hash: Hash): Promise<MintId> =>
  134. api.query.council.councilMint.at(hash);
  135. export const getGroupMint = (
  136. api: ApiPromise,
  137. group: string,
  138. hash: Hash
  139. ): Promise<MintId> => api.query[group].mint.at(hash);
  140. export const getMintsCreated = async (
  141. api: ApiPromise,
  142. hash: Hash
  143. ): Promise<number> => parseInt(await api.query.minting.mintsCreated.at(hash));
  144. export const getMint = (
  145. api: ApiPromise,
  146. hash: Hash,
  147. id: MintId | number
  148. ): Promise<Mint> => api.query.minting.mints.at(hash, id);
  149. // members
  150. export const getNextMember = async (
  151. api: ApiPromise,
  152. hash: Hash
  153. ): Promise<number> =>
  154. ((await api.query.members.nextMemberId.at(hash)) as MemberId).toNumber();
  155. export const getMember = (
  156. api: ApiPromise,
  157. hash: Hash,
  158. id: MemberId
  159. ): Promise<Membership> => api.query.members.membershipById.at(hash, id);
  160. // forum
  161. export const getNextPost = async (
  162. api: ApiPromise,
  163. hash: Hash
  164. ): Promise<number> =>
  165. ((await api.query.forum.nextPostId.at(hash)) as PostId).toNumber();
  166. export const getNextThread = async (
  167. api: ApiPromise,
  168. hash: Hash
  169. ): Promise<number> =>
  170. ((await api.query.forum.nextThreadId.at(hash)) as ThreadId).toNumber();
  171. export const getNextCategory = async (
  172. api: ApiPromise,
  173. hash: Hash
  174. ): Promise<number> =>
  175. ((await api.query.forum.nextCategoryId.at(hash)) as CategoryId).toNumber();
  176. // proposals
  177. export const getProposalCount = async (
  178. api: ApiPromise,
  179. hash: Hash
  180. ): Promise<number> =>
  181. ((await api.query.proposalsEngine.proposalCount.at(hash)) as u32).toNumber();
  182. export const getProposalInfo = async (
  183. api: ApiPromise,
  184. id: ProposalId
  185. ): Promise<ProposalOf> =>
  186. (await api.query.proposalsEngine.proposals(id)) as ProposalOf;
  187. export const getProposalDetails = async (
  188. api: ApiPromise,
  189. id: ProposalId
  190. ): Promise<ProposalDetails> =>
  191. (await api.query.proposalsCodex.proposalDetailsByProposalId(
  192. id
  193. )) as ProposalDetails;
  194. // validators
  195. export const getValidatorCount = async (
  196. api: ApiPromise,
  197. hash: Hash
  198. ): Promise<number> =>
  199. ((await api.query.staking.validatorCount.at(hash)) as u32).toNumber();
  200. export const getValidators = (
  201. api: ApiPromise,
  202. hash: Hash
  203. ): Promise<Option<Vec<AccountId>>> =>
  204. api.query.staking.snapshotValidators.at(hash);
  205. // media
  206. export const getNextEntity = async (
  207. api: ApiPromise,
  208. hash: Hash
  209. ): Promise<number> =>
  210. ((await api.query.contentDirectory.nextEntityId.at(
  211. hash
  212. )) as EntityId).toNumber();
  213. export const getNextChannel = async (
  214. api: ApiPromise,
  215. hash: Hash
  216. ): Promise<number> => api.query.content.nextChannelId.at(hash);
  217. export const getNextVideo = async (
  218. api: ApiPromise,
  219. hash: Hash
  220. ): Promise<number> => api.query.content.nextVideoId.at(hash);
  221. export const getEntity = (
  222. api: ApiPromise,
  223. hash: Hash,
  224. id: number
  225. ): Promise<Entity> => api.query.contentDirectory.entityById.at(hash, id);
  226. export const getDataObjects = async (
  227. api: ApiPromise
  228. ): Promise<Map<ContentId, DataObject>> =>
  229. ((await api.query.dataDirectory.dataByContentId.entries()) as unknown) as Map<
  230. ContentId,
  231. DataObject
  232. >;
  233. export const getDataObject = async (
  234. api: ApiPromise,
  235. id: ContentId
  236. ): Promise<Option<DataObject>> =>
  237. (await api.query.dataDirectory.dataByContentId(id)) as Option<DataObject>;