diff --git a/packages/api-derive/src/democracy/preimages.ts b/packages/api-derive/src/democracy/preimages.ts index 678da6181a..82923b729b 100644 --- a/packages/api-derive/src/democracy/preimages.ts +++ b/packages/api-derive/src/democracy/preimages.ts @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { Hash } from '@polkadot/types/interfaces'; +import type { AccountId, Balance, BlockNumber, Hash, PreimageStatus, Proposal } from '@polkadot/types/interfaces'; +import type { Bytes, Option } from '@polkadot/types-codec'; +import type { ITuple } from '@polkadot/types-codec/types'; import type { DeriveApi, DeriveProposalImage } from '../types'; import { map, of } from 'rxjs'; @@ -10,12 +12,50 @@ import { map, of } from 'rxjs'; import { isFunction } from '@polkadot/util'; import { firstMemo, memo } from '../util'; -import { parseImage } from './util'; + +type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber]; +type OldPreimage = ITuple; + +function isDemocracyPreimage (api: DeriveApi, imageOpt: Option | Option): imageOpt is Option { + return !!imageOpt && !api.query.democracy.dispatchQueue; +} + +function constructProposal (api: DeriveApi, [bytes, proposer, balance, at]: PreimageInfo): DeriveProposalImage { + let proposal: Proposal | undefined; + + try { + proposal = api.registry.createType('Proposal', bytes.toU8a(true)); + } catch (error) { + console.error(error); + } + + return { at, balance, proposal, proposer }; +} + +function parseDemocracy (api: DeriveApi, imageOpt: Option | Option): DeriveProposalImage | undefined { + if (imageOpt.isNone) { + return; + } + + if (isDemocracyPreimage(api, imageOpt)) { + const status = imageOpt.unwrap(); + + if (status.isMissing) { + return; + } + + const { data, deposit, provider, since } = status.asAvailable; + + return constructProposal(api, [data, provider, deposit, since]); + } + + return constructProposal(api, imageOpt.unwrap()); +} function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> { return api.query.democracy.preimages.multi(hashes).pipe( map((images): (DeriveProposalImage | undefined)[] => - images.map((imageOpt) => parseImage(api, imageOpt)) + images.map((imageOpt) => parseDemocracy(api, imageOpt)) ) ); } diff --git a/packages/api-derive/src/democracy/referendumsInfo.ts b/packages/api-derive/src/democracy/referendumsInfo.ts index ab8ea0147d..4c1e6663df 100644 --- a/packages/api-derive/src/democracy/referendumsInfo.ts +++ b/packages/api-derive/src/democracy/referendumsInfo.ts @@ -13,7 +13,7 @@ import { combineLatest, map, of, switchMap } from 'rxjs'; import { isFunction } from '@polkadot/util'; import { memo } from '../util'; -import { calcVotes, getStatus, parseImage } from './util'; +import { calcVotes, getStatus } from './util'; type VotingDelegating = PalletDemocracyVoteVoting['asDelegating']; type VotingDirect = PalletDemocracyVoteVoting['asDirect']; @@ -132,9 +132,9 @@ export function _referendumInfo (instanceId: string, api: DeriveApi): (index: BN const status = getStatus(info); return status - ? api.query.democracy.preimages(status.proposalHash).pipe( - map((preimage): DeriveReferendum => ({ - image: parseImage(api, preimage), + ? api.derive.democracy.preimage(status.proposalHash).pipe( + map((image): DeriveReferendum => ({ + image, imageHash: status.proposalHash, index: api.registry.createType('ReferendumIndex', index), status diff --git a/packages/api-derive/src/democracy/util.ts b/packages/api-derive/src/democracy/util.ts index 0baf037309..7bd0e471d9 100644 --- a/packages/api-derive/src/democracy/util.ts +++ b/packages/api-derive/src/democracy/util.ts @@ -1,17 +1,13 @@ // Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Bytes, Option } from '@polkadot/types'; -import type { AccountId, Balance, BlockNumber, PreimageStatus, Proposal, ReferendumInfoTo239, Tally } from '@polkadot/types/interfaces'; +import type { ReferendumInfoTo239, Tally } from '@polkadot/types/interfaces'; import type { PalletDemocracyReferendumInfo, PalletDemocracyReferendumStatus, PalletDemocracyVoteThreshold } from '@polkadot/types/lookup'; -import type { ITuple } from '@polkadot/types/types'; -import type { DeriveApi, DeriveProposalImage, DeriveReferendum, DeriveReferendumVote, DeriveReferendumVotes, DeriveReferendumVoteState } from '../types'; +import type { Option } from '@polkadot/types-codec'; +import type { DeriveReferendum, DeriveReferendumVote, DeriveReferendumVotes, DeriveReferendumVoteState } from '../types'; import { BN, bnSqrt } from '@polkadot/util'; -type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber]; -type OldPreimage = ITuple; - interface ApproxState { votedAye: BN; votedNay: BN; @@ -26,10 +22,6 @@ function isCurrentStatus (status: PalletDemocracyReferendumStatus | ReferendumIn return !!(status as PalletDemocracyReferendumStatus).tally; } -function isCurrentPreimage (api: DeriveApi, imageOpt: Option | Option): imageOpt is Option { - return !!imageOpt && !api.query.democracy.dispatchQueue; -} - export function compareRationals (n1: BN, d1: BN, n2: BN, d2: BN): boolean { while (true) { const q1 = n1.div(d1); @@ -141,44 +133,10 @@ export function getStatus (info: Option | Option): DeriveProposalImage | undefined { - if (imageOpt.isNone) { - return; - } - - if (isCurrentPreimage(api, imageOpt)) { - const status = imageOpt.unwrap(); - - if (status.isMissing) { - return; - } - - const { data, deposit, provider, since } = status.asAvailable; - - return constructProposal(api, [data, provider, deposit, since]); - } - - return constructProposal(api, imageOpt.unwrap()); + return isOldInfo(unwrapped) + ? unwrapped + : unwrapped.isOngoing + ? unwrapped.asOngoing + // done, we don't include it here... only currently active + : null; }