From 2e83d92adc6e87dd9dfdf9f76cad6e9f0ee65806 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 27 Mar 2020 20:49:18 +0100 Subject: [PATCH] Derive for deomcracy nextExternal (#2108) --- CHANGELOG.md | 4 +- packages/api-derive/src/democracy/index.ts | 2 + .../api-derive/src/democracy/nextExternal.ts | 40 +++++++++++++++++++ packages/api-derive/src/democracy/preimage.ts | 25 ++++++++++++ packages/api-derive/src/democracy/types.ts | 8 +++- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 packages/api-derive/src/democracy/nextExternal.ts create mode 100644 packages/api-derive/src/democracy/preimage.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e819e7ae4b..75d1cf048c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # 1.9.0-beta.x +**Breaking change** (TypeScript with derives only) The type names for `api.derive.*` returns has been adjusted to be consistent with `Derive*` (previously a mixture of `Derived*` and `Derive*`) +**Breaking change** (derives only) Additional cleanups of democracy proposeal & referenda derives, including support for new Polkadot/Substrate vote retrievals - Tx signing via `signAndSend` & `signAsync` now tries to use the last finalized block (depending on finalized drift) - Adjust `Error.message` on RPC failures to not include the (already known) method signature -- Additional cleanups and rework of democracy derives, including support for new Polkadot/Substrate vote retrievals - Internal adjustment of interface types to align with modules (no external impact via `@polkadot/types/interfaces`) +- Extended `api.derive.democracy` to cater for dispatchQueue and externals # 1.8.1 Mar 22, 2020 diff --git a/packages/api-derive/src/democracy/index.ts b/packages/api-derive/src/democracy/index.ts index b2c7536eaf..d79bc68242 100644 --- a/packages/api-derive/src/democracy/index.ts +++ b/packages/api-derive/src/democracy/index.ts @@ -6,6 +6,8 @@ export * from './_referendumsVotes'; export * from './_referendumVotes'; export * from './_referendumInfo'; export * from './dispatchQueue'; +export * from './nextExternal'; +export * from './preimage'; export * from './preimages'; export * from './proposals'; export * from './referendums'; diff --git a/packages/api-derive/src/democracy/nextExternal.ts b/packages/api-derive/src/democracy/nextExternal.ts new file mode 100644 index 0000000000..708dfb4695 --- /dev/null +++ b/packages/api-derive/src/democracy/nextExternal.ts @@ -0,0 +1,40 @@ +// Copyright 2017-2020 @polkadot/api-derive 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 { Hash, VoteThreshold } from '@polkadot/types/interfaces'; +import { ITuple } from '@polkadot/types/types'; +import { DeriveProposalExternal } from '../types'; + +import { Observable, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { Option } from '@polkadot/types'; + +import { memo } from '../util'; + +function withImage (api: ApiInterfaceRx, nextOpt: Option>): Observable { + if (nextOpt.isNone) { + return of(null); + } + + const [imageHash, threshold] = nextOpt.unwrap(); + + return api.derive.democracy.preimage(imageHash).pipe( + map((image): DeriveProposalExternal => ({ + image, + imageHash, + threshold + })) + ); +} + +export function nextExternal (api: ApiInterfaceRx): () => Observable { + return memo((): Observable => + api.query.democracy?.nextExternal + ? api.query.democracy.nextExternal>>().pipe( + switchMap((nextOpt) => withImage(api, nextOpt)) + ) + : of(null) + ); +} diff --git a/packages/api-derive/src/democracy/preimage.ts b/packages/api-derive/src/democracy/preimage.ts new file mode 100644 index 0000000000..56c3d0c6c0 --- /dev/null +++ b/packages/api-derive/src/democracy/preimage.ts @@ -0,0 +1,25 @@ +// Copyright 2017-2020 @polkadot/api-derive 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 { AccountId, Balance, BlockNumber, Hash } from '@polkadot/types/interfaces'; +import { ITuple } from '@polkadot/types/types'; +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { DeriveProposalImage } from '../types'; + +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { Bytes, Option } from '@polkadot/types'; + +import { memo } from '../util'; +import { parseImage } from './util'; + +type PreImage = Option>; + +export function preimage (api: ApiInterfaceRx): (hash: Hash) => Observable { + return memo((hashes: Hash[]): Observable => + api.query.democracy.preimages(hashes).pipe( + map((imageOpt) => parseImage(api, imageOpt)) + ) + ); +} diff --git a/packages/api-derive/src/democracy/types.ts b/packages/api-derive/src/democracy/types.ts index 9d96f74049..bf38e3c213 100644 --- a/packages/api-derive/src/democracy/types.ts +++ b/packages/api-derive/src/democracy/types.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, Balance, BlockNumber, Hash, Proposal, PropIndex, ReferendumInfoTo239, ReferendumStatus, Vote, ReferendumIndex } from '@polkadot/types/interfaces'; +import { AccountId, Balance, BlockNumber, Hash, Proposal, PropIndex, ReferendumInfoTo239, ReferendumStatus, Vote, ReferendumIndex, VoteThreshold } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { Vec } from '@polkadot/types'; @@ -30,6 +30,12 @@ export interface DeriveProposal { seconds: Vec; } +export interface DeriveProposalExternal { + image?: DeriveProposalImage; + imageHash: Hash; + threshold: VoteThreshold; +} + export interface DeriveReferendum { index: ReferendumIndex; image?: DeriveProposalImage;