@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,114 @@
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, ReferendumInfoTo239, Vote } from '@pezkuwi/types/interfaces';
6
+ import type { PalletDemocracyReferendumInfo, PalletDemocracyVoteVoting } from '@pezkuwi/types/lookup';
7
+ import type { BN } from '@pezkuwi/util';
8
+ import type { DeriveApi, DeriveDemocracyLock } from '../types.js';
9
+
10
+ import { map, of, switchMap } from 'rxjs';
11
+
12
+ import { BN_ZERO, isUndefined } from '@pezkuwi/util';
13
+
14
+ import { memo } from '../util/index.js';
15
+
16
+ type ReferendumInfoFinished = PalletDemocracyReferendumInfo['asFinished'];
17
+ type VotingDelegating = PalletDemocracyVoteVoting['asDelegating'];
18
+ type VotingDirect = PalletDemocracyVoteVoting['asDirect'];
19
+ type VotingDirectVote = VotingDirect['votes'][0];
20
+
21
+ const LOCKUPS = [0, 1, 2, 4, 8, 16, 32];
22
+
23
+ function parseEnd (api: DeriveApi, vote: Vote, { approved, end }: ReferendumInfoFinished): [BN, BN] {
24
+ return [
25
+ end,
26
+ (approved.isTrue && vote.isAye) || (approved.isFalse && vote.isNay)
27
+ ? end.add(
28
+ (
29
+ api.consts.democracy.voteLockingPeriod ||
30
+ api.consts.democracy.enactmentPeriod
31
+ ).muln(LOCKUPS[vote.conviction.index])
32
+ )
33
+ : BN_ZERO
34
+ ];
35
+ }
36
+
37
+ function parseLock (api: DeriveApi, [referendumId, accountVote]: VotingDirectVote, referendum: PalletDemocracyReferendumInfo): DeriveDemocracyLock {
38
+ const { balance, vote } = accountVote.asStandard;
39
+ const [referendumEnd, unlockAt] = referendum.isFinished
40
+ ? parseEnd(api, vote, referendum.asFinished)
41
+ : [BN_ZERO, BN_ZERO];
42
+
43
+ return { balance, isDelegated: false, isFinished: referendum.isFinished, referendumEnd, referendumId, unlockAt, vote };
44
+ }
45
+
46
+ function delegateLocks (api: DeriveApi, { balance, conviction, target }: VotingDelegating): Observable<DeriveDemocracyLock[]> {
47
+ return api.derive.democracy.locks(target).pipe(
48
+ map((available): DeriveDemocracyLock[] =>
49
+ available.map(({ isFinished, referendumEnd, referendumId, unlockAt, vote }): DeriveDemocracyLock => ({
50
+ balance,
51
+ isDelegated: true,
52
+ isFinished,
53
+ referendumEnd,
54
+ referendumId,
55
+ unlockAt: unlockAt.isZero()
56
+ ? unlockAt
57
+ : referendumEnd.add(
58
+ (
59
+ api.consts.democracy.voteLockingPeriod ||
60
+ api.consts.democracy.enactmentPeriod
61
+ ).muln(LOCKUPS[conviction.index])
62
+ ),
63
+ vote: api.registry.createType('Vote', { aye: vote.isAye, conviction })
64
+ }))
65
+ )
66
+ );
67
+ }
68
+
69
+ function directLocks (api: DeriveApi, { votes }: VotingDirect): Observable<DeriveDemocracyLock[]> {
70
+ if (!votes.length) {
71
+ return of([]);
72
+ }
73
+
74
+ return api.query.democracy.referendumInfoOf.multi(votes.map(([referendumId]) => referendumId)).pipe(
75
+ map((referendums) =>
76
+ votes
77
+ .map((vote, index): [VotingDirectVote, PalletDemocracyReferendumInfo | ReferendumInfoTo239 | null] =>
78
+ [vote, referendums[index].unwrapOr(null)]
79
+ )
80
+ .filter((item): item is [VotingDirectVote, PalletDemocracyReferendumInfo] =>
81
+ !!item[1] && isUndefined((item[1] as ReferendumInfoTo239).end) && item[0][1].isStandard
82
+ )
83
+ .map(([directVote, referendum]) =>
84
+ parseLock(api, directVote, referendum)
85
+ )
86
+ )
87
+ );
88
+ }
89
+
90
+ /**
91
+ * @name locks
92
+ * @description Retrieves the democracy voting locks for a given account.
93
+ * @param { string | AccountId } accountId The accountId for which to retrieve democracy voting locks.
94
+ * @example
95
+ * ```javascript
96
+ * const locks = await api.derive.democracy.locks('5FfFjX...'); // Replace with an actual accountId
97
+ * console.log("Democracy Locks:", locks);
98
+ * ```
99
+ */
100
+ export function locks (instanceId: string, api: DeriveApi): (accountId: string | AccountId) => Observable<DeriveDemocracyLock[]> {
101
+ return memo(instanceId, (accountId: string | AccountId): Observable<DeriveDemocracyLock[]> =>
102
+ api.query.democracy.votingOf
103
+ ? api.query.democracy.votingOf(accountId).pipe(
104
+ switchMap((voting): Observable<DeriveDemocracyLock[]> =>
105
+ voting.isDirect
106
+ ? directLocks(api, voting.asDirect)
107
+ : voting.isDelegating
108
+ ? delegateLocks(api, voting.asDelegating)
109
+ : of([])
110
+ )
111
+ )
112
+ : of([])
113
+ );
114
+ }
@@ -0,0 +1,49 @@
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 } from '@pezkuwi/types';
6
+ import type { H256 } from '@pezkuwi/types/interfaces';
7
+ import type { FrameSupportPreimagesBounded, PalletDemocracyVoteThreshold } from '@pezkuwi/types/lookup';
8
+ import type { ITuple } from '@pezkuwi/types/types';
9
+ import type { DeriveApi, DeriveProposalExternal } from '../types.js';
10
+
11
+ import { map, of, switchMap } from 'rxjs';
12
+
13
+ import { memo } from '../util/index.js';
14
+ import { getImageHashBounded } from './util.js';
15
+
16
+ function withImage (api: DeriveApi, nextOpt: Option<ITuple<[H256 | FrameSupportPreimagesBounded, PalletDemocracyVoteThreshold]>>): Observable<DeriveProposalExternal | null> {
17
+ if (nextOpt.isNone) {
18
+ return of(null);
19
+ }
20
+
21
+ const [hash, threshold] = nextOpt.unwrap();
22
+
23
+ return api.derive.democracy.preimage(hash).pipe(
24
+ map((image): DeriveProposalExternal => ({
25
+ image,
26
+ imageHash: getImageHashBounded(hash),
27
+ threshold
28
+ }))
29
+ );
30
+ }
31
+
32
+ /**
33
+ * @name nextExternal
34
+ * @description Retrieves the next external proposal that is scheduled for a referendum.
35
+ * @example
36
+ * ```javascript
37
+ * const nextExternal = await api.derive.democracy.nextExternal();
38
+ * console.log("Next external proposal:", nextExternal);
39
+ * ```
40
+ */
41
+ export function nextExternal (instanceId: string, api: DeriveApi): () => Observable<DeriveProposalExternal | null> {
42
+ return memo(instanceId, (): Observable<DeriveProposalExternal | null> =>
43
+ api.query.democracy?.nextExternal
44
+ ? api.query.democracy.nextExternal().pipe(
45
+ switchMap((nextOpt) => withImage(api, nextOpt))
46
+ )
47
+ : of(null)
48
+ );
49
+ }
@@ -0,0 +1,171 @@
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 { u128 } from '@pezkuwi/types';
6
+ import type { AccountId, AccountId32, Balance, BlockNumber, Call, Hash, PreimageStatus } from '@pezkuwi/types/interfaces';
7
+ import type { FrameSupportPreimagesBounded, PalletPreimageOldRequestStatus, PalletPreimageRequestStatus } from '@pezkuwi/types/lookup';
8
+ import type { Bytes, Option } from '@pezkuwi/types-codec';
9
+ import type { ITuple } from '@pezkuwi/types-codec/types';
10
+ import type { HexString } from '@pezkuwi/util/types';
11
+ import type { DeriveApi, DeriveProposalImage } from '../types.js';
12
+
13
+ import { map, of, switchMap } from 'rxjs';
14
+
15
+ import { BN_ZERO, isFunction } from '@pezkuwi/util';
16
+
17
+ import { firstMemo, memo } from '../util/index.js';
18
+ import { getImageHashBounded } from './util.js';
19
+
20
+ type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber];
21
+ type OldPreimage = ITuple<PreimageInfo>;
22
+ type CompatStatusU = PalletPreimageRequestStatus['asUnrequested'] & { deposit: ITuple<[AccountId32, u128]> };
23
+ type CompatStatusR = PalletPreimageRequestStatus['asRequested'] & { deposit: Option<ITuple<[AccountId32, u128]>> };
24
+
25
+ function getUnrequestedTicket (status: PalletPreimageRequestStatus['asUnrequested']): [AccountId32, u128] {
26
+ return status.ticket || (status as CompatStatusU).deposit;
27
+ }
28
+
29
+ function getRequestedTicket (status: PalletPreimageRequestStatus['asRequested']): [AccountId32, u128] {
30
+ return (status.maybeTicket || (status as CompatStatusR).deposit).unwrapOrDefault();
31
+ }
32
+
33
+ function isDemocracyPreimage (api: DeriveApi, imageOpt: Option<OldPreimage> | Option<PreimageStatus>): imageOpt is Option<PreimageStatus> {
34
+ return !!imageOpt && !api.query.democracy['dispatchQueue'];
35
+ }
36
+
37
+ function constructProposal (api: DeriveApi, [bytes, proposer, balance, at]: PreimageInfo): DeriveProposalImage {
38
+ let proposal: Call | undefined;
39
+
40
+ try {
41
+ proposal = api.registry.createType('Call', bytes.toU8a(true));
42
+ } catch (error) {
43
+ console.error(error);
44
+ }
45
+
46
+ return { at, balance, proposal, proposer };
47
+ }
48
+
49
+ function parseDemocracy (api: DeriveApi, imageOpt: Option<OldPreimage> | Option<PreimageStatus>): DeriveProposalImage | undefined {
50
+ if (imageOpt.isNone) {
51
+ return;
52
+ }
53
+
54
+ if (isDemocracyPreimage(api, imageOpt)) {
55
+ const status = imageOpt.unwrap();
56
+
57
+ if (status.isMissing) {
58
+ return;
59
+ }
60
+
61
+ const { data, deposit, provider, since } = status.asAvailable;
62
+
63
+ return constructProposal(api, [data, provider, deposit, since]);
64
+ }
65
+
66
+ return constructProposal(api, imageOpt.unwrap());
67
+ }
68
+
69
+ function parseImage (api: DeriveApi, [proposalHash, status, bytes]: [HexString, PalletPreimageRequestStatus | PalletPreimageOldRequestStatus | null, Bytes | null]): DeriveProposalImage | undefined {
70
+ if (!status) {
71
+ return undefined;
72
+ }
73
+
74
+ const [proposer, balance] = status.isUnrequested
75
+ ? getUnrequestedTicket((status as PalletPreimageRequestStatus).asUnrequested)
76
+ : getRequestedTicket((status as PalletPreimageRequestStatus).asRequested);
77
+ let proposal: Call | undefined;
78
+
79
+ if (bytes) {
80
+ try {
81
+ proposal = api.registry.createType('Call', bytes.toU8a(true));
82
+ } catch (error) {
83
+ console.error(error);
84
+ }
85
+ }
86
+
87
+ return { at: BN_ZERO, balance, proposal, proposalHash, proposer };
88
+ }
89
+
90
+ function getDemocracyImages (api: DeriveApi, bounded: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]): Observable<(DeriveProposalImage | undefined)[]> {
91
+ const hashes = bounded.map((b) => getImageHashBounded(b));
92
+
93
+ return api.query.democracy['preimages'].multi<Option<PreimageStatus>>(hashes).pipe(
94
+ map((images): (DeriveProposalImage | undefined)[] =>
95
+ images.map((imageOpt) => parseDemocracy(api, imageOpt))
96
+ )
97
+ );
98
+ }
99
+
100
+ function getImages (api: DeriveApi, bounded: (FrameSupportPreimagesBounded | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> {
101
+ const hashes = bounded.map((b) => getImageHashBounded(b));
102
+ const bytesType = api.registry.lookup.getTypeDef(api.query.preimage.preimageFor.creator.meta.type.asMap.key).type;
103
+
104
+ return api.query.preimage.statusFor.multi(hashes).pipe(
105
+ switchMap((optStatus) => {
106
+ const statuses = optStatus.map((o) => o.unwrapOr(null));
107
+ const keys = statuses
108
+ .map((s, i) =>
109
+ s
110
+ ? bytesType === 'H256'
111
+ // first generation
112
+ ? hashes[i]
113
+ // current generation (H256,u32)
114
+ : s.isRequested
115
+ ? [hashes[i], s.asRequested.len.unwrapOr(0)]
116
+ : [hashes[i], s.asUnrequested.len]
117
+ : null
118
+ )
119
+ .filter((p) => !!p);
120
+
121
+ return api.query.preimage.preimageFor.multi(keys).pipe(
122
+ map((optBytes) => {
123
+ let ptr = -1;
124
+
125
+ return statuses
126
+ .map((s, i): [HexString, PalletPreimageRequestStatus | PalletPreimageOldRequestStatus | null, Bytes | null] =>
127
+ s
128
+ ? [hashes[i], s, optBytes[++ptr].unwrapOr(null)]
129
+ : [hashes[i], null, null]
130
+ )
131
+ .map((v) => parseImage(api, v));
132
+ })
133
+ );
134
+ })
135
+ );
136
+ }
137
+
138
+ /**
139
+ * @name preimages
140
+ * @description Retrieves the full details (preimages) of governance proposals using their on-chain hashes.
141
+ * @param { (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[] } hashes An array of hashes representing governance proposals.
142
+ * @example
143
+ * ```javascript
144
+ * const preimages = await api.derive.democracy.preimages([HASH1, HASH2]);
145
+ * ```
146
+ */
147
+ export function preimages (instanceId: string, api: DeriveApi): (hashes: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]) => Observable<(DeriveProposalImage | undefined)[]> {
148
+ return memo(instanceId, (hashes: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]): Observable<(DeriveProposalImage | undefined)[]> =>
149
+ hashes.length
150
+ ? isFunction(api.query.democracy['preimages'])
151
+ ? getDemocracyImages(api, hashes)
152
+ : isFunction(api.query.preimage.preimageFor)
153
+ ? getImages(api, hashes)
154
+ : of([])
155
+ : of([])
156
+ );
157
+ }
158
+
159
+ /**
160
+ * @name preimage
161
+ * @description Retrieves the full details (preimage) of a governance proposal using its on-chain hash.
162
+ * @param { Hash | Uint8Array | string | FrameSupportPreimagesBounded } hash Hash that represents governance proposals.
163
+ * * @example
164
+ * ```javascript
165
+ * const preimage = await api.derive.democracy.preimage(HASH);
166
+ * ```
167
+ */
168
+ export const preimage = /*#__PURE__*/ firstMemo(
169
+ (api: DeriveApi, hash: Hash | Uint8Array | string | FrameSupportPreimagesBounded) =>
170
+ api.derive.democracy.preimages([hash])
171
+ );
@@ -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 { Option, Vec } from '@pezkuwi/types';
6
+ import type { AccountId, Balance, Hash, PropIndex } from '@pezkuwi/types/interfaces';
7
+ import type { FrameSupportPreimagesBounded } from '@pezkuwi/types/lookup';
8
+ import type { ITuple } from '@pezkuwi/types/types';
9
+ import type { DeriveApi, DeriveProposal, DeriveProposalImage } from '../types.js';
10
+
11
+ import { combineLatest, map, of, switchMap } from 'rxjs';
12
+
13
+ import { isFunction, objectSpread } from '@pezkuwi/util';
14
+
15
+ import { memo } from '../util/index.js';
16
+ import { getImageHashBounded } from './util.js';
17
+
18
+ type DepositorsNew = Option<ITuple<[Vec<AccountId>, Balance]>>;
19
+ type DepositorsOld = Option<ITuple<[Balance, Vec<AccountId>]>>;
20
+ type Depositors = DepositorsNew | DepositorsOld;
21
+ type Proposals = ITuple<[PropIndex, Hash | FrameSupportPreimagesBounded, AccountId]>[];
22
+ type Result = [Proposals, (DeriveProposalImage | undefined)[], Depositors[]];
23
+
24
+ function isNewDepositors (depositors: ITuple<[Vec<AccountId>, Balance]> | ITuple<[Balance, Vec<AccountId>]>): depositors is ITuple<[Vec<AccountId>, Balance]> {
25
+ // Detect balance...
26
+ return isFunction((depositors[1] as Balance).mul);
27
+ }
28
+
29
+ function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] {
30
+ return proposals
31
+ .filter(([, , proposer], index): boolean =>
32
+ !!(optDepositors[index]?.isSome) && !proposer.isEmpty
33
+ )
34
+ .map(([index, hash, proposer], proposalIndex): DeriveProposal => {
35
+ const depositors = optDepositors[proposalIndex].unwrap();
36
+
37
+ return objectSpread(
38
+ {
39
+ image: images[proposalIndex],
40
+ imageHash: getImageHashBounded(hash),
41
+ index,
42
+ proposer
43
+ },
44
+ isNewDepositors(depositors)
45
+ ? { balance: depositors[1], seconds: depositors[0] }
46
+ : { balance: depositors[0], seconds: depositors[1] }
47
+ );
48
+ });
49
+ }
50
+
51
+ /**
52
+ * @name proposals
53
+ * @description Retrieves the list of active public proposals in the democracy module, along with their associated preimage data and deposit information.
54
+ * @example
55
+ * ```javascript
56
+ * const proposals = await api.derive.democracy.proposals();
57
+ * console.log("proposals:", proposals);
58
+ * ```
59
+ */
60
+ export function proposals (instanceId: string, api: DeriveApi): () => Observable<DeriveProposal[]> {
61
+ return memo(instanceId, (): Observable<DeriveProposal[]> =>
62
+ isFunction(api.query.democracy?.publicProps)
63
+ ? api.query.democracy.publicProps().pipe(
64
+ switchMap((proposals) =>
65
+ proposals.length
66
+ ? combineLatest([
67
+ of(proposals),
68
+ api.derive.democracy.preimages(
69
+ proposals.map(([, hash]) => hash)
70
+ ),
71
+ api.query.democracy.depositOf.multi(
72
+ proposals.map(([index]) => index)
73
+ )
74
+ ])
75
+ : of<Result>([[], [], []])
76
+ ),
77
+ map(parse)
78
+ )
79
+ : of([])
80
+ );
81
+ }
@@ -0,0 +1,37 @@
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 { ReferendumIndex } from '@pezkuwi/types/interfaces';
6
+ import type { BN } from '@pezkuwi/util';
7
+ import type { DeriveApi } from '../types.js';
8
+
9
+ import { map, of } from 'rxjs';
10
+
11
+ import { memo } from '../util/index.js';
12
+
13
+ /**
14
+ * @name referendumIds
15
+ * @description Retrieves an array of active referendum IDs.
16
+ * @example
17
+ * ```javascript
18
+ * const referendums = await api.derive.democracy.referendumIds();
19
+ * ```
20
+ */
21
+ export function referendumIds (instanceId: string, api: DeriveApi): () => Observable<BN[]> {
22
+ return memo(instanceId, (): Observable<BN[]> =>
23
+ api.query.democracy?.lowestUnbaked
24
+ ? api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
25
+ api.query.democracy.lowestUnbaked,
26
+ api.query.democracy.referendumCount
27
+ ]).pipe(
28
+ map(([first, total]): BN[] =>
29
+ total.gt(first)
30
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
31
+ ? [...Array(total.sub(first).toNumber())].map((_, i): BN => first.addn(i))
32
+ : []
33
+ )
34
+ )
35
+ : of([])
36
+ );
37
+ }
@@ -0,0 +1,39 @@
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 { DeriveApi, DeriveReferendumExt } from '../types.js';
6
+
7
+ import { combineLatest, map, of, switchMap } from 'rxjs';
8
+
9
+ import { objectSpread } from '@pezkuwi/util';
10
+
11
+ import { memo } from '../util/index.js';
12
+
13
+ /**
14
+ * @name referendums
15
+ * @description Retrieves information about all active referendums, including their details and associated votes.
16
+ * @example
17
+ * ```javascript
18
+ * const referendums = await api.derive.democracy.referendums();
19
+ * ```
20
+ */
21
+ export function referendums (instanceId: string, api: DeriveApi): () => Observable<DeriveReferendumExt[]> {
22
+ return memo(instanceId, (): Observable<DeriveReferendumExt[]> =>
23
+ api.derive.democracy.referendumsActive().pipe(
24
+ switchMap((referendums) =>
25
+ referendums.length
26
+ ? combineLatest([
27
+ of(referendums),
28
+ api.derive.democracy._referendumsVotes(referendums)
29
+ ])
30
+ : of([[], []])
31
+ ),
32
+ map(([referendums, votes]) =>
33
+ referendums.map((referendum, index): DeriveReferendumExt =>
34
+ objectSpread({}, referendum, votes[index])
35
+ )
36
+ )
37
+ )
38
+ );
39
+ }
@@ -0,0 +1,30 @@
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 { DeriveApi, DeriveReferendum } from '../types.js';
6
+
7
+ import { of, switchMap } from 'rxjs';
8
+
9
+ import { memo } from '../util/index.js';
10
+
11
+ /**
12
+ * @name referendumsActive
13
+ * @description Retrieves information about active referendums.
14
+ * @example
15
+ * ```javascript
16
+ * const referendums = await api.derive.democracy.referendumsActive();
17
+ * console.log("Active Referendums:", referendums);
18
+ * ```
19
+ */
20
+ export function referendumsActive (instanceId: string, api: DeriveApi): () => Observable<DeriveReferendum[]> {
21
+ return memo(instanceId, (): Observable<DeriveReferendum[]> =>
22
+ api.derive.democracy.referendumIds().pipe(
23
+ switchMap((ids): Observable<DeriveReferendum[]> =>
24
+ ids.length
25
+ ? api.derive.democracy.referendumsInfo(ids)
26
+ : of([])
27
+ )
28
+ )
29
+ );
30
+ }
@@ -0,0 +1,37 @@
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 { PalletDemocracyReferendumInfo } from '@pezkuwi/types/lookup';
6
+ import type { DeriveApi } from '../types.js';
7
+
8
+ import { map, switchMap } from 'rxjs';
9
+
10
+ import { memo } from '../util/index.js';
11
+
12
+ type ReferendumInfoFinished = PalletDemocracyReferendumInfo['asFinished'];
13
+
14
+ /**
15
+ * @name referendumsFinished
16
+ * @description Retrieves information about finished referendums.
17
+ * @example
18
+ * ```javascript
19
+ * const referendums = await api.derive.democracy.referendumsFinished();
20
+ * console.log("Finished Referendums:", referendums);
21
+ * ```
22
+ */
23
+ export function referendumsFinished (instanceId: string, api: DeriveApi): () => Observable<ReferendumInfoFinished[]> {
24
+ return memo(instanceId, (): Observable<ReferendumInfoFinished[]> =>
25
+ api.derive.democracy.referendumIds().pipe(
26
+ switchMap((ids) =>
27
+ api.query.democracy.referendumInfoOf.multi(ids)
28
+ ),
29
+ map((infos): ReferendumInfoFinished[] =>
30
+ infos
31
+ .map((o) => o.unwrapOr(null))
32
+ .filter((info): info is PalletDemocracyReferendumInfo => !!info && info.isFinished)
33
+ .map((info) => info.asFinished)
34
+ )
35
+ )
36
+ );
37
+ }