@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.
- package/README.md +30 -0
- package/build/accounts/accountId.d.ts +17 -0
- package/build/accounts/flags.d.ts +29 -0
- package/build/accounts/idAndIndex.d.ts +15 -0
- package/build/accounts/idToIndex.d.ts +16 -0
- package/build/accounts/identity.d.ts +48 -0
- package/build/accounts/index.d.ts +8 -0
- package/build/accounts/indexToId.d.ts +15 -0
- package/build/accounts/indexes.d.ts +16 -0
- package/build/accounts/info.d.ts +18 -0
- package/build/accounts/types.d.ts +39 -0
- package/build/alliance/index.d.ts +70 -0
- package/build/augment.d.ts +1 -0
- package/build/bagsList/get.d.ts +12 -0
- package/build/bagsList/getExpanded.d.ts +16 -0
- package/build/bagsList/index.d.ts +3 -0
- package/build/bagsList/listNodes.d.ts +9 -0
- package/build/bagsList/types.d.ts +13 -0
- package/build/bagsList/util.d.ts +2 -0
- package/build/balances/account.d.ts +17 -0
- package/build/balances/all.d.ts +20 -0
- package/build/balances/index.d.ts +20 -0
- package/build/balances/types.d.ts +69 -0
- package/build/balances/votingBalances.d.ts +15 -0
- package/build/bounties/bounties.d.ts +12 -0
- package/build/bounties/helpers/filterBountyProposals.d.ts +2 -0
- package/build/bounties/index.d.ts +1 -0
- package/build/bundle.d.ts +9 -0
- package/build/chain/bestNumber.d.ts +11 -0
- package/build/chain/bestNumberFinalized.d.ts +12 -0
- package/build/chain/bestNumberLag.d.ts +15 -0
- package/build/chain/getBlock.d.ts +15 -0
- package/build/chain/getBlockByNumber.d.ts +16 -0
- package/build/chain/getHeader.d.ts +16 -0
- package/build/chain/index.d.ts +10 -0
- package/build/chain/subscribeFinalizedBlocks.d.ts +14 -0
- package/build/chain/subscribeFinalizedHeads.d.ts +22 -0
- package/build/chain/subscribeNewBlocks.d.ts +14 -0
- package/build/chain/subscribeNewHeads.d.ts +15 -0
- package/build/chain/util.d.ts +9 -0
- package/build/collective/helpers.d.ts +6 -0
- package/build/collective/index.d.ts +3 -0
- package/build/collective/members.d.ts +2 -0
- package/build/collective/prime.d.ts +2 -0
- package/build/collective/proposals.d.ts +6 -0
- package/build/collective/types.d.ts +19 -0
- package/build/contracts/fees.d.ts +14 -0
- package/build/contracts/index.d.ts +1 -0
- package/build/council/index.d.ts +72 -0
- package/build/council/types.d.ts +6 -0
- package/build/council/votes.d.ts +11 -0
- package/build/council/votesOf.d.ts +16 -0
- package/build/crowdloan/childKey.d.ts +15 -0
- package/build/crowdloan/contributions.d.ts +14 -0
- package/build/crowdloan/index.d.ts +3 -0
- package/build/crowdloan/ownContributions.d.ts +15 -0
- package/build/crowdloan/types.d.ts +6 -0
- package/build/crowdloan/util.d.ts +10 -0
- package/build/democracy/dispatchQueue.d.ts +12 -0
- package/build/democracy/index.d.ts +11 -0
- package/build/democracy/locks.d.ts +14 -0
- package/build/democracy/nextExternal.d.ts +12 -0
- package/build/democracy/preimages.d.ts +24 -0
- package/build/democracy/proposals.d.ts +12 -0
- package/build/democracy/referendumIds.d.ts +12 -0
- package/build/democracy/referendums.d.ts +11 -0
- package/build/democracy/referendumsActive.d.ts +12 -0
- package/build/democracy/referendumsFinished.d.ts +15 -0
- package/build/democracy/referendumsInfo.d.ts +23 -0
- package/build/democracy/sqrtElectorate.d.ts +13 -0
- package/build/democracy/types.d.ts +70 -0
- package/build/democracy/util.d.ts +18 -0
- package/build/derive.d.ts +52 -0
- package/build/elections/index.d.ts +1 -0
- package/build/elections/info.d.ts +14 -0
- package/build/elections/types.d.ts +18 -0
- package/build/imOnline/index.d.ts +1 -0
- package/build/imOnline/receivedHeartbeats.d.ts +13 -0
- package/build/index.d.ts +2 -0
- package/build/membership/index.d.ts +70 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/parachains/index.d.ts +2 -0
- package/build/parachains/info.d.ts +19 -0
- package/build/parachains/overview.d.ts +15 -0
- package/build/parachains/types.d.ts +38 -0
- package/build/parachains/util.d.ts +3 -0
- package/build/session/index.d.ts +3 -0
- package/build/session/indexes.d.ts +15 -0
- package/build/session/info.d.ts +13 -0
- package/build/session/progress.d.ts +47 -0
- package/build/session/types.d.ts +19 -0
- package/build/society/candidates.d.ts +12 -0
- package/build/society/index.d.ts +4 -0
- package/build/society/info.d.ts +12 -0
- package/build/society/member.d.ts +14 -0
- package/build/society/members.d.ts +14 -0
- package/build/society/types.d.ts +27 -0
- package/build/staking/account.d.ts +31 -0
- package/build/staking/cache.d.ts +12 -0
- package/build/staking/currentPoints.d.ts +13 -0
- package/build/staking/electedInfo.d.ts +23 -0
- package/build/staking/erasExposure.d.ts +30 -0
- package/build/staking/erasHistoric.d.ts +8 -0
- package/build/staking/erasPoints.d.ts +18 -0
- package/build/staking/erasPrefs.d.ts +27 -0
- package/build/staking/erasRewards.d.ts +14 -0
- package/build/staking/erasSlashes.d.ts +26 -0
- package/build/staking/index.d.ts +22 -0
- package/build/staking/keys.d.ts +35 -0
- package/build/staking/overview.d.ts +19 -0
- package/build/staking/ownExposure.d.ts +37 -0
- package/build/staking/ownSlashes.d.ts +35 -0
- package/build/staking/query.d.ts +31 -0
- package/build/staking/stakerExposure.d.ts +33 -0
- package/build/staking/stakerPoints.d.ts +22 -0
- package/build/staking/stakerPrefs.d.ts +24 -0
- package/build/staking/stakerRewards.d.ts +49 -0
- package/build/staking/stakerSlashes.d.ts +22 -0
- package/build/staking/stashes.d.ts +16 -0
- package/build/staking/types.d.ts +139 -0
- package/build/staking/util.d.ts +14 -0
- package/build/staking/validators.d.ts +33 -0
- package/build/staking/waitingInfo.d.ts +16 -0
- package/build/technicalCommittee/index.d.ts +70 -0
- package/build/treasury/index.d.ts +1 -0
- package/build/treasury/proposals.d.ts +12 -0
- package/build/tx/constants.d.ts +5 -0
- package/build/tx/events.d.ts +22 -0
- package/build/tx/extrinsicInfo.d.ts +61 -0
- package/build/tx/index.d.ts +3 -0
- package/build/tx/signingInfo.d.ts +25 -0
- package/build/type/HeaderExtended.d.ts +4 -0
- package/build/type/SignedBlockExtended.d.ts +4 -0
- package/build/type/index.d.ts +2 -0
- package/build/type/types.d.ts +15 -0
- package/build/type/util.d.ts +2 -0
- package/build/types.d.ts +79 -0
- package/build/util/approvalFlagsToBools.d.ts +4 -0
- package/build/util/blockNumber.d.ts +7 -0
- package/build/util/cache.d.ts +4 -0
- package/build/util/cacheImpl.d.ts +3 -0
- package/build/util/first.d.ts +4 -0
- package/build/util/index.d.ts +7 -0
- package/build/util/lazy.d.ts +4 -0
- package/build/util/types.d.ts +6 -0
- package/package.json +42 -0
- package/src/accounts/accountId.ts +44 -0
- package/src/accounts/flags.ts +90 -0
- package/src/accounts/idAndIndex.ts +51 -0
- package/src/accounts/idToIndex.ts +31 -0
- package/src/accounts/identity.ts +235 -0
- package/src/accounts/index.ts +11 -0
- package/src/accounts/indexToId.ts +33 -0
- package/src/accounts/indexes.ts +56 -0
- package/src/accounts/info.ts +58 -0
- package/src/accounts/types.ts +48 -0
- package/src/alliance/index.ts +76 -0
- package/src/augment.ts +4 -0
- package/src/bagsList/get.ts +79 -0
- package/src/bagsList/getExpanded.ts +41 -0
- package/src/bagsList/index.ts +6 -0
- package/src/bagsList/listNodes.ts +50 -0
- package/src/bagsList/types.ts +18 -0
- package/src/bagsList/util.ts +14 -0
- package/src/balances/account.ts +210 -0
- package/src/balances/all.ts +246 -0
- package/src/balances/index.ts +26 -0
- package/src/balances/types.ts +79 -0
- package/src/balances/votingBalances.ts +33 -0
- package/src/bounties/bounties.spec.ts +162 -0
- package/src/bounties/bounties.ts +76 -0
- package/src/bounties/helpers/filterBountyProposals.ts +13 -0
- package/src/bounties/index.ts +4 -0
- package/src/bundle.ts +140 -0
- package/src/chain/bestNumber.ts +21 -0
- package/src/chain/bestNumberFinalized.ts +22 -0
- package/src/chain/bestNumberLag.ts +33 -0
- package/src/chain/getBlock.ts +43 -0
- package/src/chain/getBlockByNumber.ts +30 -0
- package/src/chain/getHeader.ts +37 -0
- package/src/chain/index.ts +13 -0
- package/src/chain/subscribeFinalizedBlocks.ts +30 -0
- package/src/chain/subscribeFinalizedHeads.ts +62 -0
- package/src/chain/subscribeNewBlocks.ts +30 -0
- package/src/chain/subscribeNewHeads.ts +38 -0
- package/src/chain/util.ts +112 -0
- package/src/checkTypes.manual.ts +8 -0
- package/src/collective/helpers.ts +36 -0
- package/src/collective/index.ts +6 -0
- package/src/collective/members.ts +8 -0
- package/src/collective/prime.ts +25 -0
- package/src/collective/proposals.ts +73 -0
- package/src/collective/types.ts +30 -0
- package/src/contracts/fees.ts +61 -0
- package/src/contracts/index.ts +4 -0
- package/src/council/index.ts +79 -0
- package/src/council/types.ts +11 -0
- package/src/council/votes.ts +97 -0
- package/src/council/votesOf.ts +35 -0
- package/src/crowdloan/childKey.ts +57 -0
- package/src/crowdloan/contributions.ts +142 -0
- package/src/crowdloan/index.ts +6 -0
- package/src/crowdloan/ownContributions.ts +81 -0
- package/src/crowdloan/types.ts +11 -0
- package/src/crowdloan/util.ts +33 -0
- package/src/democracy/dispatchQueue.ts +158 -0
- package/src/democracy/index.ts +14 -0
- package/src/democracy/locks.ts +114 -0
- package/src/democracy/nextExternal.ts +49 -0
- package/src/democracy/preimages.ts +171 -0
- package/src/democracy/proposals.ts +81 -0
- package/src/democracy/referendumIds.ts +37 -0
- package/src/democracy/referendums.ts +39 -0
- package/src/democracy/referendumsActive.ts +30 -0
- package/src/democracy/referendumsFinished.ts +37 -0
- package/src/democracy/referendumsInfo.ts +181 -0
- package/src/democracy/sqrtElectorate.ts +29 -0
- package/src/democracy/types.ts +84 -0
- package/src/democracy/util.ts +166 -0
- package/src/derive.ts +41 -0
- package/src/elections/index.ts +4 -0
- package/src/elections/info.ts +126 -0
- package/src/elections/types.ts +22 -0
- package/src/imOnline/index.ts +4 -0
- package/src/imOnline/receivedHeartbeats.ts +65 -0
- package/src/index.spec.ts +103 -0
- package/src/index.ts +6 -0
- package/src/membership/index.ts +76 -0
- package/src/mod.ts +4 -0
- package/src/packageDetect.ts +11 -0
- package/src/packageInfo.ts +6 -0
- package/src/parachains/index.ts +5 -0
- package/src/parachains/info.ts +111 -0
- package/src/parachains/overview.ts +63 -0
- package/src/parachains/types.ts +46 -0
- package/src/parachains/util.ts +11 -0
- package/src/session/index.ts +6 -0
- package/src/session/indexes.ts +90 -0
- package/src/session/info.ts +39 -0
- package/src/session/progress.ts +130 -0
- package/src/session/types.ts +25 -0
- package/src/society/candidates.ts +71 -0
- package/src/society/index.ts +7 -0
- package/src/society/info.ts +50 -0
- package/src/society/member.ts +28 -0
- package/src/society/members.ts +107 -0
- package/src/society/types.ts +33 -0
- package/src/staking/account.ts +112 -0
- package/src/staking/cache.ts +48 -0
- package/src/staking/currentPoints.ts +28 -0
- package/src/staking/electedInfo.ts +54 -0
- package/src/staking/erasExposure.ts +105 -0
- package/src/staking/erasHistoric.ts +47 -0
- package/src/staking/erasPoints.ts +67 -0
- package/src/staking/erasPrefs.ts +63 -0
- package/src/staking/erasRewards.ts +52 -0
- package/src/staking/erasSlashes.ts +69 -0
- package/src/staking/index.ts +25 -0
- package/src/staking/keys.ts +86 -0
- package/src/staking/overview.ts +42 -0
- package/src/staking/ownExposure.ts +83 -0
- package/src/staking/ownSlashes.ts +68 -0
- package/src/staking/query.ts +230 -0
- package/src/staking/stakerExposure.ts +78 -0
- package/src/staking/stakerPoints.ts +46 -0
- package/src/staking/stakerPrefs.ts +45 -0
- package/src/staking/stakerRewards.ts +298 -0
- package/src/staking/stakerSlashes.ts +45 -0
- package/src/staking/stashes.ts +55 -0
- package/src/staking/types.ts +174 -0
- package/src/staking/util.ts +94 -0
- package/src/staking/validators.ts +82 -0
- package/src/staking/waitingInfo.ts +45 -0
- package/src/technicalCommittee/index.ts +76 -0
- package/src/test/bountyFactory.ts +39 -0
- package/src/test/bytesFactory.ts +15 -0
- package/src/test/helpers.ts +23 -0
- package/src/test/proposalFactory.ts +25 -0
- package/src/treasury/index.ts +4 -0
- package/src/treasury/proposals.ts +109 -0
- package/src/tx/constants.ts +13 -0
- package/src/tx/events.ts +43 -0
- package/src/tx/extrinsicInfo.ts +123 -0
- package/src/tx/index.ts +6 -0
- package/src/tx/signingInfo.ts +129 -0
- package/src/type/HeaderExtended.ts +33 -0
- package/src/type/SignedBlockExtended.ts +75 -0
- package/src/type/index.ts +5 -0
- package/src/type/types.ts +21 -0
- package/src/type/util.ts +45 -0
- package/src/types.ts +97 -0
- package/src/util/approvalFlagToBools.spec.ts +37 -0
- package/src/util/approvalFlagsToBools.ts +26 -0
- package/src/util/blockNumber.ts +15 -0
- package/src/util/cache.ts +65 -0
- package/src/util/cacheImpl.ts +30 -0
- package/src/util/first.ts +20 -0
- package/src/util/index.ts +13 -0
- package/src/util/lazy.ts +16 -0
- package/src/util/types.ts +9 -0
- package/tsconfig.build.json +21 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +27 -0
|
@@ -0,0 +1,65 @@
|
|
|
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 { AccountId } from '@pezkuwi/types/interfaces';
|
|
7
|
+
import type { Codec } from '@pezkuwi/types/types';
|
|
8
|
+
import type { DeriveApi, DeriveHeartbeats } from '../types.js';
|
|
9
|
+
|
|
10
|
+
import { combineLatest, map, of, switchMap } from 'rxjs';
|
|
11
|
+
|
|
12
|
+
import { BN_ZERO } from '@pezkuwi/util';
|
|
13
|
+
|
|
14
|
+
import { memo } from '../util/index.js';
|
|
15
|
+
|
|
16
|
+
type Result = [DeriveHeartbeats, AccountId[], Option<Codec>[], u32[]];
|
|
17
|
+
|
|
18
|
+
function mapResult ([result, validators, heartbeats, numBlocks]: Result): DeriveHeartbeats {
|
|
19
|
+
validators.forEach((validator, index): void => {
|
|
20
|
+
const validatorId = validator.toString();
|
|
21
|
+
const blockCount = numBlocks[index];
|
|
22
|
+
const hasMessage = !heartbeats[index].isEmpty;
|
|
23
|
+
const prev = result[validatorId];
|
|
24
|
+
|
|
25
|
+
if (!prev || prev.hasMessage !== hasMessage || !prev.blockCount.eq(blockCount)) {
|
|
26
|
+
result[validatorId] = {
|
|
27
|
+
blockCount,
|
|
28
|
+
hasMessage,
|
|
29
|
+
isOnline: hasMessage || blockCount.gt(BN_ZERO)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @name receivedHeartbeats
|
|
39
|
+
* @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session.
|
|
40
|
+
* @example
|
|
41
|
+
* ```javascript
|
|
42
|
+
* let unsub = await api.derive.imOnline.receivedHeartbeats((heartbeat) => {
|
|
43
|
+
* console.log(heartbeat);
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function receivedHeartbeats (instanceId: string, api: DeriveApi): () => Observable<DeriveHeartbeats> {
|
|
48
|
+
return memo(instanceId, (): Observable<DeriveHeartbeats> =>
|
|
49
|
+
api.query.imOnline?.receivedHeartbeats
|
|
50
|
+
? api.derive.staking.overview().pipe(
|
|
51
|
+
switchMap(({ currentIndex, validators }): Observable<Result> =>
|
|
52
|
+
combineLatest([
|
|
53
|
+
of({}),
|
|
54
|
+
of(validators),
|
|
55
|
+
api.query.imOnline.receivedHeartbeats.multi(
|
|
56
|
+
validators.map((_address, index) => [currentIndex, index])),
|
|
57
|
+
api.query.imOnline.authoredBlocks.multi(
|
|
58
|
+
validators.map((address) => [currentIndex, address]))
|
|
59
|
+
])
|
|
60
|
+
),
|
|
61
|
+
map(mapResult)
|
|
62
|
+
)
|
|
63
|
+
: of({})
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
8
|
+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
9
|
+
|
|
10
|
+
import type { ExactDerive } from './index.js';
|
|
11
|
+
|
|
12
|
+
import { from, Observable } from 'rxjs';
|
|
13
|
+
|
|
14
|
+
import { ApiRx } from '@pezkuwi/api';
|
|
15
|
+
import { MockProvider } from '@pezkuwi/rpc-provider/mock';
|
|
16
|
+
import { TypeRegistry } from '@pezkuwi/types/create';
|
|
17
|
+
|
|
18
|
+
const testFunction = (api: ApiRx): any => {
|
|
19
|
+
return <S extends keyof ExactDerive, M extends keyof (typeof api.derive[S])>(section: S, method: M, inputs: any[]): void => {
|
|
20
|
+
describe(`derive.${section}.${method as string}`, (): void => {
|
|
21
|
+
it('should be a function', (): void => {
|
|
22
|
+
expect(typeof api.derive[section][method]).toBe('function');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return an Observable', (): void => {
|
|
26
|
+
expect((api.derive[section][method] as any)(...inputs)).toBeInstanceOf(Observable);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function waitReady (api: ApiRx): Promise<ApiRx> {
|
|
33
|
+
return new Promise<ApiRx>((resolve) =>
|
|
34
|
+
api.isReady.subscribe((api) => resolve(api))
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('derive', (): void => {
|
|
39
|
+
const registry = new TypeRegistry();
|
|
40
|
+
|
|
41
|
+
describe('builtin', (): void => {
|
|
42
|
+
const api = new ApiRx({ provider: new MockProvider(registry), registry });
|
|
43
|
+
|
|
44
|
+
beforeAll(async () => {
|
|
45
|
+
await waitReady(api);
|
|
46
|
+
});
|
|
47
|
+
afterAll(() => api.disconnect());
|
|
48
|
+
|
|
49
|
+
testFunction(api)('accounts', 'idAndIndex', []);
|
|
50
|
+
testFunction(api)('accounts', 'idToIndex', []);
|
|
51
|
+
testFunction(api)('accounts', 'indexes', []);
|
|
52
|
+
testFunction(api)('accounts', 'indexToId', []);
|
|
53
|
+
|
|
54
|
+
testFunction(api)('balances', 'all', []);
|
|
55
|
+
testFunction(api)('balances', 'votingBalance', []);
|
|
56
|
+
testFunction(api)('balances', 'votingBalances', []);
|
|
57
|
+
|
|
58
|
+
testFunction(api)('chain', 'bestNumber', []);
|
|
59
|
+
testFunction(api)('chain', 'bestNumberFinalized', []);
|
|
60
|
+
|
|
61
|
+
testFunction(api)('democracy', 'proposals', []);
|
|
62
|
+
testFunction(api)('democracy', 'referendums', []);
|
|
63
|
+
|
|
64
|
+
testFunction(api)('elections', 'info', []);
|
|
65
|
+
|
|
66
|
+
testFunction(api)('session', 'eraLength', []);
|
|
67
|
+
testFunction(api)('session', 'eraProgress', []);
|
|
68
|
+
testFunction(api)('session', 'sessionProgress', []);
|
|
69
|
+
|
|
70
|
+
testFunction(api)('staking', 'account', []);
|
|
71
|
+
testFunction(api)('staking', 'stashes', []);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('custom', (): void => {
|
|
75
|
+
const api = new ApiRx({
|
|
76
|
+
derives: {
|
|
77
|
+
balances: {
|
|
78
|
+
fees: (): any => (): Observable<any> => from(['a', 'b'])
|
|
79
|
+
},
|
|
80
|
+
custom: {
|
|
81
|
+
test: (): any => (): Observable<any> => from([1, 2, 3])
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
provider: new MockProvider(registry),
|
|
85
|
+
registry,
|
|
86
|
+
throwOnConnect: true
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
beforeAll(async () => {
|
|
90
|
+
await waitReady(api);
|
|
91
|
+
});
|
|
92
|
+
afterAll(() => api.disconnect());
|
|
93
|
+
|
|
94
|
+
// override
|
|
95
|
+
testFunction(api)('balances', 'fees', ['a', 'b']);
|
|
96
|
+
|
|
97
|
+
// new
|
|
98
|
+
testFunction(api)('custom', 'test', [1, 2, 3]);
|
|
99
|
+
|
|
100
|
+
// existing
|
|
101
|
+
testFunction(api)('chain', 'bestNumber', []);
|
|
102
|
+
});
|
|
103
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-derive authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @name members
|
|
8
|
+
* @description Retrieves the list of members in the "membership" collective.
|
|
9
|
+
* @example
|
|
10
|
+
* ```javascript
|
|
11
|
+
* const members = await api.derive.membership.members();
|
|
12
|
+
* console.log(`Members: ${JSON.stringify(members)});
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export const members = /*#__PURE__*/ collectiveMembers('membership');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @name hasProposals
|
|
19
|
+
* @description Checks if there are any active proposals in the "membership" collective.
|
|
20
|
+
* @example
|
|
21
|
+
* ```javascript
|
|
22
|
+
* const exists = await api.derive.membership.hasProposals();
|
|
23
|
+
* console.log(exists);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const hasProposals = /*#__PURE__*/ collectiveHasProposals('membership');
|
|
27
|
+
/**
|
|
28
|
+
* @name proposal
|
|
29
|
+
* @description Retrieves details of a specific proposal in the "membership" collective by its hash.
|
|
30
|
+
* @example
|
|
31
|
+
* ```javascript
|
|
32
|
+
* const proposalDetails = await api.derive.membership.proposal(PROPOSAL_HASH);
|
|
33
|
+
* console.log(proposalDetails);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const proposal = /*#__PURE__*/ collectiveProposal('membership');
|
|
37
|
+
/**
|
|
38
|
+
* @name proposalCount
|
|
39
|
+
* @description Retrieves the total number of proposals in the "membership" collective.
|
|
40
|
+
* @example
|
|
41
|
+
* ```javascript
|
|
42
|
+
* const count = await api.derive.membership.proposalCount();
|
|
43
|
+
* console.log(`Amount of proposals: ${count}`);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export const proposalCount = /*#__PURE__*/ collectiveProposalCount('membership');
|
|
47
|
+
/**
|
|
48
|
+
* @name proposalHashes
|
|
49
|
+
* @description Retrieves an array of hashes for all active proposals in the "membership" collective.
|
|
50
|
+
* @example
|
|
51
|
+
* ```javascript
|
|
52
|
+
* const hashes = await api.derive.membership.proposalHashes();
|
|
53
|
+
* console.log(`Proposals ${JSON.stringify(hashes)}`);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const proposalHashes = /*#__PURE__*/ collectiveProposalHashes('membership');
|
|
57
|
+
/**
|
|
58
|
+
* @name proposals
|
|
59
|
+
* @description Retrieves a list of all active proposals in the "membership" collective.
|
|
60
|
+
* @example
|
|
61
|
+
* ```javascript
|
|
62
|
+
* const proposals = await api.derive.membership.proposals();
|
|
63
|
+
* console.log(proposals);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export const proposals = /*#__PURE__*/ collectiveProposals('membership');
|
|
67
|
+
/**
|
|
68
|
+
* @name prime
|
|
69
|
+
* @description Retrieves the prime member of the "membership" collective, if one exists.
|
|
70
|
+
* @example
|
|
71
|
+
* ```javascript
|
|
72
|
+
* const primeMember = await api.derive.membership.prime();
|
|
73
|
+
* console.log(primeMember);
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export const prime = /*#__PURE__*/ collectivePrime('membership');
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-derive authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
// (packageInfo imports will be kept as-is, user-editable)
|
|
6
|
+
|
|
7
|
+
import { detectPackage } from '@pezkuwi/util';
|
|
8
|
+
|
|
9
|
+
import { packageInfo } from './packageInfo.js';
|
|
10
|
+
|
|
11
|
+
detectPackage(packageInfo, null, []);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-derive authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
|
|
6
|
+
export const packageInfo = { name: '@pezkuwi/api-derive', path: 'auto', type: 'auto', version: '16.5.5' };
|
|
@@ -0,0 +1,111 @@
|
|
|
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 { CollatorId, ParaId } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { DeriveApi, DeriveParachainActive, DeriveParachainFull, DeriveParachainInfo } from '../types.js';
|
|
7
|
+
import type { Active, DidUpdate, Heads, ParaInfoResult, PendingSwap, RelayDispatchQueue, RetryQueue, SelectedThreads } from './types.js';
|
|
8
|
+
|
|
9
|
+
import { map, of } from 'rxjs';
|
|
10
|
+
|
|
11
|
+
import { objectSpread } from '@pezkuwi/util';
|
|
12
|
+
|
|
13
|
+
import { memo } from '../util/index.js';
|
|
14
|
+
import { didUpdateToBool } from './util.js';
|
|
15
|
+
|
|
16
|
+
type Result = [
|
|
17
|
+
Active,
|
|
18
|
+
RetryQueue,
|
|
19
|
+
SelectedThreads,
|
|
20
|
+
DidUpdate,
|
|
21
|
+
ParaInfoResult,
|
|
22
|
+
PendingSwap,
|
|
23
|
+
Heads,
|
|
24
|
+
RelayDispatchQueue
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
|
|
28
|
+
const found = active.find(([paraId]) => paraId === id);
|
|
29
|
+
|
|
30
|
+
if (found && found[1].isSome) {
|
|
31
|
+
const [collatorId, retriable] = found[1].unwrap();
|
|
32
|
+
|
|
33
|
+
return objectSpread<DeriveParachainActive>(
|
|
34
|
+
{ collatorId },
|
|
35
|
+
retriable.isWithRetries
|
|
36
|
+
? {
|
|
37
|
+
isRetriable: true,
|
|
38
|
+
retries: retriable.asWithRetries.toNumber()
|
|
39
|
+
}
|
|
40
|
+
: {
|
|
41
|
+
isRetriable: false,
|
|
42
|
+
retries: 0
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
|
|
51
|
+
return collatorQueue.map((queue): CollatorId | null => {
|
|
52
|
+
const found = queue.find(([paraId]) => paraId === id);
|
|
53
|
+
|
|
54
|
+
return found ? found[1] : null;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue]: Result): DeriveParachainFull | null {
|
|
59
|
+
if (info.isNone) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
active: parseActive(id, active),
|
|
65
|
+
didUpdate: didUpdateToBool(didUpdate, id),
|
|
66
|
+
heads,
|
|
67
|
+
id,
|
|
68
|
+
info: objectSpread<DeriveParachainInfo>({ id }, info.unwrap()),
|
|
69
|
+
pendingSwapId: pendingSwap.unwrapOr(null),
|
|
70
|
+
relayDispatchQueue,
|
|
71
|
+
retryCollators: parseCollators(id, retryQueue),
|
|
72
|
+
selectedCollators: parseCollators(id, selectedThreads)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @name info
|
|
78
|
+
* @param {ParaId | number} id A unique numeric (non-negative integer) identifier for a parachain.
|
|
79
|
+
* @description Retrieves detailed information about a specific parachain.
|
|
80
|
+
* @example
|
|
81
|
+
* ```javascript
|
|
82
|
+
* await api.derive.parachains.info(1000, (info) => {
|
|
83
|
+
* if (info) {
|
|
84
|
+
* console.log(`Parachain ${info.id.toString()} is active: ${info.active}`);
|
|
85
|
+
* } else {
|
|
86
|
+
* console.log("Parachain information not available.");
|
|
87
|
+
* }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function info (instanceId: string, api: DeriveApi): (id: ParaId | number) => Observable<DeriveParachainFull | null> {
|
|
92
|
+
return memo(instanceId, (id: ParaId | number): Observable<DeriveParachainFull | null> =>
|
|
93
|
+
api.query['registrar'] && api.query['parachains']
|
|
94
|
+
? api.queryMulti<Result>([
|
|
95
|
+
api.query['registrar']['active'],
|
|
96
|
+
api.query['registrar']['retryQueue'],
|
|
97
|
+
api.query['registrar']['selectedThreads'],
|
|
98
|
+
api.query['parachains']['didUpdate'],
|
|
99
|
+
[api.query['registrar']['paras'], id],
|
|
100
|
+
[api.query['registrar']['pendingSwap'], id],
|
|
101
|
+
[api.query['parachains']['heads'], id],
|
|
102
|
+
[api.query['parachains']['relayDispatchQueue'], id]
|
|
103
|
+
])
|
|
104
|
+
.pipe(
|
|
105
|
+
map((result: Result): DeriveParachainFull | null =>
|
|
106
|
+
parse(api.registry.createType('ParaId', id), result)
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
: of(null)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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 { ParaId } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { DeriveApi, DeriveParachain, DeriveParachainInfo } from '../types.js';
|
|
7
|
+
import type { DidUpdate, ParaInfoResult, PendingSwap, RelayDispatchQueueSize } from './types.js';
|
|
8
|
+
|
|
9
|
+
import { combineLatest, map, of, switchMap } from 'rxjs';
|
|
10
|
+
|
|
11
|
+
import { objectSpread } from '@pezkuwi/util';
|
|
12
|
+
|
|
13
|
+
import { memo } from '../util/index.js';
|
|
14
|
+
import { didUpdateToBool } from './util.js';
|
|
15
|
+
|
|
16
|
+
type Result = [
|
|
17
|
+
ParaId[],
|
|
18
|
+
DidUpdate,
|
|
19
|
+
RelayDispatchQueueSize[],
|
|
20
|
+
ParaInfoResult[],
|
|
21
|
+
PendingSwap[]
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function parse ([ids, didUpdate, relayDispatchQueueSizes, infos, pendingSwaps]: Result): DeriveParachain[] {
|
|
25
|
+
return ids.map((id, index): DeriveParachain => ({
|
|
26
|
+
didUpdate: didUpdateToBool(didUpdate, id),
|
|
27
|
+
id,
|
|
28
|
+
info: objectSpread<DeriveParachainInfo>({ id }, infos[index].unwrapOr(null)),
|
|
29
|
+
pendingSwapId: pendingSwaps[index].unwrapOr(null),
|
|
30
|
+
relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber()
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @name overview
|
|
36
|
+
* @description Retrieves an overview of all registered parachains.
|
|
37
|
+
* @example
|
|
38
|
+
* ```javascript
|
|
39
|
+
* await api.derive.parachains.overview((overview) => {
|
|
40
|
+
* parachains.forEach(parachain => {
|
|
41
|
+
* console.log(`Parachain ${parachain.id.toString()} is registered.`);
|
|
42
|
+
* });
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function overview (instanceId: string, api: DeriveApi): () => Observable<DeriveParachain[]> {
|
|
47
|
+
return memo(instanceId, (): Observable<DeriveParachain[]> =>
|
|
48
|
+
api.query['registrar']?.['parachains'] && api.query['parachains']
|
|
49
|
+
? api.query['registrar']['parachains']<ParaId[]>().pipe(
|
|
50
|
+
switchMap((paraIds) =>
|
|
51
|
+
combineLatest([
|
|
52
|
+
of(paraIds),
|
|
53
|
+
api.query['parachains']['didUpdate']<DidUpdate>(),
|
|
54
|
+
api.query['parachains']['relayDispatchQueueSize'].multi<RelayDispatchQueueSize>(paraIds),
|
|
55
|
+
api.query['registrar']['paras'].multi<ParaInfoResult>(paraIds),
|
|
56
|
+
api.query['registrar']['pendingSwap'].multi<PendingSwap>(paraIds)
|
|
57
|
+
])
|
|
58
|
+
),
|
|
59
|
+
map(parse)
|
|
60
|
+
)
|
|
61
|
+
: of([] as DeriveParachain[])
|
|
62
|
+
);
|
|
63
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-derive authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Bytes, Option, u32, Vec } from '@pezkuwi/types';
|
|
5
|
+
import type { CollatorId, ParaId, ParaInfo, Retriable, UpwardMessage } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { ITuple } from '@pezkuwi/types/types';
|
|
7
|
+
|
|
8
|
+
export type ParaInfoResult = Option<ParaInfo>;
|
|
9
|
+
export type PendingSwap = Option<ParaId>;
|
|
10
|
+
export type Active = Vec<ITuple<[ParaId, Option<ITuple<[CollatorId, Retriable]>>]>>;
|
|
11
|
+
export type RetryQueue = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
|
|
12
|
+
export type SelectedThreads = Vec<Vec<ITuple<[ParaId, CollatorId]>>>
|
|
13
|
+
export type Code = Bytes;
|
|
14
|
+
export type Heads = Bytes;
|
|
15
|
+
export type RelayDispatchQueue = Vec<UpwardMessage>
|
|
16
|
+
export type RelayDispatchQueueSize = ITuple<[u32, u32]>;
|
|
17
|
+
export type DidUpdate = Option<Vec<ParaId>>;
|
|
18
|
+
|
|
19
|
+
export interface DeriveParachainActive {
|
|
20
|
+
collatorId: CollatorId;
|
|
21
|
+
isRetriable: boolean;
|
|
22
|
+
retries: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DeriveParachainInfo extends ParaInfo {
|
|
26
|
+
id: ParaId;
|
|
27
|
+
icon?: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
owner?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DeriveParachain {
|
|
33
|
+
didUpdate: boolean;
|
|
34
|
+
pendingSwapId: ParaId | null;
|
|
35
|
+
id: ParaId;
|
|
36
|
+
info: DeriveParachainInfo | null;
|
|
37
|
+
relayDispatchQueueSize?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DeriveParachainFull extends DeriveParachain {
|
|
41
|
+
active: DeriveParachainActive | null;
|
|
42
|
+
heads: Bytes | null;
|
|
43
|
+
relayDispatchQueue: UpwardMessage[];
|
|
44
|
+
retryCollators: (CollatorId | null)[];
|
|
45
|
+
selectedCollators: (CollatorId | null)[];
|
|
46
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/api-derive authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ParaId } from '@pezkuwi/types/interfaces';
|
|
5
|
+
import type { DidUpdate } from './types.js';
|
|
6
|
+
|
|
7
|
+
export function didUpdateToBool (didUpdate: DidUpdate, id: ParaId): boolean {
|
|
8
|
+
return didUpdate.isSome
|
|
9
|
+
? didUpdate.unwrap().some((paraId) => paraId.eq(id))
|
|
10
|
+
: false;
|
|
11
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
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 { ActiveEraInfo, EraIndex, Moment, SessionIndex } from '@pezkuwi/types/interfaces';
|
|
7
|
+
import type { DeriveApi, DeriveSessionIndexes } from '../types.js';
|
|
8
|
+
|
|
9
|
+
import { map, of } from 'rxjs';
|
|
10
|
+
|
|
11
|
+
import { memo } from '../util/index.js';
|
|
12
|
+
|
|
13
|
+
// parse into Indexes
|
|
14
|
+
function parse ([currentIndex, activeEra, activeEraStart, currentEra, validatorCount]: [SessionIndex, EraIndex, Option<Moment>, EraIndex, u32]): DeriveSessionIndexes {
|
|
15
|
+
return {
|
|
16
|
+
activeEra,
|
|
17
|
+
activeEraStart,
|
|
18
|
+
currentEra,
|
|
19
|
+
currentIndex,
|
|
20
|
+
validatorCount
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// query based on latest
|
|
25
|
+
function queryStaking (api: DeriveApi): Observable<DeriveSessionIndexes> {
|
|
26
|
+
return api.queryMulti<[SessionIndex, Option<ActiveEraInfo>, Option<EraIndex>, u32]>([
|
|
27
|
+
api.query.session.currentIndex,
|
|
28
|
+
api.query.staking.activeEra,
|
|
29
|
+
api.query.staking.currentEra,
|
|
30
|
+
api.query.staking.validatorCount
|
|
31
|
+
]).pipe(
|
|
32
|
+
map(([currentIndex, activeOpt, currentEra, validatorCount]): DeriveSessionIndexes => {
|
|
33
|
+
const { index, start } = activeOpt.unwrapOrDefault();
|
|
34
|
+
|
|
35
|
+
return parse([
|
|
36
|
+
currentIndex,
|
|
37
|
+
index,
|
|
38
|
+
start,
|
|
39
|
+
currentEra.unwrapOrDefault(),
|
|
40
|
+
validatorCount
|
|
41
|
+
]);
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// query based on latest
|
|
47
|
+
function querySession (api: DeriveApi): Observable<DeriveSessionIndexes> {
|
|
48
|
+
return api.query.session.currentIndex().pipe(
|
|
49
|
+
map((currentIndex): DeriveSessionIndexes => parse([
|
|
50
|
+
currentIndex,
|
|
51
|
+
api.registry.createType('EraIndex'),
|
|
52
|
+
api.registry.createType('Option<Moment>'),
|
|
53
|
+
api.registry.createType('EraIndex'),
|
|
54
|
+
api.registry.createType('u32')
|
|
55
|
+
]))
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// empty set when none is available
|
|
60
|
+
function empty (api: DeriveApi): Observable<DeriveSessionIndexes> {
|
|
61
|
+
return of(parse([
|
|
62
|
+
api.registry.createType('SessionIndex', 1),
|
|
63
|
+
api.registry.createType('EraIndex'),
|
|
64
|
+
api.registry.createType('Option<Moment>'),
|
|
65
|
+
api.registry.createType('EraIndex'),
|
|
66
|
+
api.registry.createType('u32')
|
|
67
|
+
]));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @name indexes
|
|
72
|
+
* @description Retrieves session-related index data, adapting to whether
|
|
73
|
+
* the chain has staking enabled.
|
|
74
|
+
* @example
|
|
75
|
+
* ```javascript
|
|
76
|
+
* api.derive.session.indexes((indexes) => {
|
|
77
|
+
* console.log(`Current session index: ${indexes.currentIndex}`);
|
|
78
|
+
* console.log(`Validator count: ${indexes.validatorCount}`);
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function indexes (instanceId: string, api: DeriveApi): () => Observable<DeriveSessionIndexes> {
|
|
83
|
+
return memo(instanceId, (): Observable<DeriveSessionIndexes> =>
|
|
84
|
+
api.query.session
|
|
85
|
+
? api.query.staking
|
|
86
|
+
? queryStaking(api)
|
|
87
|
+
: querySession(api)
|
|
88
|
+
: empty(api)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
@@ -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, DeriveSessionInfo } from '../types.js';
|
|
6
|
+
|
|
7
|
+
import { map } from 'rxjs';
|
|
8
|
+
|
|
9
|
+
import { objectSpread } from '@pezkuwi/util';
|
|
10
|
+
|
|
11
|
+
import { memo } from '../util/index.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @name info
|
|
15
|
+
* @description Retrieves all the session and era query and calculates specific values on it as the length of the session and eras.
|
|
16
|
+
* @example
|
|
17
|
+
* ```javascript
|
|
18
|
+
* api.derive.session.info((info) => {
|
|
19
|
+
* console.log(`Session info ${JSON.stringify(info)}`);
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function info (instanceId: string, api: DeriveApi): () => Observable<DeriveSessionInfo> {
|
|
24
|
+
return memo(instanceId, (): Observable<DeriveSessionInfo> =>
|
|
25
|
+
api.derive.session.indexes().pipe(
|
|
26
|
+
map((indexes) => {
|
|
27
|
+
const sessionLength = api.consts?.babe?.epochDuration || api.registry.createType('u64', 1);
|
|
28
|
+
const sessionsPerEra = api.consts?.staking?.sessionsPerEra || api.registry.createType('SessionIndex', 1);
|
|
29
|
+
|
|
30
|
+
return objectSpread({
|
|
31
|
+
eraLength: api.registry.createType('BlockNumber', sessionsPerEra.mul(sessionLength)),
|
|
32
|
+
isEpoch: !!api.query.babe,
|
|
33
|
+
sessionLength,
|
|
34
|
+
sessionsPerEra
|
|
35
|
+
}, indexes);
|
|
36
|
+
})
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
}
|