Skip to content

Commit d13f66e

Browse files
authored
Prep for preimage pallet usage (#4372)
* Prep for preimage pallet usage * info uses derives * adjust
1 parent 9f400ea commit d13f66e

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

packages/api-derive/src/democracy/preimages.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,60 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { Observable } from 'rxjs';
5-
import type { Hash } from '@polkadot/types/interfaces';
5+
import type { AccountId, Balance, BlockNumber, Hash, PreimageStatus, Proposal } from '@polkadot/types/interfaces';
6+
import type { Bytes, Option } from '@polkadot/types-codec';
7+
import type { ITuple } from '@polkadot/types-codec/types';
68
import type { DeriveApi, DeriveProposalImage } from '../types';
79

810
import { map, of } from 'rxjs';
911

1012
import { isFunction } from '@polkadot/util';
1113

1214
import { firstMemo, memo } from '../util';
13-
import { parseImage } from './util';
15+
16+
type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber];
17+
type OldPreimage = ITuple<PreimageInfo>;
18+
19+
function isDemocracyPreimage (api: DeriveApi, imageOpt: Option<OldPreimage> | Option<PreimageStatus>): imageOpt is Option<PreimageStatus> {
20+
return !!imageOpt && !api.query.democracy.dispatchQueue;
21+
}
22+
23+
function constructProposal (api: DeriveApi, [bytes, proposer, balance, at]: PreimageInfo): DeriveProposalImage {
24+
let proposal: Proposal | undefined;
25+
26+
try {
27+
proposal = api.registry.createType('Proposal', bytes.toU8a(true));
28+
} catch (error) {
29+
console.error(error);
30+
}
31+
32+
return { at, balance, proposal, proposer };
33+
}
34+
35+
function parseDemocracy (api: DeriveApi, imageOpt: Option<OldPreimage> | Option<PreimageStatus>): DeriveProposalImage | undefined {
36+
if (imageOpt.isNone) {
37+
return;
38+
}
39+
40+
if (isDemocracyPreimage(api, imageOpt)) {
41+
const status = imageOpt.unwrap();
42+
43+
if (status.isMissing) {
44+
return;
45+
}
46+
47+
const { data, deposit, provider, since } = status.asAvailable;
48+
49+
return constructProposal(api, [data, provider, deposit, since]);
50+
}
51+
52+
return constructProposal(api, imageOpt.unwrap());
53+
}
1454

1555
function getDemocracyImages (api: DeriveApi, hashes: (Hash | Uint8Array | string)[]): Observable<(DeriveProposalImage | undefined)[]> {
1656
return api.query.democracy.preimages.multi(hashes).pipe(
1757
map((images): (DeriveProposalImage | undefined)[] =>
18-
images.map((imageOpt) => parseImage(api, imageOpt))
58+
images.map((imageOpt) => parseDemocracy(api, imageOpt))
1959
)
2060
);
2161
}

packages/api-derive/src/democracy/referendumsInfo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { combineLatest, map, of, switchMap } from 'rxjs';
1313
import { isFunction } from '@polkadot/util';
1414

1515
import { memo } from '../util';
16-
import { calcVotes, getStatus, parseImage } from './util';
16+
import { calcVotes, getStatus } from './util';
1717

1818
type VotingDelegating = PalletDemocracyVoteVoting['asDelegating'];
1919
type VotingDirect = PalletDemocracyVoteVoting['asDirect'];
@@ -132,9 +132,9 @@ export function _referendumInfo (instanceId: string, api: DeriveApi): (index: BN
132132
const status = getStatus(info);
133133

134134
return status
135-
? api.query.democracy.preimages(status.proposalHash).pipe(
136-
map((preimage): DeriveReferendum => ({
137-
image: parseImage(api, preimage),
135+
? api.derive.democracy.preimage(status.proposalHash).pipe(
136+
map((image): DeriveReferendum => ({
137+
image,
138138
imageHash: status.proposalHash,
139139
index: api.registry.createType('ReferendumIndex', index),
140140
status

packages/api-derive/src/democracy/util.ts

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
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';
65
import 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

109
import { BN, bnSqrt } from '@polkadot/util';
1110

12-
type PreimageInfo = [Bytes, AccountId, Balance, BlockNumber];
13-
type OldPreimage = ITuple<PreimageInfo>;
14-
1511
interface 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-
3325
export 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

Comments
 (0)