Add .keyPrefix for storage keys (#1809)

* Add .keyPrefix for storage keys

* Fix hex length
This commit is contained in:
Jaco Greeff
2020-02-05 15:15:49 +01:00
committed by GitHub
parent deecf4853e
commit 2de7a3c130
7 changed files with 18 additions and 10 deletions
+1
View File
@@ -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
+8 -9
View File
@@ -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<AccountId>, [Vec<AccountId>], RewardDestination, Exposure, ValidatorPrefs, Option<Keys>?];
type ResultV2 = [Option<AccountId>, ITuple<[Vec<AccountId>]>, RewardDestination, Exposure, ValidatorPrefs, Option<Keys>];
type Result = [Option<AccountId>, Nominations, RewardDestination, Exposure, ValidatorPrefs, Option<Keys>];
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<Result> {
return api.queryMulti<ResultV2>([
return api.queryMulti<Result>([
[api.query.staking.bonded, stashId],
[api.query.staking.nominators, stashId],
[api.query.staking.payee, stashId],
+1
View File
@@ -193,6 +193,7 @@ export interface DeriveStakingValidators {
export interface DerivedStakingStash {
controllerId?: AccountId;
nominators?: AccountId[];
nominateAt?: EraIndex;
rewardDestination?: RewardDestination;
nextKeys?: Keys;
stakers?: Exposure;
+3
View File
@@ -285,6 +285,9 @@ export default abstract class Decorate<ApiType extends ApiTypes> 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<Codec[]> =>
+1
View File
@@ -29,6 +29,7 @@ export interface StorageEntryBase<ApiType extends ApiTypes, F extends AnyFunctio
entries: () => PromiseOrObs<ApiType, [StorageKey, ObsInnerType<ReturnType<F>>][]>;
hash: (...args: Parameters<F>) => PromiseOrObs<ApiType, Hash>;
key: (...args: Parameters<F>) => string;
keyPrefix: () => string;
size: (...args: Parameters<F>) => PromiseOrObs<ApiType, u64>;
multi: ApiType extends 'rxjs' ? StorageEntryObservableMulti : StorageEntryPromiseMulti;
}
@@ -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;
}
@@ -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;