Add storage subscription API endpoints (#126)

* Add storage subscriptions

* StorageResult formatting

* Decoding WIP

* Storage subsciptions in place

* Allow null decodes, ref https://github.com/polkadot-js/apps/issues/215

* e2e tests for subscribeStorage

* Cache subscription results, return with changeset
This commit is contained in:
Jaco Greeff
2018-08-11 20:50:57 +02:00
committed by GitHub
parent 4d815ef726
commit 187c66f600
50 changed files with 463 additions and 278 deletions
+12 -1
View File
@@ -2,18 +2,29 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Param$Types, Param$Type$Array } from '@polkadot/params/types';
import { Storages } from '@polkadot/storage/types';
import { SectionItem, Param$Types, Param$Type$Array } from '@polkadot/params/types';
import { FormattersFunctionMap } from './types';
import typeToString from '@polkadot/params/typeToString';
import isUndefined from '@polkadot/util/is/undefined';
import logger from '@polkadot/util/logger';
import u8aToHex from '@polkadot/util/u8a/toHex';
import createStorageKey from '@polkadot/storage/key';
import echo from './echo';
const l = logger('api-format');
function formatSingleType (formatters: FormattersFunctionMap, type: Param$Types, value: any): any {
if (type === 'StorageKey') {
const [key, ...params]: [SectionItem<Storages>, Array<any>] = value;
return u8aToHex(
createStorageKey(key).apply(null, params)
);
}
const formatter = formatters.get(type);
if (isUndefined(formatter)) {
+8
View File
@@ -5,6 +5,7 @@
import { Params, Param$Types } from '@polkadot/params/types';
import { FormatterFunction } from './types';
import assert from '@polkadot/util/assert';
import addressDecode from '@polkadot/util-keyring/address/decode';
import bytesEncode from '@polkadot/primitives/json/bytes/encode';
import hashEncode from '@polkadot/primitives/json/hash/encode';
@@ -19,6 +20,13 @@ const formatters = new Map<Param$Types, FormatterFunction>([
]);
export default function formatInputs (params: Params, values: Array<any>): Array<any> {
const required = params.filter(({ isOptional }) => !isOptional);
const optionalText = params.length
? ` (${(params.length - required.length) || 'none'} optional)`
: '';
assert(values.length >= required.length && values.length <= params.length, `${params.length || 'no'} params expected${optionalText}, found ${values.length} instead`);
const types = params.map(({ type }) => type);
return format(formatters, types, values);