@pezkuwi/api-derive 16.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (304) hide show
  1. package/README.md +30 -0
  2. package/build/accounts/accountId.d.ts +17 -0
  3. package/build/accounts/flags.d.ts +29 -0
  4. package/build/accounts/idAndIndex.d.ts +15 -0
  5. package/build/accounts/idToIndex.d.ts +16 -0
  6. package/build/accounts/identity.d.ts +48 -0
  7. package/build/accounts/index.d.ts +8 -0
  8. package/build/accounts/indexToId.d.ts +15 -0
  9. package/build/accounts/indexes.d.ts +16 -0
  10. package/build/accounts/info.d.ts +18 -0
  11. package/build/accounts/types.d.ts +39 -0
  12. package/build/alliance/index.d.ts +70 -0
  13. package/build/augment.d.ts +1 -0
  14. package/build/bagsList/get.d.ts +12 -0
  15. package/build/bagsList/getExpanded.d.ts +16 -0
  16. package/build/bagsList/index.d.ts +3 -0
  17. package/build/bagsList/listNodes.d.ts +9 -0
  18. package/build/bagsList/types.d.ts +13 -0
  19. package/build/bagsList/util.d.ts +2 -0
  20. package/build/balances/account.d.ts +17 -0
  21. package/build/balances/all.d.ts +20 -0
  22. package/build/balances/index.d.ts +20 -0
  23. package/build/balances/types.d.ts +69 -0
  24. package/build/balances/votingBalances.d.ts +15 -0
  25. package/build/bounties/bounties.d.ts +12 -0
  26. package/build/bounties/helpers/filterBountyProposals.d.ts +2 -0
  27. package/build/bounties/index.d.ts +1 -0
  28. package/build/bundle.d.ts +9 -0
  29. package/build/chain/bestNumber.d.ts +11 -0
  30. package/build/chain/bestNumberFinalized.d.ts +12 -0
  31. package/build/chain/bestNumberLag.d.ts +15 -0
  32. package/build/chain/getBlock.d.ts +15 -0
  33. package/build/chain/getBlockByNumber.d.ts +16 -0
  34. package/build/chain/getHeader.d.ts +16 -0
  35. package/build/chain/index.d.ts +10 -0
  36. package/build/chain/subscribeFinalizedBlocks.d.ts +14 -0
  37. package/build/chain/subscribeFinalizedHeads.d.ts +22 -0
  38. package/build/chain/subscribeNewBlocks.d.ts +14 -0
  39. package/build/chain/subscribeNewHeads.d.ts +15 -0
  40. package/build/chain/util.d.ts +9 -0
  41. package/build/collective/helpers.d.ts +6 -0
  42. package/build/collective/index.d.ts +3 -0
  43. package/build/collective/members.d.ts +2 -0
  44. package/build/collective/prime.d.ts +2 -0
  45. package/build/collective/proposals.d.ts +6 -0
  46. package/build/collective/types.d.ts +19 -0
  47. package/build/contracts/fees.d.ts +14 -0
  48. package/build/contracts/index.d.ts +1 -0
  49. package/build/council/index.d.ts +72 -0
  50. package/build/council/types.d.ts +6 -0
  51. package/build/council/votes.d.ts +11 -0
  52. package/build/council/votesOf.d.ts +16 -0
  53. package/build/crowdloan/childKey.d.ts +15 -0
  54. package/build/crowdloan/contributions.d.ts +14 -0
  55. package/build/crowdloan/index.d.ts +3 -0
  56. package/build/crowdloan/ownContributions.d.ts +15 -0
  57. package/build/crowdloan/types.d.ts +6 -0
  58. package/build/crowdloan/util.d.ts +10 -0
  59. package/build/democracy/dispatchQueue.d.ts +12 -0
  60. package/build/democracy/index.d.ts +11 -0
  61. package/build/democracy/locks.d.ts +14 -0
  62. package/build/democracy/nextExternal.d.ts +12 -0
  63. package/build/democracy/preimages.d.ts +24 -0
  64. package/build/democracy/proposals.d.ts +12 -0
  65. package/build/democracy/referendumIds.d.ts +12 -0
  66. package/build/democracy/referendums.d.ts +11 -0
  67. package/build/democracy/referendumsActive.d.ts +12 -0
  68. package/build/democracy/referendumsFinished.d.ts +15 -0
  69. package/build/democracy/referendumsInfo.d.ts +23 -0
  70. package/build/democracy/sqrtElectorate.d.ts +13 -0
  71. package/build/democracy/types.d.ts +70 -0
  72. package/build/democracy/util.d.ts +18 -0
  73. package/build/derive.d.ts +52 -0
  74. package/build/elections/index.d.ts +1 -0
  75. package/build/elections/info.d.ts +14 -0
  76. package/build/elections/types.d.ts +18 -0
  77. package/build/imOnline/index.d.ts +1 -0
  78. package/build/imOnline/receivedHeartbeats.d.ts +13 -0
  79. package/build/index.d.ts +2 -0
  80. package/build/membership/index.d.ts +70 -0
  81. package/build/packageDetect.d.ts +1 -0
  82. package/build/packageInfo.d.ts +6 -0
  83. package/build/parachains/index.d.ts +2 -0
  84. package/build/parachains/info.d.ts +19 -0
  85. package/build/parachains/overview.d.ts +15 -0
  86. package/build/parachains/types.d.ts +38 -0
  87. package/build/parachains/util.d.ts +3 -0
  88. package/build/session/index.d.ts +3 -0
  89. package/build/session/indexes.d.ts +15 -0
  90. package/build/session/info.d.ts +13 -0
  91. package/build/session/progress.d.ts +47 -0
  92. package/build/session/types.d.ts +19 -0
  93. package/build/society/candidates.d.ts +12 -0
  94. package/build/society/index.d.ts +4 -0
  95. package/build/society/info.d.ts +12 -0
  96. package/build/society/member.d.ts +14 -0
  97. package/build/society/members.d.ts +14 -0
  98. package/build/society/types.d.ts +27 -0
  99. package/build/staking/account.d.ts +31 -0
  100. package/build/staking/cache.d.ts +12 -0
  101. package/build/staking/currentPoints.d.ts +13 -0
  102. package/build/staking/electedInfo.d.ts +23 -0
  103. package/build/staking/erasExposure.d.ts +30 -0
  104. package/build/staking/erasHistoric.d.ts +8 -0
  105. package/build/staking/erasPoints.d.ts +18 -0
  106. package/build/staking/erasPrefs.d.ts +27 -0
  107. package/build/staking/erasRewards.d.ts +14 -0
  108. package/build/staking/erasSlashes.d.ts +26 -0
  109. package/build/staking/index.d.ts +22 -0
  110. package/build/staking/keys.d.ts +35 -0
  111. package/build/staking/overview.d.ts +19 -0
  112. package/build/staking/ownExposure.d.ts +37 -0
  113. package/build/staking/ownSlashes.d.ts +35 -0
  114. package/build/staking/query.d.ts +31 -0
  115. package/build/staking/stakerExposure.d.ts +33 -0
  116. package/build/staking/stakerPoints.d.ts +22 -0
  117. package/build/staking/stakerPrefs.d.ts +24 -0
  118. package/build/staking/stakerRewards.d.ts +49 -0
  119. package/build/staking/stakerSlashes.d.ts +22 -0
  120. package/build/staking/stashes.d.ts +16 -0
  121. package/build/staking/types.d.ts +139 -0
  122. package/build/staking/util.d.ts +14 -0
  123. package/build/staking/validators.d.ts +33 -0
  124. package/build/staking/waitingInfo.d.ts +16 -0
  125. package/build/technicalCommittee/index.d.ts +70 -0
  126. package/build/treasury/index.d.ts +1 -0
  127. package/build/treasury/proposals.d.ts +12 -0
  128. package/build/tx/constants.d.ts +5 -0
  129. package/build/tx/events.d.ts +22 -0
  130. package/build/tx/extrinsicInfo.d.ts +61 -0
  131. package/build/tx/index.d.ts +3 -0
  132. package/build/tx/signingInfo.d.ts +25 -0
  133. package/build/type/HeaderExtended.d.ts +4 -0
  134. package/build/type/SignedBlockExtended.d.ts +4 -0
  135. package/build/type/index.d.ts +2 -0
  136. package/build/type/types.d.ts +15 -0
  137. package/build/type/util.d.ts +2 -0
  138. package/build/types.d.ts +79 -0
  139. package/build/util/approvalFlagsToBools.d.ts +4 -0
  140. package/build/util/blockNumber.d.ts +7 -0
  141. package/build/util/cache.d.ts +4 -0
  142. package/build/util/cacheImpl.d.ts +3 -0
  143. package/build/util/first.d.ts +4 -0
  144. package/build/util/index.d.ts +7 -0
  145. package/build/util/lazy.d.ts +4 -0
  146. package/build/util/types.d.ts +6 -0
  147. package/package.json +42 -0
  148. package/src/accounts/accountId.ts +44 -0
  149. package/src/accounts/flags.ts +90 -0
  150. package/src/accounts/idAndIndex.ts +51 -0
  151. package/src/accounts/idToIndex.ts +31 -0
  152. package/src/accounts/identity.ts +235 -0
  153. package/src/accounts/index.ts +11 -0
  154. package/src/accounts/indexToId.ts +33 -0
  155. package/src/accounts/indexes.ts +56 -0
  156. package/src/accounts/info.ts +58 -0
  157. package/src/accounts/types.ts +48 -0
  158. package/src/alliance/index.ts +76 -0
  159. package/src/augment.ts +4 -0
  160. package/src/bagsList/get.ts +79 -0
  161. package/src/bagsList/getExpanded.ts +41 -0
  162. package/src/bagsList/index.ts +6 -0
  163. package/src/bagsList/listNodes.ts +50 -0
  164. package/src/bagsList/types.ts +18 -0
  165. package/src/bagsList/util.ts +14 -0
  166. package/src/balances/account.ts +210 -0
  167. package/src/balances/all.ts +246 -0
  168. package/src/balances/index.ts +26 -0
  169. package/src/balances/types.ts +79 -0
  170. package/src/balances/votingBalances.ts +33 -0
  171. package/src/bounties/bounties.spec.ts +162 -0
  172. package/src/bounties/bounties.ts +76 -0
  173. package/src/bounties/helpers/filterBountyProposals.ts +13 -0
  174. package/src/bounties/index.ts +4 -0
  175. package/src/bundle.ts +140 -0
  176. package/src/chain/bestNumber.ts +21 -0
  177. package/src/chain/bestNumberFinalized.ts +22 -0
  178. package/src/chain/bestNumberLag.ts +33 -0
  179. package/src/chain/getBlock.ts +43 -0
  180. package/src/chain/getBlockByNumber.ts +30 -0
  181. package/src/chain/getHeader.ts +37 -0
  182. package/src/chain/index.ts +13 -0
  183. package/src/chain/subscribeFinalizedBlocks.ts +30 -0
  184. package/src/chain/subscribeFinalizedHeads.ts +62 -0
  185. package/src/chain/subscribeNewBlocks.ts +30 -0
  186. package/src/chain/subscribeNewHeads.ts +38 -0
  187. package/src/chain/util.ts +112 -0
  188. package/src/checkTypes.manual.ts +8 -0
  189. package/src/collective/helpers.ts +36 -0
  190. package/src/collective/index.ts +6 -0
  191. package/src/collective/members.ts +8 -0
  192. package/src/collective/prime.ts +25 -0
  193. package/src/collective/proposals.ts +73 -0
  194. package/src/collective/types.ts +30 -0
  195. package/src/contracts/fees.ts +61 -0
  196. package/src/contracts/index.ts +4 -0
  197. package/src/council/index.ts +79 -0
  198. package/src/council/types.ts +11 -0
  199. package/src/council/votes.ts +97 -0
  200. package/src/council/votesOf.ts +35 -0
  201. package/src/crowdloan/childKey.ts +57 -0
  202. package/src/crowdloan/contributions.ts +142 -0
  203. package/src/crowdloan/index.ts +6 -0
  204. package/src/crowdloan/ownContributions.ts +81 -0
  205. package/src/crowdloan/types.ts +11 -0
  206. package/src/crowdloan/util.ts +33 -0
  207. package/src/democracy/dispatchQueue.ts +158 -0
  208. package/src/democracy/index.ts +14 -0
  209. package/src/democracy/locks.ts +114 -0
  210. package/src/democracy/nextExternal.ts +49 -0
  211. package/src/democracy/preimages.ts +171 -0
  212. package/src/democracy/proposals.ts +81 -0
  213. package/src/democracy/referendumIds.ts +37 -0
  214. package/src/democracy/referendums.ts +39 -0
  215. package/src/democracy/referendumsActive.ts +30 -0
  216. package/src/democracy/referendumsFinished.ts +37 -0
  217. package/src/democracy/referendumsInfo.ts +181 -0
  218. package/src/democracy/sqrtElectorate.ts +29 -0
  219. package/src/democracy/types.ts +84 -0
  220. package/src/democracy/util.ts +166 -0
  221. package/src/derive.ts +41 -0
  222. package/src/elections/index.ts +4 -0
  223. package/src/elections/info.ts +126 -0
  224. package/src/elections/types.ts +22 -0
  225. package/src/imOnline/index.ts +4 -0
  226. package/src/imOnline/receivedHeartbeats.ts +65 -0
  227. package/src/index.spec.ts +103 -0
  228. package/src/index.ts +6 -0
  229. package/src/membership/index.ts +76 -0
  230. package/src/mod.ts +4 -0
  231. package/src/packageDetect.ts +11 -0
  232. package/src/packageInfo.ts +6 -0
  233. package/src/parachains/index.ts +5 -0
  234. package/src/parachains/info.ts +111 -0
  235. package/src/parachains/overview.ts +63 -0
  236. package/src/parachains/types.ts +46 -0
  237. package/src/parachains/util.ts +11 -0
  238. package/src/session/index.ts +6 -0
  239. package/src/session/indexes.ts +90 -0
  240. package/src/session/info.ts +39 -0
  241. package/src/session/progress.ts +130 -0
  242. package/src/session/types.ts +25 -0
  243. package/src/society/candidates.ts +71 -0
  244. package/src/society/index.ts +7 -0
  245. package/src/society/info.ts +50 -0
  246. package/src/society/member.ts +28 -0
  247. package/src/society/members.ts +107 -0
  248. package/src/society/types.ts +33 -0
  249. package/src/staking/account.ts +112 -0
  250. package/src/staking/cache.ts +48 -0
  251. package/src/staking/currentPoints.ts +28 -0
  252. package/src/staking/electedInfo.ts +54 -0
  253. package/src/staking/erasExposure.ts +105 -0
  254. package/src/staking/erasHistoric.ts +47 -0
  255. package/src/staking/erasPoints.ts +67 -0
  256. package/src/staking/erasPrefs.ts +63 -0
  257. package/src/staking/erasRewards.ts +52 -0
  258. package/src/staking/erasSlashes.ts +69 -0
  259. package/src/staking/index.ts +25 -0
  260. package/src/staking/keys.ts +86 -0
  261. package/src/staking/overview.ts +42 -0
  262. package/src/staking/ownExposure.ts +83 -0
  263. package/src/staking/ownSlashes.ts +68 -0
  264. package/src/staking/query.ts +230 -0
  265. package/src/staking/stakerExposure.ts +78 -0
  266. package/src/staking/stakerPoints.ts +46 -0
  267. package/src/staking/stakerPrefs.ts +45 -0
  268. package/src/staking/stakerRewards.ts +298 -0
  269. package/src/staking/stakerSlashes.ts +45 -0
  270. package/src/staking/stashes.ts +55 -0
  271. package/src/staking/types.ts +174 -0
  272. package/src/staking/util.ts +94 -0
  273. package/src/staking/validators.ts +82 -0
  274. package/src/staking/waitingInfo.ts +45 -0
  275. package/src/technicalCommittee/index.ts +76 -0
  276. package/src/test/bountyFactory.ts +39 -0
  277. package/src/test/bytesFactory.ts +15 -0
  278. package/src/test/helpers.ts +23 -0
  279. package/src/test/proposalFactory.ts +25 -0
  280. package/src/treasury/index.ts +4 -0
  281. package/src/treasury/proposals.ts +109 -0
  282. package/src/tx/constants.ts +13 -0
  283. package/src/tx/events.ts +43 -0
  284. package/src/tx/extrinsicInfo.ts +123 -0
  285. package/src/tx/index.ts +6 -0
  286. package/src/tx/signingInfo.ts +129 -0
  287. package/src/type/HeaderExtended.ts +33 -0
  288. package/src/type/SignedBlockExtended.ts +75 -0
  289. package/src/type/index.ts +5 -0
  290. package/src/type/types.ts +21 -0
  291. package/src/type/util.ts +45 -0
  292. package/src/types.ts +97 -0
  293. package/src/util/approvalFlagToBools.spec.ts +37 -0
  294. package/src/util/approvalFlagsToBools.ts +26 -0
  295. package/src/util/blockNumber.ts +15 -0
  296. package/src/util/cache.ts +65 -0
  297. package/src/util/cacheImpl.ts +30 -0
  298. package/src/util/first.ts +20 -0
  299. package/src/util/index.ts +13 -0
  300. package/src/util/lazy.ts +16 -0
  301. package/src/util/types.ts +9 -0
  302. package/tsconfig.build.json +21 -0
  303. package/tsconfig.build.tsbuildinfo +1 -0
  304. package/tsconfig.spec.json +27 -0
