Skip to content

Commit 7a7d6e9

Browse files
authored
New democracy types (#2078)
* New democracy types * ReferendumInfo * Adjust democracy derives * Spring cleaning * Remove ReferendumInfoExtended
1 parent 7a1bdb1 commit 7a7d6e9

File tree

9 files changed

+254
-89
lines changed

9 files changed

+254
-89
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- **Breaking change** The format for any custom RPCs have been changed, alongside API-internal changes to allow for better RPC management. If you are currently using custom RPCs (or planning to do so), look at the [updated documentation](https://polkadot.js.org/api/start/rpc.custom.html)
44
- **Breaking change** Alongside API RPC changes, the `@polkadot/jsonrpc` package has been removed. Since it was never documented and only used internally, this should not have adverse impacts. All RPC definitions itself has now been moved to the relevant modules inside `@polkadot/types/interfaces`
5+
- **Important** Substrate has made changes to democracy, if using an older chain add the `ReferendumInfo: 'ReferendumInfoTo239'` type if using referendums
6+
- Adjust referendum derives to cater for new and old democracy (`referendumInfo` now includes `status` field, not `info`)
57
- The Substrate extrinsic phase definitions has been expanded with `Initialization` to align with the latest versions
68

79
# 1.7.1 Mar 17, 2020

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This software may be modified and distributed under the terms
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

5-
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
5+
import { ReferendumInfo, ReferendumInfoTo239, ReferendumStatus } from '@polkadot/types/interfaces';
66
import { DerivedReferendum } from '../types';
77
import { PreImage } from './proposals';
88

@@ -14,20 +14,35 @@ import { Option } from '@polkadot/types';
1414

1515
import { memo } from '../util';
1616

17-
function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<ReferendumInfo>, _preimage?: PreImage): DerivedReferendum | null {
18-
const preImage = _preimage?.isSome
19-
? _preimage.unwrap()
20-
: null;
21-
const info = _info.unwrapOr(null);
17+
function isOld (info: ReferendumInfo | ReferendumInfoTo239): info is ReferendumInfoTo239 {
18+
return !!(info as ReferendumInfoTo239).proposalHash;
19+
}
2220

23-
if (!info) {
21+
function getStatus (info: Option<ReferendumInfo | ReferendumInfoTo239>): ReferendumStatus | ReferendumInfoTo239 | null {
22+
if (info.isNone) {
2423
return null;
2524
}
2625

26+
const unwrapped = info.unwrap();
27+
28+
if (isOld(unwrapped)) {
29+
return unwrapped;
30+
} else if (unwrapped.isOngoing) {
31+
return unwrapped.asOngoing;
32+
}
33+
34+
// done, we don't include it here... only currently active
35+
return null;
36+
}
37+
38+
function constructInfo (api: ApiInterfaceRx, index: BN | number, status: ReferendumStatus | ReferendumInfoTo239, _preimage?: PreImage): DerivedReferendum | null {
39+
const preImage = _preimage?.isSome
40+
? _preimage.unwrap()
41+
: null;
42+
2743
return {
2844
index: api.registry.createType('PropIndex', index),
29-
info,
30-
hash: info.proposalHash,
45+
hash: status.proposalHash,
3146
proposal: preImage
3247
? api.registry.createType('Proposal', preImage[0].toU8a(true))
3348
: undefined,
@@ -37,25 +52,26 @@ function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<R
3752
balance: preImage[2],
3853
proposer: preImage[1]
3954
}
40-
: undefined
55+
: undefined,
56+
status
4157
};
4258
}
4359

44-
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo>): Observable<DerivedReferendum | null> {
45-
return ((
46-
info?.isSome
47-
? api.query.democracy.preimages(info.unwrap().proposalHash)
48-
: of(undefined)
49-
) as Observable<PreImage | undefined>).pipe(
50-
map((preimage?: PreImage): DerivedReferendum | null =>
51-
constructInfo(api, index, info, preimage)
60+
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo | ReferendumInfoTo239>): Observable<DerivedReferendum | null> {
61+
const status = getStatus(info);
62+
63+
return status
64+
? api.query.democracy.preimages(status.proposalHash).pipe(
65+
map((preimage?: PreImage): DerivedReferendum | null =>
66+
constructInfo(api, index, status, preimage)
67+
)
5268
)
53-
);
69+
: of(null);
5470
}
5571

5672
export function referendumInfo (api: ApiInterfaceRx): (index: BN | number) => Observable<DerivedReferendum | null> {
5773
return memo((index: BN | number): Observable<DerivedReferendum | null> =>
58-
api.query.democracy.referendumInfoOf<Option<ReferendumInfo>>(index).pipe(
74+
api.query.democracy.referendumInfoOf<Option<ReferendumInfo | ReferendumInfoTo239>>(index).pipe(
5975
switchMap((info): Observable<DerivedReferendum | null> =>
6076
retrieveInfo(api, index, info)
6177
)

packages/api-derive/src/type/ReferendumInfoExtended.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

packages/api-derive/src/type/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

55
export { default as HeaderExtended } from './HeaderExtended';
6-
export { default as ReferendumInfoExtended } from './ReferendumInfoExtended';

packages/api-derive/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This software may be modified and distributed under the terms
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

5-
import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Hash, Index, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces';
5+
import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Hash, Index, Proposal, PropIndex, ProposalIndex, ReferendumInfoTo239, ReferendumStatus, RegistrationJudgement, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces';
66

77
import BN from 'bn.js';
88
import { u32, Vec } from '@polkadot/types';
@@ -131,9 +131,9 @@ export interface DeriveProposal {
131131
export interface DerivedReferendum {
132132
hash: Hash;
133133
index: PropIndex;
134-
info: ReferendumInfo;
135134
preimage?: DeriveProposalPreImage;
136135
proposal?: Proposal;
136+
status: ReferendumStatus | ReferendumInfoTo239;
137137
}
138138

139139
export type DerivedRecentlyOffline = Record<string, RecentlyOffline[]>;

packages/types/src/augment/registry.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EthereumAddress } from '@polkadot/types/interfaces/claims';
1212
import { MemberCount, ProposalIndex, Votes, VotesTo230 } from '@polkadot/types/interfaces/collective';
1313
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
1414
import { AliveContractInfo, CodeHash, ContractCallRequest, ContractExecResult, ContractExecResultSuccess, ContractInfo, ContractStorageKey, Gas, PrefabWasmModule, PrefabWasmModuleReserved, Schedule, ScheduleTo212, SeedOf, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts';
15-
import { Conviction, PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo } from '@polkadot/types/interfaces/democracy';
15+
import { AccountVote, AccountVoteSplit, AccountVoteStandard, Conviction, Delegations, PriorLock, PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo, ReferendumInfoFinished, ReferendumInfoTo239, ReferendumStatus, Tally, Voting, VotingDelegating, VotingDirect, VotingDirectVote } from '@polkadot/types/interfaces/democracy';
1616
import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from '@polkadot/types/interfaces/elections';
1717
import { CreatedBlock, ImportedAux } from '@polkadot/types/interfaces/engine';
1818
import { Account, Log } from '@polkadot/types/interfaces/evm';
@@ -410,9 +410,24 @@ declare module '@polkadot/types/types/registry' {
410410
TrieId: TrieId;
411411
'Option<TrieId>': Option<TrieId>;
412412
'Vec<TrieId>': Vec<TrieId>;
413+
AccountVote: AccountVote;
414+
'Option<AccountVote>': Option<AccountVote>;
415+
'Vec<AccountVote>': Vec<AccountVote>;
416+
AccountVoteSplit: AccountVoteSplit;
417+
'Option<AccountVoteSplit>': Option<AccountVoteSplit>;
418+
'Vec<AccountVoteSplit>': Vec<AccountVoteSplit>;
419+
AccountVoteStandard: AccountVoteStandard;
420+
'Option<AccountVoteStandard>': Option<AccountVoteStandard>;
421+
'Vec<AccountVoteStandard>': Vec<AccountVoteStandard>;
413422
Conviction: Conviction;
414423
'Option<Conviction>': Option<Conviction>;
415424
'Vec<Conviction>': Vec<Conviction>;
425+
Delegations: Delegations;
426+
'Option<Delegations>': Option<Delegations>;
427+
'Vec<Delegations>': Vec<Delegations>;
428+
PriorLock: PriorLock;
429+
'Option<PriorLock>': Option<PriorLock>;
430+
'Vec<PriorLock>': Vec<PriorLock>;
416431
PropIndex: PropIndex;
417432
'Compact<PropIndex>': Compact<PropIndex>;
418433
'Option<PropIndex>': Option<PropIndex>;
@@ -427,9 +442,33 @@ declare module '@polkadot/types/types/registry' {
427442
'Compact<ReferendumIndex>': Compact<ReferendumIndex>;
428443
'Option<ReferendumIndex>': Option<ReferendumIndex>;
429444
'Vec<ReferendumIndex>': Vec<ReferendumIndex>;
445+
ReferendumInfoTo239: ReferendumInfoTo239;
446+
'Option<ReferendumInfoTo239>': Option<ReferendumInfoTo239>;
447+
'Vec<ReferendumInfoTo239>': Vec<ReferendumInfoTo239>;
430448
ReferendumInfo: ReferendumInfo;
431449
'Option<ReferendumInfo>': Option<ReferendumInfo>;
432450
'Vec<ReferendumInfo>': Vec<ReferendumInfo>;
451+
ReferendumInfoFinished: ReferendumInfoFinished;
452+
'Option<ReferendumInfoFinished>': Option<ReferendumInfoFinished>;
453+
'Vec<ReferendumInfoFinished>': Vec<ReferendumInfoFinished>;
454+
ReferendumStatus: ReferendumStatus;
455+
'Option<ReferendumStatus>': Option<ReferendumStatus>;
456+
'Vec<ReferendumStatus>': Vec<ReferendumStatus>;
457+
Tally: Tally;
458+
'Option<Tally>': Option<Tally>;
459+
'Vec<Tally>': Vec<Tally>;
460+
Voting: Voting;
461+
'Option<Voting>': Option<Voting>;
462+
'Vec<Voting>': Vec<Voting>;
463+
VotingDirect: VotingDirect;
464+
'Option<VotingDirect>': Option<VotingDirect>;
465+
'Vec<VotingDirect>': Vec<VotingDirect>;
466+
VotingDirectVote: VotingDirectVote;
467+
'Option<VotingDirectVote>': Option<VotingDirectVote>;
468+
'Vec<VotingDirectVote>': Vec<VotingDirectVote>;
469+
VotingDelegating: VotingDelegating;
470+
'Option<VotingDelegating>': Option<VotingDelegating>;
471+
'Vec<VotingDelegating>': Vec<VotingDelegating>;
433472
ApprovalFlag: ApprovalFlag;
434473
'Compact<ApprovalFlag>': Compact<ApprovalFlag>;
435474
'Option<ApprovalFlag>': Option<ApprovalFlag>;

packages/types/src/interfaces/democracy/definitions.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,81 @@ export { AllConvictions };
2626
export default {
2727
rpc: {},
2828
types: {
29+
AccountVote: {
30+
_enum: {
31+
Standard: 'AccountVoteStandard',
32+
Split: 'AccountVoteSplit'
33+
}
34+
},
35+
AccountVoteSplit: {
36+
aye: 'Balance',
37+
nay: 'Balance'
38+
},
39+
AccountVoteStandard: {
40+
vote: 'Vote',
41+
balance: 'Balance'
42+
},
2943
Conviction: {
3044
_enum: AllConvictions
3145
},
46+
Delegations: {
47+
votes: 'Balance',
48+
capital: 'Balance'
49+
},
50+
PriorLock: '(BlockNumber, Balance)',
3251
PropIndex: 'u32',
3352
Proposal: 'Call',
3453
ProxyState: {
3554
Open: 'AccountId',
3655
Active: 'AccountId'
3756
},
3857
ReferendumIndex: 'u32',
39-
ReferendumInfo: {
58+
ReferendumInfoTo239: {
4059
end: 'BlockNumber',
4160
proposalHash: 'Hash',
4261
threshold: 'VoteThreshold',
4362
delay: 'BlockNumber'
63+
},
64+
ReferendumInfo: {
65+
_enum: {
66+
Ongoing: 'ReferendumStatus',
67+
Finished: 'ReferendumInfoFinished'
68+
}
69+
},
70+
ReferendumInfoFinished: {
71+
approved: 'bool',
72+
end: 'BlockNumber'
73+
},
74+
ReferendumStatus: {
75+
end: 'BlockNumber',
76+
proposalHash: 'Hash',
77+
threshold: 'VoteThreshold',
78+
delay: 'BlockNumber',
79+
tally: 'Tally'
80+
},
81+
Tally: {
82+
ayes: 'Balance',
83+
nays: 'Balance',
84+
turnout: 'Balance'
85+
},
86+
Voting: {
87+
_enum: {
88+
Direct: 'VotingDirect',
89+
Delegating: 'VotingDelegating'
90+
}
91+
},
92+
VotingDirect: {
93+
votes: 'Vec<VotingDirectVote>',
94+
delegations: 'Delegations',
95+
prior: 'PriorLock'
96+
},
97+
VotingDirectVote: '(ReferendumIndex, AccountVote)',
98+
VotingDelegating: {
99+
balance: 'Balance',
100+
target: 'AccountId',
101+
conviction: 'Conviction',
102+
delegations: 'Delegations',
103+
prior: 'PriorLock'
44104
}
45105
}
46106
} as Definitions;

0 commit comments

Comments
 (0)