@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,83 @@
|
|
|
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 { EraIndex } from '@pezkuwi/types/interfaces';
|
|
7
|
+
import type { SpStakingExposure, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@pezkuwi/types/lookup';
|
|
8
|
+
import type { AnyNumber } from '@pezkuwi/types-codec/types';
|
|
9
|
+
import type { DeriveApi, DeriveOwnExposure } from '../types.js';
|
|
10
|
+
|
|
11
|
+
import { combineLatest, map, of } from 'rxjs';
|
|
12
|
+
|
|
13
|
+
import { firstMemo, memo } from '../util/index.js';
|
|
14
|
+
import { erasHistoricApplyAccount } from './util.js';
|
|
15
|
+
|
|
16
|
+
export function _ownExposures (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean, page: u32 | AnyNumber) => Observable<DeriveOwnExposure[]> {
|
|
17
|
+
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean, page: u32 | AnyNumber): Observable<DeriveOwnExposure[]> => {
|
|
18
|
+
const emptyStakingExposure = api.registry.createType<SpStakingExposure>('Exposure');
|
|
19
|
+
// The reason we don't explicitly make the actual types is for compatibility. If the chain doesn't have the noted type it will fail
|
|
20
|
+
// on construction. Therefore we just make an empty option.
|
|
21
|
+
const emptyOptionPage = api.registry.createType<Option<SpStakingExposurePage>>('Option<Null>');
|
|
22
|
+
const emptyOptionMeta = api.registry.createType<Option<SpStakingPagedExposureMetadata>>('Option<Null>');
|
|
23
|
+
|
|
24
|
+
return eras.length
|
|
25
|
+
? combineLatest([
|
|
26
|
+
// Backwards and forward compat for historical integrity when using `erasHistoricApplyAccount`
|
|
27
|
+
api.query.staking.erasStakersClipped
|
|
28
|
+
? combineLatest(eras.map((e) => api.query.staking.erasStakersClipped(e, accountId)))
|
|
29
|
+
: of(eras.map((_) => emptyStakingExposure)),
|
|
30
|
+
api.query.staking.erasStakers
|
|
31
|
+
? combineLatest(eras.map((e) => api.query.staking.erasStakers(e, accountId)))
|
|
32
|
+
: of(eras.map((_) => emptyStakingExposure)),
|
|
33
|
+
api.query.staking.erasStakersPaged
|
|
34
|
+
? combineLatest(eras.map((e) => api.query.staking.erasStakersPaged<Option<SpStakingExposurePage>>(e, accountId, page)))
|
|
35
|
+
: of(eras.map((_) => emptyOptionPage)),
|
|
36
|
+
api.query.staking.erasStakersOverview
|
|
37
|
+
? combineLatest(eras.map((e) => api.query.staking.erasStakersOverview(e, accountId)))
|
|
38
|
+
: of(eras.map((_) => emptyOptionMeta))
|
|
39
|
+
]).pipe(
|
|
40
|
+
map(([clp, exp, paged, expMeta]): DeriveOwnExposure[] =>
|
|
41
|
+
eras.map((era, index) => ({ clipped: clp[index], era, exposure: exp[index], exposureMeta: expMeta[index], exposurePaged: paged[index] }))
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
: of([]);
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @name ownExposure
|
|
51
|
+
* @description Retrieves the staking exposure of a validator for a specific era, including their own stake.
|
|
52
|
+
* @param { Uint8Array | string } accountId The validator stash account.
|
|
53
|
+
* @param {EraIndex} era The staking era to query.
|
|
54
|
+
* @param { u32 | AnyNumber } page? (Optional) The pagination index.
|
|
55
|
+
* @example
|
|
56
|
+
* ```javascript
|
|
57
|
+
* const era = api.createType("EraIndex", 1000);
|
|
58
|
+
* const exposure = await api.derive.staking.ownExposure(
|
|
59
|
+
* "11VR4pF6c7kfBhfmuwwjWY3FodeYBKWx7ix2rsRCU2q6hqJ",
|
|
60
|
+
* era
|
|
61
|
+
* );
|
|
62
|
+
* console.log(JSON.stringify(exposure));
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export const ownExposure = /*#__PURE__*/ firstMemo(
|
|
66
|
+
(api: DeriveApi, accountId: Uint8Array | string, era: EraIndex, page?: u32 | AnyNumber) =>
|
|
67
|
+
api.derive.staking._ownExposures(accountId, [era], true, page || 0)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @name ownExposures
|
|
72
|
+
* @description Retrieves staking exposures for a validator across multiple historical eras.
|
|
73
|
+
* @param { Uint8Array | string } accountId The validator stash account.
|
|
74
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
75
|
+
* @example
|
|
76
|
+
* ```javascript
|
|
77
|
+
* const exposures = await api.derive.staking.ownExposures(
|
|
78
|
+
* ALICE,
|
|
79
|
+
* true
|
|
80
|
+
* );
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export const ownExposures = /*#__PURE__*/ erasHistoricApplyAccount('_ownExposures');
|
|
@@ -0,0 +1,68 @@
|
|
|
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 { BalanceOf, EraIndex, Perbill } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { ITuple } from '@pezkuwi/types/types';
|
|
7
|
+
import type { DeriveApi, DeriveStakerSlashes } from '../types.js';
|
|
8
|
+
|
|
9
|
+
import { combineLatest, map, of } from 'rxjs';
|
|
10
|
+
|
|
11
|
+
import { firstMemo, memo } from '../util/index.js';
|
|
12
|
+
import { erasHistoricApplyAccount } from './util.js';
|
|
13
|
+
|
|
14
|
+
export function _ownSlashes (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerSlashes[]> {
|
|
15
|
+
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable<DeriveStakerSlashes[]> =>
|
|
16
|
+
eras.length
|
|
17
|
+
? combineLatest([
|
|
18
|
+
combineLatest(eras.map((e) => api.query.staking.validatorSlashInEra(e, accountId))),
|
|
19
|
+
combineLatest(eras.map((e) => api.query.staking.nominatorSlashInEra(e, accountId)))
|
|
20
|
+
]).pipe(
|
|
21
|
+
map(([vals, noms]): DeriveStakerSlashes[] =>
|
|
22
|
+
eras.map((era, index) => ({
|
|
23
|
+
era,
|
|
24
|
+
total: vals[index].isSome
|
|
25
|
+
? (vals[index].unwrap() as ITuple<[Perbill, BalanceOf]>)[1]
|
|
26
|
+
: (noms[index].unwrapOrDefault() as BalanceOf)
|
|
27
|
+
}))
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
: of([])
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @name ownSlash
|
|
36
|
+
* @description Retrieves the slashes applied to a specific account in a given era.
|
|
37
|
+
* @param { Uint8Array | string } accountId The validator stash account.
|
|
38
|
+
* @param {EraIndex} era The staking era to query.
|
|
39
|
+
* @example
|
|
40
|
+
* ```javascript
|
|
41
|
+
* const era = api.createType("EraIndex", 1000);
|
|
42
|
+
* const slashedAmount = await api.derive.staking.ownSlash(
|
|
43
|
+
* ALICE,
|
|
44
|
+
* era
|
|
45
|
+
* );
|
|
46
|
+
* console.log(`Era: ${slashedAmount.era}, total ${slashedAmount.total}`);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const ownSlash = /*#__PURE__*/ firstMemo(
|
|
50
|
+
(api: DeriveApi, accountId: Uint8Array | string, era: EraIndex) =>
|
|
51
|
+
api.derive.staking._ownSlashes(accountId, [era], true)
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @name ownSlashes
|
|
56
|
+
* @description Retrieves the slashes for a specific account across all historic eras.
|
|
57
|
+
* @param { Uint8Array | string } accountId The validator stash account.
|
|
58
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
59
|
+
* @example
|
|
60
|
+
* ```javascript
|
|
61
|
+
* const slashes = await api.derive.staking.ownSlashes(
|
|
62
|
+
* ALICE,
|
|
63
|
+
* true
|
|
64
|
+
* );
|
|
65
|
+
* console.log(slashes);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export const ownSlashes = /*#__PURE__*/ erasHistoricApplyAccount('_ownSlashes');
|
|
@@ -0,0 +1,230 @@
|
|
|
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, StorageKey, u32, Vec } from '@pezkuwi/types';
|
|
6
|
+
import type { AccountId, EraIndex } from '@pezkuwi/types/interfaces';
|
|
7
|
+
import type { PalletStakingNominations, PalletStakingRewardDestination, PalletStakingStakingLedger, PalletStakingValidatorPrefs, SpStakingExposure, SpStakingExposurePage, SpStakingPagedExposureMetadata } from '@pezkuwi/types/lookup';
|
|
8
|
+
import type { AnyNumber } from '@pezkuwi/types-codec/types';
|
|
9
|
+
import type { DeriveApi, DeriveStakingQuery, StakingQueryFlags } from '../types.js';
|
|
10
|
+
|
|
11
|
+
import { combineLatest, map, of, switchMap } from 'rxjs';
|
|
12
|
+
|
|
13
|
+
import { firstMemo, memo } from '../util/index.js';
|
|
14
|
+
|
|
15
|
+
// handle compatibility between generations of structures
|
|
16
|
+
function rewardDestinationCompat (rewardDestination: PalletStakingRewardDestination | Option<PalletStakingRewardDestination>): PalletStakingRewardDestination | null {
|
|
17
|
+
// We ensure the type is an Option by checking if isSome is a boolean. When isSome doesn't exist it will always return undefined.
|
|
18
|
+
return typeof (rewardDestination as Option<PalletStakingRewardDestination>).isSome === 'boolean'
|
|
19
|
+
? (rewardDestination as Option<PalletStakingRewardDestination>).unwrapOr(null)
|
|
20
|
+
: (rewardDestination as PalletStakingRewardDestination);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function filterClaimedRewards (api: DeriveApi, cl: number[]): Vec<u32> {
|
|
24
|
+
return api.registry.createType('Vec<u32>', cl.filter((c) => c !== -1));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function filterRewards (stashIds: AccountId[], eras: number[], claimedRewards: [StorageKey<[u32, AccountId]>, Vec<u32>][], stakersOverview: [StorageKey<[u32, AccountId]>, Option<SpStakingPagedExposureMetadata>][]): number[][] {
|
|
28
|
+
const claimedData: Record<string, Map<number, u32[]>> = {};
|
|
29
|
+
const overviewData: Record<string, Map<number, u32>> = {};
|
|
30
|
+
const ids = stashIds.map((i) => i.toString());
|
|
31
|
+
|
|
32
|
+
claimedRewards.forEach(([keys, rewards]) => {
|
|
33
|
+
const id = keys.args[1].toString();
|
|
34
|
+
const era = keys.args[0].toNumber();
|
|
35
|
+
|
|
36
|
+
if (ids.includes(id)) {
|
|
37
|
+
if (claimedData[id]) {
|
|
38
|
+
claimedData[id].set(era, rewards.toArray());
|
|
39
|
+
} else {
|
|
40
|
+
claimedData[id] = new Map();
|
|
41
|
+
claimedData[id].set(era, rewards.toArray());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
stakersOverview.forEach(([keys, overview]) => {
|
|
47
|
+
const id = keys.args[1].toString();
|
|
48
|
+
const era = keys.args[0].toNumber();
|
|
49
|
+
|
|
50
|
+
if (ids.includes(id) && overview.isSome) {
|
|
51
|
+
if (overviewData[id]) {
|
|
52
|
+
overviewData[id].set(era, overview.unwrap().pageCount);
|
|
53
|
+
} else {
|
|
54
|
+
overviewData[id] = new Map();
|
|
55
|
+
overviewData[id].set(era, overview.unwrap().pageCount);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return stashIds.map((id) => {
|
|
61
|
+
const rewardsPerEra = claimedData[id.toString()];
|
|
62
|
+
const overviewPerEra = overviewData[id.toString()];
|
|
63
|
+
|
|
64
|
+
return eras.map((era) => {
|
|
65
|
+
if (rewardsPerEra && rewardsPerEra.has(era) && overviewPerEra && overviewPerEra.has(era)) {
|
|
66
|
+
const rewards = rewardsPerEra.get(era) as unknown as u32[];
|
|
67
|
+
const pageCount = overviewPerEra.get(era) as unknown as u32;
|
|
68
|
+
|
|
69
|
+
return rewards.length === pageCount.toNumber()
|
|
70
|
+
? era
|
|
71
|
+
: -1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return -1;
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseDetails (api: DeriveApi, stashId: AccountId, controllerIdOpt: Option<AccountId> | null, nominatorsOpt: Option<PalletStakingNominations>, rewardDestinationOpts: Option<PalletStakingRewardDestination> | PalletStakingRewardDestination, validatorPrefs: PalletStakingValidatorPrefs, exposure: Option<SpStakingExposurePage>, stakingLedgerOpt: Option<PalletStakingStakingLedger>, exposureMeta: Option<SpStakingPagedExposureMetadata>, claimedRewards: number[], exposureEraStakers: SpStakingExposure): DeriveStakingQuery {
|
|
80
|
+
return {
|
|
81
|
+
accountId: stashId,
|
|
82
|
+
claimedRewardsEras: filterClaimedRewards(api, claimedRewards),
|
|
83
|
+
controllerId: controllerIdOpt?.unwrapOr(null) || null,
|
|
84
|
+
exposureEraStakers,
|
|
85
|
+
exposureMeta,
|
|
86
|
+
exposurePaged: exposure,
|
|
87
|
+
nominators: nominatorsOpt.isSome
|
|
88
|
+
? nominatorsOpt.unwrap().targets
|
|
89
|
+
: [],
|
|
90
|
+
rewardDestination: rewardDestinationCompat(rewardDestinationOpts),
|
|
91
|
+
stakingLedger: stakingLedgerOpt.unwrapOrDefault(),
|
|
92
|
+
stashId,
|
|
93
|
+
validatorPrefs
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getLedgers (api: DeriveApi, optIds: (Option<AccountId> | null)[], { withLedger = false }: StakingQueryFlags): Observable<Option<PalletStakingStakingLedger>[]> {
|
|
98
|
+
const ids = optIds
|
|
99
|
+
.filter((o): o is Option<AccountId> => withLedger && !!o && o.isSome)
|
|
100
|
+
.map((o) => o.unwrap());
|
|
101
|
+
const emptyLed = api.registry.createType<Option<PalletStakingStakingLedger>>('Option<StakingLedger>');
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
ids.length
|
|
105
|
+
? combineLatest(ids.map((s) => api.query.staking.ledger(s)))
|
|
106
|
+
: of([])
|
|
107
|
+
).pipe(
|
|
108
|
+
map((optLedgers): Option<PalletStakingStakingLedger>[] => {
|
|
109
|
+
let offset = -1;
|
|
110
|
+
|
|
111
|
+
return optIds.map((o): Option<PalletStakingStakingLedger> =>
|
|
112
|
+
o && o.isSome
|
|
113
|
+
? optLedgers[++offset] || emptyLed
|
|
114
|
+
: emptyLed
|
|
115
|
+
);
|
|
116
|
+
})
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraIndex, { withClaimedRewardsEras, withController, withDestination, withExposure, withExposureErasStakersLegacy, withExposureMeta, withLedger, withNominations, withPrefs }: StakingQueryFlags, page: u32 | AnyNumber): Observable<[(Option<AccountId> | null)[], Option<PalletStakingNominations>[], Option<PalletStakingRewardDestination>[], PalletStakingValidatorPrefs[], Option<SpStakingExposurePage>[], Option<SpStakingPagedExposureMetadata>[], number[][], SpStakingExposure[]]> {
|
|
121
|
+
const emptyNoms = api.registry.createType<Option<PalletStakingNominations>>('Option<Nominations>');
|
|
122
|
+
const emptyRewa = api.registry.createType<Option<PalletStakingRewardDestination>>('RewardDestination');
|
|
123
|
+
const emptyExpoEraStakers = api.registry.createType<SpStakingExposure>('Exposure');
|
|
124
|
+
const emptyPrefs = api.registry.createType<PalletStakingValidatorPrefs>('ValidatorPrefs');
|
|
125
|
+
// The reason we don't explicitly make the actual types is for compatibility. If the chain doesn't have the noted type it will fail
|
|
126
|
+
// on construction. Therefore we just make an empty option.
|
|
127
|
+
const emptyExpo = api.registry.createType<Option<SpStakingExposurePage>>('Option<Null>');
|
|
128
|
+
const emptyExpoMeta = api.registry.createType<Option<SpStakingPagedExposureMetadata>>('Option<Null>');
|
|
129
|
+
const emptyClaimedRewards = [-1];
|
|
130
|
+
|
|
131
|
+
const depth = Number(api.consts.staking.historyDepth.toNumber());
|
|
132
|
+
const eras = new Array(depth).fill(0).map((_, idx) => {
|
|
133
|
+
if (idx === 0) {
|
|
134
|
+
return activeEra.toNumber() - 1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return activeEra.toNumber() - idx - 1;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return combineLatest([
|
|
141
|
+
withController || withLedger
|
|
142
|
+
? combineLatest(stashIds.map((s) => api.query.staking.bonded(s)))
|
|
143
|
+
: of(stashIds.map(() => null)),
|
|
144
|
+
withNominations
|
|
145
|
+
? combineLatest(stashIds.map((s) => api.query.staking.nominators(s)))
|
|
146
|
+
: of(stashIds.map(() => emptyNoms)),
|
|
147
|
+
withDestination
|
|
148
|
+
? combineLatest(stashIds.map((s) => api.query.staking.payee(s)))
|
|
149
|
+
: of(stashIds.map(() => emptyRewa)),
|
|
150
|
+
withPrefs
|
|
151
|
+
? combineLatest(stashIds.map((s) => api.query.staking.validators(s)))
|
|
152
|
+
: of(stashIds.map(() => emptyPrefs)),
|
|
153
|
+
withExposure && api.query.staking.erasStakersPaged
|
|
154
|
+
? combineLatest(stashIds.map((s) => api.query.staking.erasStakersPaged<Option<SpStakingExposurePage>>(activeEra, s, page)))
|
|
155
|
+
: of(stashIds.map(() => emptyExpo)),
|
|
156
|
+
withExposureMeta && api.query.staking.erasStakersOverview
|
|
157
|
+
? combineLatest(stashIds.map((s) => api.query.staking.erasStakersOverview(activeEra, s)))
|
|
158
|
+
: of(stashIds.map(() => emptyExpoMeta)),
|
|
159
|
+
withClaimedRewardsEras && api.query.staking.claimedRewards
|
|
160
|
+
? combineLatest([
|
|
161
|
+
api.query.staking.claimedRewards.entries<Vec<u32>>(),
|
|
162
|
+
api.query.staking.erasStakersOverview.entries<Option<SpStakingPagedExposureMetadata>>()
|
|
163
|
+
]).pipe(
|
|
164
|
+
map(([rewardsStorageVec, overviewStorageVec]) => filterRewards(stashIds, eras, rewardsStorageVec, overviewStorageVec))
|
|
165
|
+
)
|
|
166
|
+
: of(stashIds.map(() => emptyClaimedRewards)),
|
|
167
|
+
withExposureErasStakersLegacy && api.query.staking.erasStakers
|
|
168
|
+
? combineLatest(stashIds.map((s) => api.query.staking.erasStakers(activeEra, s)))
|
|
169
|
+
: of(stashIds.map(() => emptyExpoEraStakers))
|
|
170
|
+
]);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function getBatch (api: DeriveApi, activeEra: EraIndex, stashIds: AccountId[], flags: StakingQueryFlags, page: u32 | AnyNumber): Observable<DeriveStakingQuery[]> {
|
|
174
|
+
return getStashInfo(api, stashIds, activeEra, flags, page).pipe(
|
|
175
|
+
switchMap(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, exposure, exposureMeta, claimedRewardsEras, exposureEraStakers]): Observable<DeriveStakingQuery[]> =>
|
|
176
|
+
getLedgers(api, controllerIdOpt, flags).pipe(
|
|
177
|
+
map((stakingLedgerOpts) =>
|
|
178
|
+
stashIds.map((stashId, index) =>
|
|
179
|
+
parseDetails(api, stashId, controllerIdOpt[index], nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], exposure[index], stakingLedgerOpts[index], exposureMeta[index], claimedRewardsEras[index], exposureEraStakers[index])
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @name query
|
|
189
|
+
* @param { Uint8Array | string } accountId The stash account to query.
|
|
190
|
+
* @param { StakingQueryFlags } flags Flags to customize the query.
|
|
191
|
+
* @param { u32 } page (Optional) pagination parameter.
|
|
192
|
+
* @description Retrieves staking details for a given stash account.
|
|
193
|
+
* @example
|
|
194
|
+
* ```javascript
|
|
195
|
+
* const stakingInfo = await api.derive.staking.query(
|
|
196
|
+
* ALICE,
|
|
197
|
+
* {}
|
|
198
|
+
* );
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export const query = /*#__PURE__*/ firstMemo(
|
|
202
|
+
(api: DeriveApi, accountId: Uint8Array | string, flags: StakingQueryFlags, page?: u32) =>
|
|
203
|
+
api.derive.staking.queryMulti([accountId], flags, page)
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @name queryMulti
|
|
208
|
+
* @param { (Uint8Array | string)[] } accountIds List of stash accounts to query.
|
|
209
|
+
* @param { StakingQueryFlags } flags Flags to customize the query.
|
|
210
|
+
* @param { u32 } page (Optional) pagination parameter.
|
|
211
|
+
* @description Retrieves staking details for multiple stash accounts.
|
|
212
|
+
* @example
|
|
213
|
+
* ```javascript
|
|
214
|
+
* const stakingInfos = await api.derive.staking.queryMulti([stashId1, stashId2], {});
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
export function queryMulti (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | AnyNumber) => Observable<DeriveStakingQuery[]> {
|
|
218
|
+
return memo(instanceId, (accountIds: (Uint8Array | string)[], flags: StakingQueryFlags, page?: u32 | AnyNumber): Observable<DeriveStakingQuery[]> =>
|
|
219
|
+
api.derive.session.indexes().pipe(
|
|
220
|
+
switchMap(({ activeEra }): Observable<DeriveStakingQuery[]> => {
|
|
221
|
+
const stashIds = accountIds.map((a) => api.registry.createType('AccountId', a));
|
|
222
|
+
const p = page || 0;
|
|
223
|
+
|
|
224
|
+
return stashIds.length
|
|
225
|
+
? getBatch(api, activeEra, stashIds, flags, p)
|
|
226
|
+
: of([]);
|
|
227
|
+
})
|
|
228
|
+
)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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 { EraIndex } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { DeriveApi } from '../types.js';
|
|
7
|
+
import type { DeriveEraValidatorExposurePaged, DeriveStakerExposure } from './types.js';
|
|
8
|
+
|
|
9
|
+
import { map, switchMap } from 'rxjs';
|
|
10
|
+
|
|
11
|
+
import { firstMemo, memo } from '../util/index.js';
|
|
12
|
+
|
|
13
|
+
export function _stakerExposures (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive?: boolean) => Observable<DeriveStakerExposure[][]> {
|
|
14
|
+
return memo(instanceId, (accountIds: (Uint8Array | string)[], eras: EraIndex[], withActive = false): Observable<DeriveStakerExposure[][]> => {
|
|
15
|
+
const stakerIds = accountIds.map((a) => api.registry.createType('AccountId', a).toString());
|
|
16
|
+
|
|
17
|
+
return api.derive.staking._erasExposure(eras, withActive).pipe(
|
|
18
|
+
map((exposures): DeriveStakerExposure[][] =>
|
|
19
|
+
stakerIds.map((stakerId) =>
|
|
20
|
+
exposures.map(({ era, nominators: allNominators, validators: allValidators }): DeriveStakerExposure => {
|
|
21
|
+
const isValidator = !!allValidators[stakerId];
|
|
22
|
+
const validators: DeriveEraValidatorExposurePaged = {};
|
|
23
|
+
const nominating = allNominators[stakerId] || [];
|
|
24
|
+
|
|
25
|
+
if (isValidator) {
|
|
26
|
+
validators[stakerId] = allValidators[stakerId];
|
|
27
|
+
} else if (nominating) {
|
|
28
|
+
nominating.forEach(({ validatorId }): void => {
|
|
29
|
+
validators[validatorId] = allValidators[validatorId];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { era, isEmpty: !Object.keys(validators).length, isValidator, nominating, validators };
|
|
34
|
+
})
|
|
35
|
+
)
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @name stakerExposures
|
|
43
|
+
* @param { (Uint8Array | string)[] } accountIds List of validator stash accounts.
|
|
44
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
45
|
+
* @description Retrieves staking exposure for multiple accounts across historical eras.
|
|
46
|
+
* @example
|
|
47
|
+
* ```javascript
|
|
48
|
+
* const exposure = await api.derive.staking.stakerExposures(
|
|
49
|
+
* [ALICE, BOB],
|
|
50
|
+
* true
|
|
51
|
+
* );
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function stakerExposures (instanceId: string, api: DeriveApi): (accountIds: (Uint8Array | string)[], withActive?: boolean) => Observable<DeriveStakerExposure[][]> {
|
|
55
|
+
return memo(instanceId, (accountIds: (Uint8Array | string)[], withActive = false): Observable<DeriveStakerExposure[][]> =>
|
|
56
|
+
api.derive.staking.erasHistoric(withActive).pipe(
|
|
57
|
+
switchMap((eras) => api.derive.staking._stakerExposures(accountIds, eras, withActive))
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @name stakerExposure
|
|
64
|
+
* @param { Uint8Array | string } accountId The validator stash account.
|
|
65
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
66
|
+
* @description Retrieves staking exposure for a single account across historical eras. Exposure refers to the total stake associated with a validator.
|
|
67
|
+
* @example
|
|
68
|
+
* ```javascript
|
|
69
|
+
* const exposure = await api.derive.staking.stakerExposure(
|
|
70
|
+
* ALICE,
|
|
71
|
+
* true
|
|
72
|
+
* );
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export const stakerExposure = /*#__PURE__*/ firstMemo(
|
|
76
|
+
(api: DeriveApi, accountId: Uint8Array | string, withActive?: boolean) =>
|
|
77
|
+
api.derive.staking.stakerExposures([accountId], withActive)
|
|
78
|
+
);
|
|
@@ -0,0 +1,46 @@
|
|
|
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 { EraIndex } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { DeriveApi, DeriveStakerPoints } from '../types.js';
|
|
7
|
+
|
|
8
|
+
import { map } from 'rxjs';
|
|
9
|
+
|
|
10
|
+
import { memo } from '../util/index.js';
|
|
11
|
+
import { erasHistoricApplyAccount } from './util.js';
|
|
12
|
+
|
|
13
|
+
export function _stakerPoints (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerPoints[]> {
|
|
14
|
+
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable<DeriveStakerPoints[]> => {
|
|
15
|
+
const stakerId = api.registry.createType('AccountId', accountId).toString();
|
|
16
|
+
|
|
17
|
+
return api.derive.staking._erasPoints(eras, withActive).pipe(
|
|
18
|
+
map((points): DeriveStakerPoints[] =>
|
|
19
|
+
points.map(({ era, eraPoints, validators }): DeriveStakerPoints => ({
|
|
20
|
+
era,
|
|
21
|
+
eraPoints,
|
|
22
|
+
points: validators[stakerId] || api.registry.createType('RewardPoint')
|
|
23
|
+
}))
|
|
24
|
+
)
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @name stakerPoints
|
|
31
|
+
* @param { Uint8Array | string } accountId The stakers AccountId.
|
|
32
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
33
|
+
* @description Retrieves the era reward points earned by a given staker across all eras.
|
|
34
|
+
* @example
|
|
35
|
+
* ```javascript
|
|
36
|
+
* const points = await api.derive.staking.stakerPoints(
|
|
37
|
+
* ALICE, //Alice accountId
|
|
38
|
+
* false
|
|
39
|
+
* );
|
|
40
|
+
* console.log(
|
|
41
|
+
* 'Validator Era Points:',
|
|
42
|
+
* points.map(({ era, points }) => `Era ${era}: ${points.toString()} points`)
|
|
43
|
+
* );
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export const stakerPoints = /*#__PURE__*/ erasHistoricApplyAccount('_stakerPoints');
|
|
@@ -0,0 +1,45 @@
|
|
|
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 { EraIndex } from '@pezkuwi/types/interfaces';
|
|
6
|
+
import type { DeriveApi, DeriveStakerPrefs } from '../types.js';
|
|
7
|
+
|
|
8
|
+
import { map } from 'rxjs';
|
|
9
|
+
|
|
10
|
+
import { memo } from '../util/index.js';
|
|
11
|
+
import { erasHistoricApplyAccount } from './util.js';
|
|
12
|
+
|
|
13
|
+
export function _stakerPrefs (instanceId: string, api: DeriveApi): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerPrefs[]> {
|
|
14
|
+
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], _withActive: boolean): Observable<DeriveStakerPrefs[]> =>
|
|
15
|
+
api.query.staking.erasValidatorPrefs.multi(eras.map((e) => [e, accountId])).pipe(
|
|
16
|
+
map((all): DeriveStakerPrefs[] =>
|
|
17
|
+
all.map((validatorPrefs, index): DeriveStakerPrefs => ({
|
|
18
|
+
era: eras[index],
|
|
19
|
+
validatorPrefs
|
|
20
|
+
}))
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @name stakerPrefs
|
|
28
|
+
* @param { Uint8Array | string } accountId The stakers AccountId.
|
|
29
|
+
* @param { boolean } withActive Whether to include the active era.
|
|
30
|
+
* @description Retrieves the validator preferences for a given staker across historical eras.
|
|
31
|
+
* @example
|
|
32
|
+
* ```javascript
|
|
33
|
+
* const prefs = await api.derive.staking.stakerPrefs(
|
|
34
|
+
* ALICE, //Alice accountId
|
|
35
|
+
* false
|
|
36
|
+
* );
|
|
37
|
+
* console.log(
|
|
38
|
+
* 'Validator Preferences:',
|
|
39
|
+
* prefs.map(
|
|
40
|
+
* ({ era, validatorPrefs }) => `Era ${era}: Commission ${validatorPrefs.commission.toString()}`
|
|
41
|
+
* )
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export const stakerPrefs = /*#__PURE__*/ erasHistoricApplyAccount('_stakerPrefs');
|