From 06893d02d854c5b53b6099547edca6126e79a540 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 14 Dec 2018 16:29:16 +0100 Subject: [PATCH] Add chainProperties call to api-observable (#472) --- packages/api-observable/src/Calls.ts | 14 +++++++++++++- packages/types/src/ChainProperties.spec.ts | 22 ++++++++++++++++++++++ packages/types/src/ChainProperties.ts | 8 +++----- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 packages/types/src/ChainProperties.spec.ts diff --git a/packages/api-observable/src/Calls.ts b/packages/api-observable/src/Calls.ts index 037e088eb2..1974d41b37 100644 --- a/packages/api-observable/src/Calls.ts +++ b/packages/api-observable/src/Calls.ts @@ -4,7 +4,7 @@ import { Observable } from 'rxjs'; import { defaultIfEmpty, map } from 'rxjs/operators'; -import { BlockNumber, Extrinsic, ExtrinsicStatus, Hash, Header, SignedBlock } from '@polkadot/types/index'; +import { BlockNumber, ChainProperties, Extrinsic, ExtrinsicStatus, Hash, Header, SignedBlock } from '@polkadot/types/index'; import ApiQueries from './Queries'; @@ -26,6 +26,18 @@ export default class ApiCalls extends ApiQueries { return this._api.system.chain(); } + chainProperties = (): Observable => { + return this._api.system + .properties() + .pipe( + map((properties?: ChainProperties | null): ChainProperties => + properties + ? properties + : new ChainProperties() + ) + ); + } + getBlock = (hash: Uint8Array): Observable => { return this._api.chain.getBlock(hash); } diff --git a/packages/types/src/ChainProperties.spec.ts b/packages/types/src/ChainProperties.spec.ts new file mode 100644 index 0000000000..a23c7033b0 --- /dev/null +++ b/packages/types/src/ChainProperties.spec.ts @@ -0,0 +1,22 @@ +// Copyright 2017-2018 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import ChainProperties from './ChainProperties'; + +describe('ChainProperties', () => { + it('decodes from a null value', () => { + expect( + [...new ChainProperties(null).entries()] + ).toEqual([]); + }); + + it('decodes from an actual object', () => { + expect( + [...new ChainProperties({ + decimals: 15, + tokenSymbol: 'BBQ' + }).entries()] + ).toEqual([['decimals', 15], ['tokenSymbol', 'BBQ']]); + }); +}); diff --git a/packages/types/src/ChainProperties.ts b/packages/types/src/ChainProperties.ts index 2ee1eef149..f1c6b5056e 100644 --- a/packages/types/src/ChainProperties.ts +++ b/packages/types/src/ChainProperties.ts @@ -15,14 +15,12 @@ import { Codec } from './types'; * @noInheritDoc */ export default class ChainProperties extends Map implements Codec { - constructor (value: { [index: string]: any } = {}) { + constructor (value?: { [index: string]: any } | null) { super(ChainProperties.decodeChainProperties(value)); } - private static decodeChainProperties (value: { [index: string]: any }): Array<[string, any]> { - return Object.keys(value).map((key) => ([ - key, value[key] - ] as [string, any])); + private static decodeChainProperties (value?: { [index: string]: any } | null): Array<[string, any]> { + return Object.entries(value || {}); } /**