@@ -0,0 +1,97 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { QueryableModuleStorage } from '@pezkuwi/api-base/types';
6
+ import type { Vec } from '@pezkuwi/types';
7
+ import type { AccountId, Balance } from '@pezkuwi/types/interfaces';
8
+ import type { PalletElectionsPhragmenVoter } from '@pezkuwi/types/lookup';
9
+ import type { ITuple } from '@pezkuwi/types/types';
10
+ import type { DeriveApi, DeriveCouncilVote, DeriveCouncilVotes } from '../types.js';
11
+
12
+ import { combineLatest, map, of } from 'rxjs';
13
+
14
+ import { memo } from '../util/index.js';
15
+
16
+ // Voter is current tuple is 2.x-era
17
+ type VoteEntry = PalletElectionsPhragmenVoter | ITuple<[Balance, Vec<AccountId>]>;
18
+
19
+ function isVoter (value: VoteEntry): value is PalletElectionsPhragmenVoter {
20
+ return !Array.isArray(value);
21
+ }
22
+
23
+ function retrieveStakeOf (elections: QueryableModuleStorage<'rxjs'>): Observable<[AccountId, Balance][]> {
24
+ return elections['stakeOf'].entries<Balance, [AccountId]>().pipe(
25
+ map((entries) =>
26
+ entries.map(([{ args: [accountId] }, stake]) => [accountId, stake])
27
+ )
28
+ );
29
+ }
30
+
31
+ function retrieveVoteOf (elections: DeriveApi['query']['elections']): Observable<[AccountId, AccountId[]][]> {
32
+ return elections['votesOf'].entries<Vec<AccountId>, [AccountId]>().pipe(
33
+ map((entries) =>
34
+ entries.map(([{ args: [accountId] }, votes]) => [accountId, votes])
35
+ )
36
+ );
37
+ }
38
+
39
+ function retrievePrev (api: DeriveApi, elections: DeriveApi['query']['elections']): Observable<DeriveCouncilVotes> {
40
+ return combineLatest([
41
+ retrieveStakeOf(elections),
42
+ retrieveVoteOf(elections)
43
+ ]).pipe(
44
+ map(([stakes, votes]): DeriveCouncilVotes => {
45
+ const result: DeriveCouncilVotes = [];
46
+
47
+ votes.forEach(([voter, votes]): void => {
48
+ result.push([voter, { stake: api.registry.createType('Balance'), votes }]);
49
+ });
50
+
51
+ stakes.forEach(([staker, stake]): void => {
52
+ const entry = result.find(([voter]) => voter.eq(staker));
53
+
54
+ if (entry) {
55
+ entry[1].stake = stake;
56
+ } else {
57
+ result.push([staker, { stake, votes: [] }]);
58
+ }
59
+ });
60
+
61
+ return result;
62
+ })
63
+ );
64
+ }
65
+
66
+ function retrieveCurrent (elections: DeriveApi['query']['elections']): Observable<DeriveCouncilVotes> {
67
+ return elections.voting.entries<VoteEntry, [AccountId]>().pipe(
68
+ map((entries): DeriveCouncilVotes =>
69
+ entries.map(([{ args: [accountId] }, value]): [AccountId, DeriveCouncilVote] => [
70
+ accountId,
71
+ isVoter(value)
72
+ ? { stake: value.stake, votes: value.votes }
73
+ : { stake: value[0], votes: value[1] }
74
+ ])
75
+ )
76
+ );
77
+ }
78
+
79
+ /**
80
+ * @name votes
81
+ * @description Retrieves the council election votes for all participants.
82
+ * @example
83
+ * ```javascript
84
+ * const votes = await api.derive.council.votes();
85
+ * ```
86
+ */
87
+ export function votes (instanceId: string, api: DeriveApi): () => Observable<DeriveCouncilVotes> {
88
+ const elections = api.query.elections || api.query['phragmenElection'] || api.query['electionsPhragmen'];
89
+
90
+ return memo(instanceId, (): Observable<DeriveCouncilVotes> =>
91
+ elections
92
+ ? elections['stakeOf']
93
+ ? retrievePrev(api, elections)
94
+ : retrieveCurrent(elections)
95
+ : of([])
96
+ );
97
+ }
@@ -0,0 +1,35 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { AccountId } from '@pezkuwi/types/interfaces';
6
+ import type { DeriveApi, DeriveCouncilVote } from '../types.js';
7
+
8
+ import { map } from 'rxjs';
9
+
10
+ import { memo } from '../util/index.js';
11
+
12
+ /**
13
+ * @name votesOf
14
+ * @description Retrieves the council votes associated with a given account.
15
+ * @returns The stake and the list of candidates the account has voted for.
16
+ * @param {string | Uint8Array | AccountId} accountId The accountId to retrieve votes for.
17
+ * @example
18
+ * ```javascript
19
+ * const accountId = "5Gw3s7qQ9Z..."; // Replace with a valid account ID
20
+ * const votes = await api.derive.council.votesOf(accountId);
21
+ * console.log("Account votes:", votes);
22
+ * ```
23
+ */
24
+ export function votesOf (instanceId: string, api: DeriveApi): (accountId: string | Uint8Array | AccountId) => Observable<DeriveCouncilVote> {
25
+ return memo(instanceId, (accountId: string | Uint8Array | AccountId): Observable<DeriveCouncilVote> =>
26
+ api.derive.council.votes().pipe(
27
+ map((votes): DeriveCouncilVote =>
28
+ (
29
+ votes.find(([from]) => from.eq(accountId)) ||
30
+ [null, { stake: api.registry.createType('Balance'), votes: [] as AccountId[] }]
31
+ )[1]
32
+ )
33
+ )
34
+ );
35
+ }
@@ -0,0 +1,57 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { Option, u32 } from '@pezkuwi/types';
6
+ import type { PolkadotRuntimeCommonCrowdloanFundInfo } from '@pezkuwi/types/lookup';
7
+ import type { BN } from '@pezkuwi/util';
8
+ import type { DeriveApi } from '../types.js';
9
+
10
+ import { map } from 'rxjs';
11
+
12
+ import { u8aConcat, u8aToHex } from '@pezkuwi/util';
13
+ import { blake2AsU8a } from '@pezkuwi/util-crypto';
14
+
15
+ import { memo } from '../util/index.js';
16
+
17
+ interface AllInfo extends PolkadotRuntimeCommonCrowdloanFundInfo {
18
+ // previously it was named trieIndex
19
+ trieIndex?: u32;
20
+ }
21
+
22
+ function createChildKey (info: AllInfo): string {
23
+ return u8aToHex(
24
+ u8aConcat(
25
+ ':child_storage:default:',
26
+ blake2AsU8a(
27
+ u8aConcat(
28
+ 'crowdloan',
29
+ (info.fundIndex || info.trieIndex).toU8a()
30
+ )
31
+ )
32
+ )
33
+ );
34
+ }
35
+
36
+ /**
37
+ * @name childKey
38
+ * @description Retrieves the child storage key for a given parachain’s crowdloan contributions.
39
+ * This key is used to access contribution data stored in a separate child trie of the blockchain’s state.
40
+ * @param {string | number | BN} paraId The parachain ID for which contributions are being queried.
41
+ * @example
42
+ * ```javascript
43
+ * const childKey = await api.derive.crowdloan.childKey(3369);
44
+ * console.log("Child Key:", childKey);
45
+ * ```
46
+ */
47
+ export function childKey (instanceId: string, api: DeriveApi): (paraId: string | number | BN) => Observable<string | null> {
48
+ return memo(instanceId, (paraId: string | number | BN): Observable<string | null> =>
49
+ api.query['crowdloan']['funds']<Option<PolkadotRuntimeCommonCrowdloanFundInfo>>(paraId).pipe(
50
+ map((optInfo) =>
51
+ optInfo.isSome
52
+ ? createChildKey(optInfo.unwrap())
53
+ : null
54
+ )
55
+ )
56
+ );
57
+ }
@@ -0,0 +1,142 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { StorageKey } from '@pezkuwi/types';
6
+ import type { BN } from '@pezkuwi/util';
7
+ import type { DeriveApi, DeriveContributions } from '../types.js';
8
+
9
+ import { BehaviorSubject, combineLatest, EMPTY, map, of, startWith, switchMap, tap, toArray } from 'rxjs';
10
+
11
+ import { arrayFlatten, isFunction, nextTick } from '@pezkuwi/util';
12
+
13
+ import { memo } from '../util/index.js';
14
+ import { extractContributed } from './util.js';
15
+
16
+ interface Changes {
17
+ added: string[];
18
+ blockHash: string;
19
+ removed: string[];
20
+ }
21
+
22
+ const PAGE_SIZE_K = 1000; // limit aligned with the 1k on the node (trie lookups are heavy)
23
+
24
+ function _getUpdates (api: DeriveApi, paraId: string | number | BN): Observable<Changes> {
25
+ let added: string[] = [];
26
+ let removed: string[] = [];
27
+
28
+ return api.query.system.events().pipe(
29
+ switchMap((events): Observable<Changes> => {
30
+ const changes = extractContributed(paraId, events);
31
+
32
+ if (changes.added.length || changes.removed.length) {
33
+ added = added.concat(...changes.added);
34
+ removed = removed.concat(...changes.removed);
35
+
36
+ return of({ added, addedDelta: changes.added, blockHash: events.createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
37
+ }
38
+
39
+ return EMPTY;
40
+ }),
41
+ startWith({ added, addedDelta: [], blockHash: '-', removed, removedDelta: [] })
42
+ );
43
+ }
44
+
45
+ function _eventTriggerAll (api: DeriveApi, paraId: string | number | BN): Observable<string> {
46
+ return api.query.system.events().pipe(
47
+ switchMap((events): Observable<string> => {
48
+ const items = events.filter(({ event: { data: [eventParaId], method, section } }) =>
49
+ section === 'crowdloan' &&
50
+ ['AllRefunded', 'Dissolved', 'PartiallyRefunded'].includes(method) &&
51
+ eventParaId.eq(paraId)
52
+ );
53
+
54
+ return items.length
55
+ ? of(events.createdAtHash?.toHex() || '-')
56
+ : EMPTY;
57
+ }),
58
+ startWith('-')
59
+ );
60
+ }
61
+
62
+ function _getKeysPaged (api: DeriveApi, childKey: string): Observable<StorageKey[]> {
63
+ const subject = new BehaviorSubject<string | undefined>(undefined);
64
+
65
+ return subject.pipe(
66
+ switchMap((startKey) =>
67
+ api.rpc.childstate.getKeysPaged(childKey, '0x', PAGE_SIZE_K, startKey)
68
+ ),
69
+ tap((keys): void => {
70
+ nextTick((): void => {
71
+ keys.length === PAGE_SIZE_K
72
+ ? subject.next(keys[PAGE_SIZE_K - 1].toHex())
73
+ : subject.complete();
74
+ });
75
+ }),
76
+ toArray(), // toArray since we want to startSubject to be completed
77
+ map((keyArr: StorageKey[][]) => arrayFlatten(keyArr))
78
+ );
79
+ }
80
+
81
+ function _getAll (api: DeriveApi, paraId: string | number | BN, childKey: string): Observable<string[]> {
82
+ return _eventTriggerAll(api, paraId).pipe(
83
+ switchMap(() =>
84
+ isFunction(api.rpc.childstate.getKeysPaged)
85
+ ? _getKeysPaged(api, childKey)
86
+ : api.rpc.childstate.getKeys(childKey, '0x')
87
+ ),
88
+ map((keys) =>
89
+ keys.map((k) => k.toHex())
90
+ )
91
+ );
92
+ }
93
+
94
+ function _contributions (api: DeriveApi, paraId: string | number | BN, childKey: string): Observable<DeriveContributions> {
95
+ return combineLatest([
96
+ _getAll(api, paraId, childKey),
97
+ _getUpdates(api, paraId)
98
+ ]).pipe(
99
+ map(([keys, { added, blockHash, removed }]): DeriveContributions => {
100
+ const contributorsMap: Record<string, boolean> = {};
101
+
102
+ keys.forEach((k): void => {
103
+ contributorsMap[k] = true;
104
+ });
105
+
106
+ added.forEach((k): void => {
107
+ contributorsMap[k] = true;
108
+ });
109
+
110
+ removed.forEach((k): void => {
111
+ delete contributorsMap[k];
112
+ });
113
+
114
+ return {
115
+ blockHash,
116
+ contributorsHex: Object.keys(contributorsMap)
117
+ };
118
+ })
119
+ );
120
+ }
121
+
122
+ /**
123
+ * @name contributions
124
+ * @description Retrieves all contributions for a given parachain crowdloan.
125
+ * @param {string | number | BN} paraId The parachain ID for which contributions are being queried.
126
+ * @example
127
+ * ```javascript
128
+ * const contributions = await api.derive.crowdloan.contributions(3369);
129
+ * console.log("Contributions:", contributions);
130
+ * ```
131
+ */
132
+ export function contributions (instanceId: string, api: DeriveApi): (paraId: string | number | BN) => Observable<DeriveContributions> {
133
+ return memo(instanceId, (paraId: string | number | BN): Observable<DeriveContributions> =>
134
+ api.derive.crowdloan.childKey(paraId).pipe(
135
+ switchMap((childKey) =>
136
+ childKey
137
+ ? _contributions(api, paraId, childKey)
138
+ : of({ blockHash: '-', contributorsHex: [] })
139
+ )
140
+ )
141
+ );
142
+ }
@@ -0,0 +1,6 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './childKey.js';
5
+ export * from './contributions.js';
6
+ export * from './ownContributions.js';
@@ -0,0 +1,81 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { BN } from '@pezkuwi/util';
6
+ import type { DeriveApi, DeriveOwnContributions } from '../types.js';
7
+
8
+ import { combineLatest, EMPTY, map, of, startWith, switchMap } from 'rxjs';
9
+
10
+ import { objectSpread } from '@pezkuwi/util';
11
+
12
+ import { memo } from '../util/index.js';
13
+ import { extractContributed } from './util.js';
14
+
15
+ function _getValues (api: DeriveApi, childKey: string, keys: string[]): Observable<DeriveOwnContributions> {
16
+ // We actually would love to use multi-keys https://github.com/paritytech/substrate/issues/9203
17
+ return combineLatest(keys.map((k) => api.rpc.childstate.getStorage(childKey, k))).pipe(
18
+ map((values) =>
19
+ values
20
+ .map((v) => api.registry.createType('Option<StorageData>', v))
21
+ .map((o) =>
22
+ o.isSome
23
+ ? api.registry.createType('Balance', o.unwrap())
24
+ : api.registry.createType('Balance')
25
+ )
26
+ .reduce((all: DeriveOwnContributions, b, index): DeriveOwnContributions =>
27
+ objectSpread(all, { [keys[index]]: b }), {})
28
+ )
29
+ );
30
+ }
31
+
32
+ function _watchOwnChanges (api: DeriveApi, paraId: string | number | BN, childkey: string, keys: string[]): Observable<DeriveOwnContributions> {
33
+ return api.query.system.events().pipe(
34
+ switchMap((events): Observable<DeriveOwnContributions> => {
35
+ const changes = extractContributed(paraId, events);
36
+ const filtered = keys.filter((k) =>
37
+ changes.added.includes(k) ||
38
+ changes.removed.includes(k)
39
+ );
40
+
41
+ return filtered.length
42
+ ? _getValues(api, childkey, filtered)
43
+ : EMPTY;
44
+ }),
45
+ startWith({})
46
+ );
47
+ }
48
+
49
+ function _contributions (api: DeriveApi, paraId: string | number | BN, childKey: string, keys: string[]): Observable<DeriveOwnContributions> {
50
+ return combineLatest([
51
+ _getValues(api, childKey, keys),
52
+ _watchOwnChanges(api, paraId, childKey, keys)
53
+ ]).pipe(
54
+ map(([all, latest]) =>
55
+ objectSpread({}, all, latest)
56
+ )
57
+ );
58
+ }
59
+
60
+ /**
61
+ * @name ownContributions
62
+ * @description Retrieves the contribution amounts made by specific accounts (`keys`) to a given parachain crowdloan (`paraId`).
63
+ * @param {string | number | BN} paraId The parachain ID for which contributions are being queried.
64
+ * @param {string[]} keys An array of account addresses whose contributions are to be fetched.
65
+ * @example
66
+ * ```javascript
67
+ * const contributions = await api.derive.crowdloan.ownContributions(2000, ['5Ff...PqV', '5Gg...XyZ']);
68
+ * console.log("Own Contributions:", contributions);
69
+ * ```
70
+ */
71
+ export function ownContributions (instanceId: string, api: DeriveApi): (paraId: string | number | BN, keys: string[]) => Observable<DeriveOwnContributions> {
72
+ return memo(instanceId, (paraId: string | number | BN, keys: string[]): Observable<DeriveOwnContributions> =>
73
+ api.derive.crowdloan.childKey(paraId).pipe(
74
+ switchMap((childKey) =>
75
+ childKey && keys.length
76
+ ? _contributions(api, paraId, childKey, keys)
77
+ : of({})
78
+ )
79
+ )
80
+ );
81
+ }
@@ -0,0 +1,11 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Balance } from '@pezkuwi/types/interfaces';
5
+
6
+ export interface DeriveContributions {
7
+ blockHash: string;
8
+ contributorsHex: string[];
9
+ }
10
+
11
+ export type DeriveOwnContributions = Record<string, Balance>;
@@ -0,0 +1,33 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { FrameSystemEventRecord } from '@pezkuwi/types/lookup';
5
+ import type { Vec } from '@pezkuwi/types-codec';
6
+ import type { BN } from '@pezkuwi/util';
7
+
8
+ interface Changes {
9
+ added: string[];
10
+ blockHash: string;
11
+ removed: string[];
12
+ }
13
+
14
+ export function extractContributed (paraId: string | number | BN, events: Vec<FrameSystemEventRecord>): Changes {
15
+ const added: string[] = [];
16
+ const removed: string[] = [];
17
+
18
+ return events
19
+ .filter(({ event: { data: [, eventParaId], method, section } }) =>
20
+ section === 'crowdloan' &&
21
+ ['Contributed', 'Withdrew'].includes(method) &&
22
+ eventParaId.eq(paraId)
23
+ )
24
+ .reduce((result: Changes, { event: { data: [accountId], method } }): Changes => {
25
+ if (method === 'Contributed') {
26
+ result.added.push(accountId.toHex());
27
+ } else {
28
+ result.removed.push(accountId.toHex());
29
+ }
30
+
31
+ return result;
32
+ }, { added, blockHash: events.createdAtHash?.toHex() || '-', removed });
33
+ }
@@ -0,0 +1,158 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable } from 'rxjs';
5
+ import type { Bytes, Option, u8, u32, Vec } from '@pezkuwi/types';
6
+ import type { BlockNumber, Call, Hash, ReferendumIndex, Scheduled } from '@pezkuwi/types/interfaces';
7
+ import type { FrameSupportPreimagesBounded, PalletSchedulerScheduled } from '@pezkuwi/types/lookup';
8
+ import type { Codec, ITuple } from '@pezkuwi/types/types';
9
+ import type { HexString } from '@pezkuwi/util/types';
10
+ import type { DeriveApi, DeriveDispatch, DeriveProposalImage } from '../types.js';
11
+
12
+ import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
13
+
14
+ import { Enum } from '@pezkuwi/types';
15
+ import { isFunction, objectSpread, stringToHex } from '@pezkuwi/util';
16
+
17
+ import { memo } from '../util/index.js';
18
+ import { getImageHashBounded } from './util.js';
19
+
20
+ const DEMOCRACY_ID = stringToHex('democrac');
21
+
22
+ // included here for backwards compat
23
+ interface PalletSchedulerScheduledV3 extends Codec {
24
+ maybeId: Option<Bytes>;
25
+ priority: u8;
26
+ call: FrameSupportScheduleMaybeHashed;
27
+ maybePeriodic: Option<ITuple<[u32, u32]>>;
28
+ origin: Codec;
29
+ }
30
+
31
+ // included here for backwards compat
32
+ interface FrameSupportScheduleMaybeHashed extends Codec {
33
+ isHash: boolean;
34
+ isValue: boolean;
35
+ asValue: Call;
36
+ asHash: Hash;
37
+ }
38
+
39
+ interface SchedulerInfo {
40
+ at: BlockNumber;
41
+ imageHash: HexString;
42
+ index: ReferendumIndex;
43
+ }
44
+
45
+ function isMaybeHashedOrBounded (call: FrameSupportPreimagesBounded | FrameSupportScheduleMaybeHashed | Call): call is FrameSupportScheduleMaybeHashed | FrameSupportPreimagesBounded {
46
+ // check for enum
47
+ return call instanceof Enum;
48
+ }
49
+
50
+ function isBounded (call: FrameSupportPreimagesBounded | FrameSupportScheduleMaybeHashed): call is FrameSupportPreimagesBounded {
51
+ // check for type
52
+ return (call as FrameSupportPreimagesBounded).isInline || (call as FrameSupportPreimagesBounded).isLegacy || (call as FrameSupportPreimagesBounded).isLookup;
53
+ }
54
+
55
+ function queryQueue (api: DeriveApi): Observable<DeriveDispatch[]> {
56
+ return api.query.democracy['dispatchQueue']<Vec<ITuple<[BlockNumber, Hash, ReferendumIndex]>>>().pipe(
57
+ switchMap((dispatches) =>
58
+ combineLatest([
59
+ of(dispatches),
60
+ api.derive.democracy.preimages(
61
+ dispatches.map(([, hash]) => hash))
62
+ ])
63
+ ),
64
+ map(([dispatches, images]) =>
65
+ dispatches.map(([at, imageHash, index], dispatchIndex): DeriveDispatch => ({
66
+ at,
67
+ image: images[dispatchIndex],
68
+ imageHash: getImageHashBounded(imageHash),
69
+ index
70
+ }))
71
+ )
72
+ );
73
+ }
74
+
75
+ function schedulerEntries (api: DeriveApi): Observable<[BlockNumber[], Option<PalletSchedulerScheduled | PalletSchedulerScheduledV3 | Scheduled>[][]]> {
76
+ // We don't get entries, but rather we get the keys (triggered via finished referendums) and
77
+ // the subscribe to those keys - this means we pickup when the schedulers actually executes
78
+ // at a block, the entry for that block will become empty
79
+ return api.derive.democracy.referendumsFinished().pipe(
80
+ switchMap(() =>
81
+ api.query.scheduler.agenda.keys()
82
+ ),
83
+ switchMap((keys) => {
84
+ const blockNumbers = keys.map(({ args: [blockNumber] }) => blockNumber);
85
+
86
+ return blockNumbers.length
87
+ ? combineLatest([
88
+ of(blockNumbers),
89
+ // this should simply be api.query.scheduler.agenda.multi,
90
+ // however we have had cases on Darwinia where the indices have moved around after an
91
+ // upgrade, which results in invalid on-chain data
92
+ api.query.scheduler.agenda.multi(blockNumbers).pipe(
93
+ catchError(() => of(blockNumbers.map(() => [])))
94
+ )
95
+ ])
96
+ : of<[BlockNumber[], Option<PalletSchedulerScheduledV3 | Scheduled>[][]]>([[], []]);
97
+ })
98
+ );
99
+ }
100
+
101
+ function queryScheduler (api: DeriveApi): Observable<DeriveDispatch[]> {
102
+ return schedulerEntries(api).pipe(
103
+ switchMap(([blockNumbers, agendas]): Observable<[SchedulerInfo[], (DeriveProposalImage | undefined)[]]> => {
104
+ const result: SchedulerInfo[] = [];
105
+
106
+ blockNumbers.forEach((at, index): void => {
107
+ (agendas[index] || []).filter((o) => o.isSome).forEach((o): void => {
108
+ const scheduled = o.unwrap();
109
+
110
+ if (scheduled.maybeId.isSome) {
111
+ const id = scheduled.maybeId.unwrap().toHex();
112
+
113
+ if (id.startsWith(DEMOCRACY_ID)) {
114
+ const imageHash = isMaybeHashedOrBounded(scheduled.call)
115
+ ? isBounded(scheduled.call)
116
+ ? getImageHashBounded(scheduled.call)
117
+ : scheduled.call.isHash
118
+ ? scheduled.call.asHash.toHex()
119
+ : scheduled.call.asValue.args[0].toHex()
120
+ : scheduled.call.args[0].toHex();
121
+
122
+ result.push({ at, imageHash, index: api.registry.createType('(u64, ReferendumIndex)', id)[1] });
123
+ }
124
+ }
125
+ });
126
+ });
127
+
128
+ return combineLatest([
129
+ of(result),
130
+ result.length
131
+ ? api.derive.democracy.preimages(result.map(({ imageHash }) => imageHash))
132
+ : of([])
133
+ ]);
134
+ }),
135
+ map(([infos, images]): DeriveDispatch[] =>
136
+ infos.map((info, index) => objectSpread({ image: images[index] }, info))
137
+ )
138
+ );
139
+ }
140
+
141
+ /**
142
+ * @name dispatchQueue
143
+ * @description Retrieves the list of scheduled or pending dispatches in the governance system.
144
+ * @example
145
+ * ```javascript
146
+ * const queue = await api.derive.democracy.dispatchQueue();
147
+ * console.log("Dispatch Queue:", queue);
148
+ * ```
149
+ */
150
+ export function dispatchQueue (instanceId: string, api: DeriveApi): () => Observable<DeriveDispatch[]> {
151
+ return memo(instanceId, (): Observable<DeriveDispatch[]> =>
152
+ isFunction(api.query.scheduler?.agenda)
153
+ ? queryScheduler(api)
154
+ : api.query.democracy['dispatchQueue']
155
+ ? queryQueue(api)
156
+ : of([])
157
+ );
158
+ }
@@ -0,0 +1,14 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './dispatchQueue.js';
5
+ export * from './locks.js';
6
+ export * from './nextExternal.js';
7
+ export * from './preimages.js';
8
+ export * from './proposals.js';
9
+ export * from './referendumIds.js';
10
+ export * from './referendums.js';
11
+ export * from './referendumsActive.js';
12
+ export * from './referendumsFinished.js';
13
+ export * from './referendumsInfo.js';
14
+ export * from './sqrtElectorate.js';