@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,162 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import type { ApiPromise } from '@pezkuwi/api';
7
+ import type { SubmittableExtrinsic } from '@pezkuwi/api-base/types';
8
+ import type { Bytes, Option, StorageKey } from '@pezkuwi/types';
9
+ import type { Bounty, BountyIndex, Proposal, ProposalIndex } from '@pezkuwi/types/interfaces';
10
+ import type { Codec, InterfaceTypes } from '@pezkuwi/types/types';
11
+ import type { DeriveApi, DeriveCollectiveProposal } from '../types.js';
12
+
13
+ import { firstValueFrom, of } from 'rxjs';
14
+
15
+ import { BountyFactory } from '../test/bountyFactory.js';
16
+ import { BytesFactory } from '../test/bytesFactory.js';
17
+ import { createApiWithAugmentations } from '../test/helpers.js';
18
+ import { ProposalFactory } from '../test/proposalFactory.js';
19
+ import { bounties } from './index.js';
20
+
21
+ const DEFAULT_PROPOSER = '5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM';
22
+
23
+ describe('bounties derive', () => {
24
+ let storageKey: (index: number) => StorageKey;
25
+ let defaultBounty: () => Bounty;
26
+ let emptyOption: <T extends Codec> (typeName: keyof InterfaceTypes) => Option<T>;
27
+ let optionOf: <T extends Codec> (value: T) => Option<T>;
28
+ let bountyIndex: (index: number) => BountyIndex;
29
+ let proposalIndex: (index: number) => ProposalIndex;
30
+ let bytes: (value: string) => Bytes;
31
+ let api: ApiPromise;
32
+ let createProposal: (method: SubmittableExtrinsic<'promise'>) => Proposal;
33
+ let defaultMockApi: DeriveApi;
34
+
35
+ beforeAll(() => {
36
+ api = createApiWithAugmentations();
37
+
38
+ ({ bountyIndex, defaultBounty, emptyOption, optionOf, storageKey } = new BountyFactory(api));
39
+ ({ bytes } = new BytesFactory(api.registry));
40
+ ({ createProposal, proposalIndex } = new ProposalFactory(api));
41
+ });
42
+
43
+ beforeEach(() => {
44
+ defaultMockApi = {
45
+ derive: {
46
+ council: {
47
+ proposals: () => of([] as DeriveCollectiveProposal[])
48
+ }
49
+ },
50
+ query: {
51
+ council: {
52
+ proposalCount: () => of(proposalIndex(2))
53
+ },
54
+ treasury: {
55
+ bounties: {
56
+ keys: () => of([storageKey(0), storageKey(1)]),
57
+ multi: () => of([optionOf(defaultBounty()), optionOf(defaultBounty())])
58
+ },
59
+ bountyCount: () => of(bountyIndex(2)),
60
+ bountyDescriptions: {
61
+ multi: () => of([
62
+ optionOf(bytes('make polkadot even better')),
63
+ optionOf(bytes('some other bounty'))
64
+ ])
65
+ }
66
+ }
67
+ },
68
+ tx: api.tx
69
+ } as unknown as DeriveApi;
70
+ });
71
+
72
+ it('creates storage key', function () {
73
+ expect(storageKey(194).args[0].eq(194)).toBe(true);
74
+ });
75
+
76
+ it('creates proposal', function () {
77
+ expect(createProposal(api.tx.balances.transferAllowDeath('5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z', 1))).toBeTruthy();
78
+ });
79
+
80
+ it('combines bounties with descriptions', async () => {
81
+ const mockApi = {
82
+ ...defaultMockApi,
83
+ query: {
84
+ ...defaultMockApi.query,
85
+ treasury: {
86
+ bounties: {
87
+ keys: () => of([storageKey(0), storageKey(2), storageKey(3)]),
88
+ multi: () => of([optionOf(defaultBounty()), emptyOption('Bounty'), optionOf(defaultBounty())])
89
+ },
90
+ bountyCount: () => of(bountyIndex(3)),
91
+ bountyDescriptions: {
92
+ multi: () => of([
93
+ optionOf(bytes('make polkadot even better')),
94
+ optionOf(bytes('this will be totally ignored')),
95
+ emptyOption('Bytes')
96
+ ])
97
+ }
98
+ }
99
+ }
100
+ } as unknown as DeriveApi;
101
+
102
+ const result = await firstValueFrom(bounties('', mockApi)());
103
+
104
+ expect(result).toHaveLength(2);
105
+ expect(result[0].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
106
+ expect(result[0].description).toEqual('make polkadot even better');
107
+ expect(result[0].index.eq(0)).toBe(true);
108
+ expect(result[1].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
109
+ expect(result[1].description).toEqual('');
110
+ expect(result[1].index.eq(3)).toBe(true);
111
+ });
112
+
113
+ it('returns motions', async () => {
114
+ const result = await firstValueFrom(bounties('', defaultMockApi)());
115
+
116
+ expect(result).toHaveLength(2);
117
+ expect(result[0].proposals).toHaveLength(0);
118
+ expect(result[1].proposals).toHaveLength(0);
119
+ });
120
+
121
+ it('when no council, returns bounties without motions', async () => {
122
+ const mockApi = {
123
+ ...defaultMockApi,
124
+ query: {
125
+ ...defaultMockApi.query,
126
+ council: null
127
+ }
128
+ } as unknown as DeriveApi;
129
+
130
+ const result = await firstValueFrom(bounties('', mockApi)());
131
+
132
+ expect(result).toHaveLength(2);
133
+ expect(result[0].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
134
+ expect(result[0].description).toEqual('make polkadot even better');
135
+ expect(result[0].index.eq(0)).toBe(true);
136
+ expect(result[0].proposals).toHaveLength(0);
137
+ expect(result[1].bounty.proposer.toString()).toEqual(DEFAULT_PROPOSER);
138
+ expect(result[1].description).toEqual('some other bounty');
139
+ expect(result[1].index.eq(1)).toBe(true);
140
+ expect(result[1].proposals).toHaveLength(0);
141
+ });
142
+
143
+ it('combines bounties with motions', async () => {
144
+ const mockApi = {
145
+ ...defaultMockApi,
146
+ derive: {
147
+ council: {
148
+ proposals: () => of([
149
+ { proposal: createProposal(api.tx.bounties.approveBounty(1)) },
150
+ { proposal: api.tx.treasury['approveProposal'] && createProposal(api.tx.treasury['approveProposal'](1)) }] as DeriveCollectiveProposal[])
151
+ }
152
+ }
153
+ } as unknown as DeriveApi;
154
+
155
+ const result = await firstValueFrom(bounties('', mockApi)());
156
+
157
+ expect(result).toHaveLength(2);
158
+ expect(result[0].proposals).toHaveLength(0);
159
+ expect(result[1].proposals).toHaveLength(1);
160
+ expect(result[1].proposals[0].proposal?.method).toEqual('approveBounty');
161
+ });
162
+ });
@@ -0,0 +1,76 @@
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 } from '@pezkuwi/types';
6
+ import type { BountyIndex } from '@pezkuwi/types/interfaces';
7
+ import type { PalletBountiesBounty } from '@pezkuwi/types/lookup';
8
+ import type { DeriveApi, DeriveBounties, DeriveCollectiveProposal } from '../types.js';
9
+
10
+ import { combineLatest, map, of, switchMap } from 'rxjs';
11
+
12
+ import { memo } from '../util/index.js';
13
+ import { filterBountiesProposals } from './helpers/filterBountyProposals.js';
14
+
15
+ type Result = [Option<PalletBountiesBounty>[], Option<Bytes>[], BountyIndex[], DeriveCollectiveProposal[]];
16
+
17
+ function parseResult ([maybeBounties, maybeDescriptions, ids, bountyProposals]: Result): DeriveBounties {
18
+ const bounties: DeriveBounties = [];
19
+
20
+ maybeBounties.forEach((bounty, index) => {
21
+ if (bounty.isSome) {
22
+ bounties.push({
23
+ bounty: bounty.unwrap(),
24
+ description: maybeDescriptions[index].unwrapOrDefault().toUtf8(),
25
+ index: ids[index],
26
+ proposals: bountyProposals.filter((bountyProposal) =>
27
+ bountyProposal.proposal && ids[index].eq(bountyProposal.proposal.args[0])
28
+ )
29
+ });
30
+ }
31
+ });
32
+
33
+ return bounties;
34
+ }
35
+
36
+ /**
37
+ * @name bounties
38
+ * @descrive Retrieves all active bounties, their descriptions, and associated proposals.
39
+ * @example
40
+ * ```javascript
41
+ * const bounties = await api.derive.bounties();
42
+ * console.log("Active bounties:", bounties);
43
+ * ```
44
+ */
45
+ export function bounties (instanceId: string, api: DeriveApi): () => Observable<DeriveBounties> {
46
+ const bountyBase = api.query.bounties || api.query.treasury;
47
+
48
+ return memo(instanceId, (): Observable<DeriveBounties> =>
49
+ bountyBase.bounties
50
+ ? combineLatest([
51
+ bountyBase.bountyCount(),
52
+ api.query.council
53
+ ? api.query.council.proposalCount()
54
+ : of(0)
55
+ ]).pipe(
56
+ switchMap(() => combineLatest([
57
+ bountyBase.bounties.keys(),
58
+ api.derive.council
59
+ ? api.derive.council.proposals()
60
+ : of([])
61
+ ])),
62
+ switchMap(([keys, proposals]): Observable<Result> => {
63
+ const ids = keys.map(({ args: [id] }) => id);
64
+
65
+ return combineLatest([
66
+ bountyBase.bounties.multi(ids),
67
+ bountyBase.bountyDescriptions.multi(ids),
68
+ of(ids),
69
+ of(filterBountiesProposals(api, proposals))
70
+ ]);
71
+ }),
72
+ map(parseResult)
73
+ )
74
+ : of(parseResult([[], [], [], []]))
75
+ );
76
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { DeriveApi, DeriveCollectiveProposal } from '../../types.js';
5
+
6
+ export function filterBountiesProposals (api: DeriveApi, allProposals: DeriveCollectiveProposal[]): DeriveCollectiveProposal[] {
7
+ const bountyTxBase = api.tx.bounties ? api.tx.bounties : api.tx.treasury;
8
+ const bountyProposalCalls = [bountyTxBase.approveBounty, bountyTxBase.closeBounty, bountyTxBase.proposeCurator, bountyTxBase.unassignCurator];
9
+
10
+ return allProposals.filter((proposal) => bountyProposalCalls.find((bountyCall) =>
11
+ proposal.proposal && bountyCall.is(proposal.proposal))
12
+ );
13
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './bounties.js';
package/src/bundle.ts ADDED
@@ -0,0 +1,140 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { DeriveCustom } from '@pezkuwi/api-base/types';
5
+ import type { AnyFunction, AnyString } from '@pezkuwi/types/types';
6
+ import type { ExactDerive } from './derive.js';
7
+ import type { DeriveApi } from './types.js';
8
+
9
+ import { lazyDeriveSection } from './util/index.js';
10
+ import { derive } from './derive.js';
11
+
12
+ export * from './derive.js';
13
+ export * from './type/index.js';
14
+
15
+ interface Avail {
16
+ instances: string[];
17
+ methods: string[];
18
+ withDetect?: boolean;
19
+ }
20
+
21
+ export { lazyDeriveSection };
22
+
23
+ // Enable derive only if some of these modules are available
24
+ const checks: Record<string, Avail> = {
25
+ allianceMotion: {
26
+ instances: ['allianceMotion'],
27
+ methods: []
28
+ },
29
+ bagsList: {
30
+ instances: ['voterBagsList', 'voterList', 'bagsList'],
31
+ methods: [],
32
+ withDetect: true
33
+ },
34
+ contracts: {
35
+ instances: ['contracts'],
36
+ methods: []
37
+ },
38
+ council: {
39
+ instances: ['council'],
40
+ methods: [],
41
+ withDetect: true
42
+ },
43
+ crowdloan: {
44
+ instances: ['crowdloan'],
45
+ methods: []
46
+ },
47
+ democracy: {
48
+ instances: ['democracy'],
49
+ methods: []
50
+ },
51
+ elections: {
52
+ instances: ['phragmenElection', 'electionsPhragmen', 'elections', 'council'],
53
+ methods: [],
54
+ withDetect: true
55
+ },
56
+ imOnline: {
57
+ instances: ['imOnline'],
58
+ methods: []
59
+ },
60
+ membership: {
61
+ instances: ['membership'],
62
+ methods: []
63
+ },
64
+ parachains: {
65
+ instances: ['parachains', 'registrar'],
66
+ methods: []
67
+ },
68
+ session: {
69
+ instances: ['session'],
70
+ methods: []
71
+ },
72
+ society: {
73
+ instances: ['society'],
74
+ methods: []
75
+ },
76
+ staking: {
77
+ instances: ['staking'],
78
+ methods: ['erasRewardPoints']
79
+ },
80
+ technicalCommittee: {
81
+ instances: ['technicalCommittee'],
82
+ methods: [],
83
+ withDetect: true
84
+ },
85
+ treasury: {
86
+ instances: ['treasury'],
87
+ methods: []
88
+ }
89
+ };
90
+
91
+ function getModuleInstances (api: DeriveApi, specName: AnyString, moduleName: string): string[] {
92
+ return api.registry.getModuleInstances(specName, moduleName) || [];
93
+ }
94
+
95
+ /**
96
+ * Returns an object that will inject `api` into all the functions inside
97
+ * `allSections`, and keep the object architecture of `allSections`.
98
+ */
99
+ /** @internal */
100
+ function injectFunctions (instanceId: string, api: DeriveApi, derives: DeriveCustom): ExactDerive {
101
+ const result: Record<string, Record<string, AnyFunction>> = {};
102
+ const names = Object.keys(derives);
103
+ const keys = Object.keys(api.query);
104
+ const specName = api.runtimeVersion.specName;
105
+
106
+ const filterKeys = (q: string) => keys.includes(q);
107
+ const filterInstances = (q: string) => getModuleInstances(api, specName, q).some(filterKeys);
108
+ const filterMethods = (all: string[]) => (m: string) => all.some((q) => keys.includes(q) && api.query[q][m]);
109
+ const getKeys = (s: string) => Object.keys(derives[s]);
110
+ const creator = (s: string, m: string) => derives[s][m](instanceId, api);
111
+ const isIncluded = (c: string) => (!checks[c] || (
112
+ (checks[c].instances.some(filterKeys) && (
113
+ !checks[c].methods.length ||
114
+ checks[c].methods.every(filterMethods(checks[c].instances))
115
+ )) ||
116
+ (
117
+ checks[c].withDetect &&
118
+ checks[c].instances.some(filterInstances)
119
+ )
120
+ ));
121
+
122
+ for (let i = 0, count = names.length; i < count; i++) {
123
+ const name = names[i];
124
+
125
+ isIncluded(name) &&
126
+ lazyDeriveSection(result, name, getKeys, creator);
127
+ }
128
+
129
+ return result as ExactDerive;
130
+ }
131
+
132
+ // FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom}
133
+ // For now we just drop the custom derive typings
134
+ /** @internal */
135
+ export function getAvailableDerives (instanceId: string, api: DeriveApi, custom: DeriveCustom = {}): ExactDerive {
136
+ return {
137
+ ...injectFunctions(instanceId, api, derive as DeriveCustom),
138
+ ...injectFunctions(instanceId, api, custom)
139
+ };
140
+ }
@@ -0,0 +1,21 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { DeriveApi } from '../types.js';
5
+
6
+ import { createBlockNumberDerive } from './util.js';
7
+
8
+ /**
9
+ * @name bestNumber
10
+ * @descrive Retrieves the latest block number.
11
+ * @example
12
+ * ```javascript
13
+ * api.derive.chain.bestNumber((blockNumber) => {
14
+ * console.log(`the current best block is #${blockNumber}`);
15
+ * });
16
+ * ```
17
+ */
18
+ export const bestNumber = /*#__PURE__*/ createBlockNumberDerive(
19
+ (api: DeriveApi) =>
20
+ api.rpc.chain.subscribeNewHeads()
21
+ );
@@ -0,0 +1,22 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { DeriveApi } from '../types.js';
5
+
6
+ import { createBlockNumberDerive } from './util.js';
7
+
8
+ /**
9
+ * @name bestNumberFinalized
10
+ * @returns A BlockNumber
11
+ * @description Get the latest finalized block number.
12
+ * @example
13
+ * ```javascript
14
+ * api.derive.chain.bestNumberFinalized((blockNumber) => {
15
+ * console.log(`the current finalized block is #${blockNumber}`);
16
+ * });
17
+ * ```
18
+ */
19
+ export const bestNumberFinalized = /*#__PURE__*/ createBlockNumberDerive(
20
+ (api: DeriveApi) =>
21
+ api.rpc.chain.subscribeFinalizedHeads()
22
+ );
@@ -0,0 +1,33 @@
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 { BlockNumber } from '@pezkuwi/types/interfaces';
6
+ import type { DeriveApi } from '../types.js';
7
+
8
+ import { combineLatest, map } from 'rxjs';
9
+
10
+ import { memo } from '../util/index.js';
11
+
12
+ /**
13
+ * @name bestNumberLag
14
+ * @returns A number of blocks
15
+ * @description Calculates the lag between finalized head and best head
16
+ * @examplew
17
+ * ```javascript
18
+ * api.derive.chain.bestNumberLag((lag) => {
19
+ * console.log(`finalized is ${lag} blocks behind head`);
20
+ * });
21
+ * ```
22
+ */
23
+ export function bestNumberLag (instanceId: string, api: DeriveApi): () => Observable<BlockNumber> {
24
+ return memo(instanceId, (): Observable<BlockNumber> =>
25
+ combineLatest([
26
+ api.derive.chain.bestNumber(),
27
+ api.derive.chain.bestNumberFinalized()
28
+ ]).pipe(
29
+ map(([bestNumber, bestNumberFinalized]): BlockNumber =>
30
+ api.registry.createType('BlockNumber', bestNumber.sub(bestNumberFinalized))
31
+ )
32
+ ));
33
+ }
@@ -0,0 +1,43 @@
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 { SignedBlockExtended } from '../type/types.js';
6
+ import type { DeriveApi } from '../types.js';
7
+
8
+ import { combineLatest, map, of, switchMap } from 'rxjs';
9
+
10
+ import { createSignedBlockExtended } from '../type/index.js';
11
+ import { memo } from '../util/index.js';
12
+ import { getAuthorDetails } from './util.js';
13
+
14
+ /**
15
+ * @name getBlock
16
+ * @param {( Uint8Array | string )} hash A block hash as U8 array or string.
17
+ * @description Get a specific block (e.g. rpc.chain.getBlock) and extend it with the author
18
+ * @example
19
+ * ```javascript
20
+ * const { author, block } = await api.derive.chain.getBlock('0x123...456');
21
+ *
22
+ * console.log(`block #${block.header.number} was authored by ${author}`);
23
+ * ```
24
+ */
25
+ export function getBlock (instanceId: string, api: DeriveApi): (hash: Uint8Array | string) => Observable<SignedBlockExtended> {
26
+ return memo(instanceId, (blockHash: Uint8Array | string): Observable<SignedBlockExtended> =>
27
+ combineLatest([
28
+ api.rpc.chain.getBlock(blockHash),
29
+ api.queryAt(blockHash)
30
+ ]).pipe(
31
+ switchMap(([signedBlock, queryAt]) =>
32
+ combineLatest([
33
+ of(signedBlock),
34
+ queryAt.system.events(),
35
+ getAuthorDetails(api, signedBlock.block.header, blockHash)
36
+ ])
37
+ ),
38
+ map(([signedBlock, events, [, validators, author]]) =>
39
+ createSignedBlockExtended(events.registry, signedBlock, events, validators, author)
40
+ )
41
+ )
42
+ );
43
+ }
@@ -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 { AnyNumber } from '@pezkuwi/types/types';
6
+ import type { SignedBlockExtended } from '../type/types.js';
7
+ import type { DeriveApi } from '../types.js';
8
+
9
+ import { switchMap } from 'rxjs';
10
+
11
+ import { memo } from '../util/index.js';
12
+
13
+ /**
14
+ * @name getBlockByNumber
15
+ * @param {( BN | bigint | Uint8Array | number | string )} blockNumber
16
+ * @description Get a specific block (e.g. rpc.chain.getBlock) and extend it with the author by block number
17
+ * @example
18
+ * ```javascript
19
+ * const { author, block } = await api.derive.chain.getBlockByNumber(123);
20
+ *
21
+ * console.log(`block #${block.header.number} was authored by ${author}`);
22
+ * ```
23
+ */
24
+ export function getBlockByNumber (instanceId: string, api: DeriveApi): (blockNumber: AnyNumber) => Observable<SignedBlockExtended> {
25
+ return memo(instanceId, (blockNumber: AnyNumber): Observable<SignedBlockExtended> =>
26
+ api.rpc.chain.getBlockHash(blockNumber).pipe(
27
+ switchMap((h) => api.derive.chain.getBlock(h))
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 { HeaderExtended } from '../type/types.js';
6
+ import type { DeriveApi } from '../types.js';
7
+
8
+ import { map, switchMap } from 'rxjs';
9
+
10
+ import { createHeaderExtended } from '../type/index.js';
11
+ import { memo } from '../util/index.js';
12
+ import { getAuthorDetails } from './util.js';
13
+
14
+ /**
15
+ * @name getHeader
16
+ * @param {( Uint8Array | string )} hash - A block hash as U8 array or string.
17
+ * @returns An array containing the block header and the block author
18
+ * @description Get a specific block header and extend it with the author
19
+ * @example
20
+ * ```javascript
21
+ * const { author, number } = await api.derive.chain.getHeader('0x123...456');
22
+ *
23
+ * console.log(`block #${number} was authored by ${author}`);
24
+ * ```
25
+ */
26
+ export function getHeader (instanceId: string, api: DeriveApi): (blockHash: Uint8Array | string) => Observable<HeaderExtended> {
27
+ return memo(instanceId, (blockHash: Uint8Array | string): Observable<HeaderExtended> =>
28
+ api.rpc.chain.getHeader(blockHash).pipe(
29
+ switchMap((header) =>
30
+ getAuthorDetails(api, header, blockHash)
31
+ ),
32
+ map(([header, validators, author]) =>
33
+ createHeaderExtended((validators || header).registry, header, validators, author)
34
+ )
35
+ )
36
+ );
37
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2017-2025 @polkadot/api-derive authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './bestNumber.js';
5
+ export * from './bestNumberFinalized.js';
6
+ export * from './bestNumberLag.js';
7
+ export * from './getBlock.js';
8
+ export * from './getBlockByNumber.js';
9
+ export * from './getHeader.js';
10
+ export * from './subscribeFinalizedBlocks.js';
11
+ export * from './subscribeFinalizedHeads.js';
12
+ export * from './subscribeNewBlocks.js';
13
+ export * from './subscribeNewHeads.js';
@@ -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 { SignedBlockExtended } from '../type/types.js';
6
+ import type { DeriveApi } from '../types.js';
7
+
8
+ import { mergeMap } from 'rxjs';
9
+
10
+ import { memo } from '../util/index.js';
11
+
12
+ /**
13
+ * @name subscribeFinalizedBlocks
14
+ * @description Retrieves the finalized block & events for that block
15
+ * @example
16
+ * ```javascript
17
+ * const unsub = await api.derive.chain.subscribeFinalizedBlocks((finalizedBlock) => {
18
+ * console.log(`# Finalized block ${finalizedBlock.block.hash}`);
19
+ * });
20
+ * ```
21
+ */
22
+ export function subscribeFinalizedBlocks (instanceId: string, api: DeriveApi): () => Observable<SignedBlockExtended> {
23
+ return memo(instanceId, (): Observable<SignedBlockExtended> =>
24
+ api.derive.chain.subscribeFinalizedHeads().pipe(
25
+ mergeMap((header) =>
26
+ api.derive.chain.getBlock(header.createdAtHash || header.hash)
27
+ )
28
+ )
29
+ );
30
+ }