diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c198d4ec3..4d4169f9d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add typings for `api.tx.*.*` - the `polkadot-types-from-chain` now also generates these - Remove additional V1-only derives `(staking.recentlyOffline`, `balances.nominatorsFor`) - Add `.isMax()` on int values, an opposite of `isZero()` (e.g. `u128`, as well as derived, e.g. `Balances`) +- Add `.keyPrefix()` to all `api.query.*` complimenting the `.key()` call, both returning a hex string # 1.0.1 diff --git a/packages/api-derive/src/staking/infoStash.ts b/packages/api-derive/src/staking/infoStash.ts index a9e19ea124..561badb9e8 100644 --- a/packages/api-derive/src/staking/infoStash.ts +++ b/packages/api-derive/src/staking/infoStash.ts @@ -3,24 +3,23 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ApiInterfaceRx } from '@polkadot/api/types'; -import { AccountId, Exposure, Keys, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces'; -import { ITuple } from '@polkadot/types/types'; +import { AccountId, Exposure, Keys, Nominations, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces'; import { DerivedStakingStash } from '../types'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -import { Option, Vec } from '@polkadot/types'; +import { Option } from '@polkadot/types'; import { memo } from '../util'; -type Result = [Option, [Vec], RewardDestination, Exposure, ValidatorPrefs, Option?]; -type ResultV2 = [Option, ITuple<[Vec]>, RewardDestination, Exposure, ValidatorPrefs, Option]; +type Result = [Option, Nominations, RewardDestination, Exposure, ValidatorPrefs, Option]; -function parse (stashId: AccountId, [_controllerId, [nominators], rewardDestination, stakers, validatorPrefs, _nextKeys]: Result): DerivedStakingStash { +function parse (stashId: AccountId, [_controllerId, nominators, rewardDestination, stakers, validatorPrefs, _nextKeys]: Result): DerivedStakingStash { return { controllerId: _controllerId.unwrapOr(undefined), - nextKeys: _nextKeys?.unwrapOr(undefined), - nominators, + nextKeys: _nextKeys.unwrapOr(undefined), + nominators: nominators.targets, + nominateAt: nominators.submittedIn, rewardDestination, stakers, stashId, @@ -29,7 +28,7 @@ function parse (stashId: AccountId, [_controllerId, [nominators], rewardDestinat } function query (api: ApiInterfaceRx, stashId: AccountId): Observable { - return api.queryMulti([ + return api.queryMulti([ [api.query.staking.bonded, stashId], [api.query.staking.nominators, stashId], [api.query.staking.payee, stashId], diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 5f7725fc53..2b119c1e75 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -193,6 +193,7 @@ export interface DeriveStakingValidators { export interface DerivedStakingStash { controllerId?: AccountId; nominators?: AccountId[]; + nominateAt?: EraIndex; rewardDestination?: RewardDestination; nextKeys?: Keys; stakers?: Exposure; diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index f4631043cb..6803b36703 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -285,6 +285,9 @@ export default abstract class Decorate extends Events decorated.key = (arg1?: Arg, arg2?: Arg): string => u8aToHex(compactStripLength(creator(creator.meta.type.isDoubleMap ? [arg1, arg2] : arg1))[1]); + decorated.keyPrefix = (): string => + u8aToHex(creator.keyPrefix); + if (this.hasSubscriptions) { // When using double map storage function, user need to pass double map key as an array decorated.multi = decorateMethod((args: (Arg | Arg[])[]): Observable => diff --git a/packages/api/src/types/storage.ts b/packages/api/src/types/storage.ts index 386428da73..8cccdb14bd 100644 --- a/packages/api/src/types/storage.ts +++ b/packages/api/src/types/storage.ts @@ -29,6 +29,7 @@ export interface StorageEntryBase PromiseOrObs>][]>; hash: (...args: Parameters) => PromiseOrObs; key: (...args: Parameters) => string; + keyPrefix: () => string; size: (...args: Parameters) => PromiseOrObs; multi: ApiType extends 'rxjs' ? StorageEntryObservableMulti : StorageEntryPromiseMulti; } diff --git a/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts b/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts index dafc867f39..1662df77f9 100644 --- a/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts +++ b/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts @@ -8,7 +8,7 @@ import { Codec, Registry } from '@polkadot/types/types'; import BN from 'bn.js'; import { Compact, Raw, createType, createTypeUnsafe } from '@polkadot/types/codec'; import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey'; -import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util'; +import { assert, compactStripLength, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util'; import { xxhashAsU8a } from '@polkadot/util-crypto'; import getHasher, { HasherFunction } from './getHasher'; @@ -218,5 +218,7 @@ export default function createFunction (registry: Registry, itemFn: CreateItemFn extendDoubleMap(registry, itemFn, storageFn); } + storageFn.keyPrefix = storageFn.iterKey || compactStripLength(storageFn())[1]; + return storageFn; } diff --git a/packages/types/src/primitive/StorageKey.ts b/packages/types/src/primitive/StorageKey.ts index 8ed886055e..67fd9005e9 100644 --- a/packages/types/src/primitive/StorageKey.ts +++ b/packages/types/src/primitive/StorageKey.ts @@ -12,6 +12,7 @@ import Bytes from './Bytes'; export interface StorageEntry { (arg?: any): Uint8Array; iterKey?: Uint8Array & Codec; + keyPrefix: Uint8Array; meta: StorageEntryMetadataLatest; method: string; prefix: string;