11// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22// SPDX-License-Identifier: Apache-2.0
33
4- import type { Bytes , Option } from '@polkadot/types' ;
5- import type { AccountId , Balance , BlockNumber , PreimageStatus , Proposal , ReferendumInfoTo239 , Tally } from '@polkadot/types/interfaces' ;
4+ import type { ReferendumInfoTo239 , Tally } from '@polkadot/types/interfaces' ;
65import type { PalletDemocracyReferendumInfo , PalletDemocracyReferendumStatus , PalletDemocracyVoteThreshold } from '@polkadot/types/lookup' ;
7- import type { ITuple } from '@polkadot/types/types ' ;
8- import type { DeriveApi , DeriveProposalImage , DeriveReferendum , DeriveReferendumVote , DeriveReferendumVotes , DeriveReferendumVoteState } from '../types' ;
6+ import type { Option } from '@polkadot/types-codec ' ;
7+ import type { DeriveReferendum , DeriveReferendumVote , DeriveReferendumVotes , DeriveReferendumVoteState } from '../types' ;
98
109import { BN , bnSqrt } from '@polkadot/util' ;
1110
12- type PreimageInfo = [ Bytes , AccountId , Balance , BlockNumber ] ;
13- type OldPreimage = ITuple < PreimageInfo > ;
14-
1511interface ApproxState {
1612 votedAye : BN ;
1713 votedNay : BN ;
@@ -26,10 +22,6 @@ function isCurrentStatus (status: PalletDemocracyReferendumStatus | ReferendumIn
2622 return ! ! ( status as PalletDemocracyReferendumStatus ) . tally ;
2723}
2824
29- function isCurrentPreimage ( api : DeriveApi , imageOpt : Option < OldPreimage > | Option < PreimageStatus > ) : imageOpt is Option < PreimageStatus > {
30- return ! ! imageOpt && ! api . query . democracy . dispatchQueue ;
31- }
32-
3325export function compareRationals ( n1 : BN , d1 : BN , n2 : BN , d2 : BN ) : boolean {
3426 while ( true ) {
3527 const q1 = n1 . div ( d1 ) ;
@@ -141,44 +133,10 @@ export function getStatus (info: Option<PalletDemocracyReferendumInfo | Referend
141133
142134 const unwrapped = info . unwrap ( ) ;
143135
144- if ( isOldInfo ( unwrapped ) ) {
145- return unwrapped ;
146- } else if ( unwrapped . isOngoing ) {
147- return unwrapped . asOngoing ;
148- }
149-
150- // done, we don't include it here... only currently active
151- return null ;
152- }
153-
154- function constructProposal ( api : DeriveApi , [ bytes , proposer , balance , at ] : PreimageInfo ) : DeriveProposalImage {
155- let proposal : Proposal | undefined ;
156-
157- try {
158- proposal = api . registry . createType ( 'Proposal' , bytes . toU8a ( true ) ) ;
159- } catch ( error ) {
160- console . error ( error ) ;
161- }
162-
163- return { at, balance, proposal, proposer } ;
164- }
165-
166- export function parseImage ( api : DeriveApi , imageOpt : Option < OldPreimage > | Option < PreimageStatus > ) : DeriveProposalImage | undefined {
167- if ( imageOpt . isNone ) {
168- return ;
169- }
170-
171- if ( isCurrentPreimage ( api , imageOpt ) ) {
172- const status = imageOpt . unwrap ( ) ;
173-
174- if ( status . isMissing ) {
175- return ;
176- }
177-
178- const { data, deposit, provider, since } = status . asAvailable ;
179-
180- return constructProposal ( api , [ data , provider , deposit , since ] ) ;
181- }
182-
183- return constructProposal ( api , imageOpt . unwrap ( ) ) ;
136+ return isOldInfo ( unwrapped )
137+ ? unwrapped
138+ : unwrapped . isOngoing
139+ ? unwrapped . asOngoing
140+ // done, we don't include it here... only currently active
141+ : null ;
184142}
0 commit comments