From bee7db805a6e0fad32059a884333069f3b448241 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 20 Oct 2022 12:33:52 +0300 Subject: [PATCH] Retrieve preimages from new pallet (#5281) --- .../api-derive/src/democracy/preimages.ts | 67 ++++++++++++++++--- packages/api-derive/src/democracy/types.ts | 4 +- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/packages/api-derive/src/democracy/preimages.ts b/packages/api-derive/src/democracy/preimages.ts index eb5d61b58b..df66c80d34 100644 --- a/packages/api-derive/src/democracy/preimages.ts +++ b/packages/api-derive/src/democracy/preimages.ts @@ -2,17 +2,19 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { AccountId, Balance, BlockNumber, Hash, PreimageStatus, Proposal } from '@polkadot/types/interfaces'; -import type { FrameSupportPreimagesBounded } from '@polkadot/types/lookup'; +import type { AccountId, Balance, BlockNumber, Call, Hash, PreimageStatus } from '@polkadot/types/interfaces'; +import type { FrameSupportPreimagesBounded, PalletPreimageRequestStatus } from '@polkadot/types/lookup'; import type { Bytes, Option } from '@polkadot/types-codec'; import type { ITuple } from '@polkadot/types-codec/types'; +import type { HexString } from '@polkadot/util/types'; import type { DeriveApi, DeriveProposalImage } from '../types'; -import { map, of } from 'rxjs'; +import { map, of, switchMap } from 'rxjs'; -import { isFunction } from '@polkadot/util'; +import { BN_ZERO, isFunction } from '@polkadot/util'; import { firstMemo, memo } from '../util'; +import { getImageHashBounded } from './util'; type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber]; type OldPreimage = ITuple; @@ -22,7 +24,7 @@ function isDemocracyPreimage (api: DeriveApi, imageOpt: Option | Op } function constructProposal (api: DeriveApi, [bytes, proposer, balance, at]: PreimageInfo): DeriveProposalImage { - let proposal: Proposal | undefined; + let proposal: Call | undefined; try { proposal = api.registry.createType('Call', bytes.toU8a(true)); @@ -53,6 +55,27 @@ function parseDemocracy (api: DeriveApi, imageOpt: Option | Option< return constructProposal(api, imageOpt.unwrap()); } +function parseImage (api: DeriveApi, [proposalHash, status, bytes]: [HexString, PalletPreimageRequestStatus | null, Bytes | null]): DeriveProposalImage | undefined { + if (!status) { + return undefined; + } + + const [proposer, balance] = status.isUnrequested + ? status.asUnrequested.deposit + : status.asRequested.deposit.unwrapOrDefault(); + let proposal: Call | undefined; + + if (bytes) { + try { + proposal = api.registry.createType('Call', bytes.toU8a(true)); + } catch (error) { + console.error(error); + } + } + + return { at: BN_ZERO, balance, proposal, proposalHash, proposer }; +} + function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> { return api.query.democracy.preimages.multi>(hashes).pipe( map((images): (DeriveProposalImage | undefined)[] => @@ -61,9 +84,37 @@ function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string ); } -function getImages (api: DeriveApi, hashes: FrameSupportPreimagesBounded[]): Observable<(DeriveProposalImage | undefined)[]> { - // TODO: We want to create the actual derives - for now this is not supported - return of(hashes.map(() => undefined)); +function getImages (api: DeriveApi, bounded: FrameSupportPreimagesBounded[]): Observable<(DeriveProposalImage | undefined)[]> { + const hashes = bounded.map((b) => getImageHashBounded(b)); + + return api.query.preimage.statusFor.multi(hashes).pipe( + switchMap((optStatus) => { + const statuses = optStatus.map((o) => o.unwrapOr(null)); + const keys = statuses + .map((s, i) => + s + ? s.isRequested + ? [hashes[i], s.asRequested.len.unwrapOr(0)] + : [hashes[i], s.asUnrequested.len] + : null + ) + .filter((p) => !!p); + + return api.query.preimage.preimageFor.multi(keys).pipe( + map((optBytes) => { + let ptr = -1; + + return statuses + .map((s, i): [HexString, PalletPreimageRequestStatus | null, Bytes | null] => + s + ? [hashes[i], s, optBytes[++ptr].unwrapOr(null)] + : [hashes[i], null, null] + ) + .map((v) => parseImage(api, v)); + }) + ); + }) + ); } export function preimages (instanceId: string, api: DeriveApi): (hashes: (Hash | Uint8Array | string | FrameSupportPreimagesBounded)[]) => Observable<(DeriveProposalImage | undefined)[]> { diff --git a/packages/api-derive/src/democracy/types.ts b/packages/api-derive/src/democracy/types.ts index f4a349af5b..7d2efa18ae 100644 --- a/packages/api-derive/src/democracy/types.ts +++ b/packages/api-derive/src/democracy/types.ts @@ -2,13 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import type { Vec } from '@polkadot/types'; -import type { AccountId, Balance, BlockNumber, Call, Hash, PropIndex, ReferendumIndex, ReferendumInfoTo239, Vote } from '@polkadot/types/interfaces'; +import type { AccountId, Balance, Call, Hash, PropIndex, ReferendumIndex, ReferendumInfoTo239, Vote } from '@polkadot/types/interfaces'; import type { PalletDemocracyReferendumStatus, PalletDemocracyVoteThreshold } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; export interface AtBlock { - at: BlockNumber; + at: BN; } export interface DeriveDemocracyLock {