New democracy types (#2078)

* New democracy types

* ReferendumInfo

* Adjust democracy derives

* Spring cleaning

* Remove ReferendumInfoExtended
This commit is contained in:
Jaco Greeff
2020-03-22 11:05:57 +01:00
committed by GitHub
parent 7a1bdb18a8
commit 7a7d6e9b8a
9 changed files with 254 additions and 89 deletions
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { ReferendumInfo, ReferendumInfoTo239, ReferendumStatus } from '@polkadot/types/interfaces';
import { DerivedReferendum } from '../types';
import { PreImage } from './proposals';
@@ -14,20 +14,35 @@ import { Option } from '@polkadot/types';
import { memo } from '../util';
function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<ReferendumInfo>, _preimage?: PreImage): DerivedReferendum | null {
const preImage = _preimage?.isSome
? _preimage.unwrap()
: null;
const info = _info.unwrapOr(null);
function isOld (info: ReferendumInfo | ReferendumInfoTo239): info is ReferendumInfoTo239 {
return !!(info as ReferendumInfoTo239).proposalHash;
}
if (!info) {
function getStatus (info: Option<ReferendumInfo | ReferendumInfoTo239>): ReferendumStatus | ReferendumInfoTo239 | null {
if (info.isNone) {
return null;
}
const unwrapped = info.unwrap();
if (isOld(unwrapped)) {
return unwrapped;
} else if (unwrapped.isOngoing) {
return unwrapped.asOngoing;
}
// done, we don't include it here... only currently active
return null;
}
function constructInfo (api: ApiInterfaceRx, index: BN | number, status: ReferendumStatus | ReferendumInfoTo239, _preimage?: PreImage): DerivedReferendum | null {
const preImage = _preimage?.isSome
? _preimage.unwrap()
: null;
return {
index: api.registry.createType('PropIndex', index),
info,
hash: info.proposalHash,
hash: status.proposalHash,
proposal: preImage
? api.registry.createType('Proposal', preImage[0].toU8a(true))
: undefined,
@@ -37,25 +52,26 @@ function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<R
balance: preImage[2],
proposer: preImage[1]
}
: undefined
: undefined,
status
};
}
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo>): Observable<DerivedReferendum | null> {
return ((
info?.isSome
? api.query.democracy.preimages(info.unwrap().proposalHash)
: of(undefined)
) as Observable<PreImage | undefined>).pipe(
map((preimage?: PreImage): DerivedReferendum | null =>
constructInfo(api, index, info, preimage)
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo | ReferendumInfoTo239>): Observable<DerivedReferendum | null> {
const status = getStatus(info);
return status
? api.query.democracy.preimages(status.proposalHash).pipe(
map((preimage?: PreImage): DerivedReferendum | null =>
constructInfo(api, index, status, preimage)
)
)
);
: of(null);
}
export function referendumInfo (api: ApiInterfaceRx): (index: BN | number) => Observable<DerivedReferendum | null> {
return memo((index: BN | number): Observable<DerivedReferendum | null> =>
api.query.democracy.referendumInfoOf<Option<ReferendumInfo>>(index).pipe(
api.query.democracy.referendumInfoOf<Option<ReferendumInfo | ReferendumInfoTo239>>(index).pipe(
switchMap((info): Observable<DerivedReferendum | null> =>
retrieveInfo(api, index, info)
)
@@ -1,57 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { AnyJson, Constructor, Registry } from '@polkadot/types/types';
import BN from 'bn.js';
import democracyTypes from '@polkadot/types/interfaces/democracy/definitions';
import { Struct } from '@polkadot/types';
// We can ignore the properties, added via Struct.with
const _ReferendumInfo: Constructor<ReferendumInfo> = Struct.with(democracyTypes.types.ReferendumInfo as any) as any;
/**
* @name ReferendumInfoExtended
* @description
* A [[ReferendumInfo]] with an additional `index` field
*/
export default class ReferendumInfoExtended extends _ReferendumInfo {
readonly #index: ReferendumIndex;
constructor (registry: Registry, value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
super(registry, value);
this.#index = value instanceof ReferendumInfoExtended
? value.index
: registry.createType('ReferendumIndex', index);
}
/**
* @description Convenience getter, returns the referendumIndex
*/
public get index (): ReferendumIndex {
return this.#index;
}
/**
* @description Creates a human-friendly JSON representation
*/
public toHuman (isExtended?: boolean): AnyJson {
return {
...super.toHuman(isExtended) as { [index: string]: AnyJson },
index: this.index.toHuman(isExtended)
};
}
/**
* @description Creates the JSON representation
*/
public toJSON (): AnyJson {
return {
...super.toJSON() as { [index: string]: AnyJson },
index: this.index.toJSON()
};
}
}
-1
View File
@@ -3,4 +3,3 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as HeaderExtended } from './HeaderExtended';
export { default as ReferendumInfoExtended } from './ReferendumInfoExtended';
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
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';
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';
import BN from 'bn.js';
import { u32, Vec } from '@polkadot/types';
@@ -131,9 +131,9 @@ export interface DeriveProposal {
export interface DerivedReferendum {
hash: Hash;
index: PropIndex;
info: ReferendumInfo;
preimage?: DeriveProposalPreImage;
proposal?: Proposal;
status: ReferendumStatus | ReferendumInfoTo239;
}
export type DerivedRecentlyOffline = Record<string, RecentlyOffline[]>;
+40 -1
View File
@@ -12,7 +12,7 @@ import { EthereumAddress } from '@polkadot/types/interfaces/claims';
import { MemberCount, ProposalIndex, Votes, VotesTo230 } from '@polkadot/types/interfaces/collective';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { AliveContractInfo, CodeHash, ContractCallRequest, ContractExecResult, ContractExecResultSuccess, ContractInfo, ContractStorageKey, Gas, PrefabWasmModule, PrefabWasmModuleReserved, Schedule, ScheduleTo212, SeedOf, TombstoneContractInfo, TrieId } from '@polkadot/types/interfaces/contracts';
import { Conviction, PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo } from '@polkadot/types/interfaces/democracy';
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';
import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from '@polkadot/types/interfaces/elections';
import { CreatedBlock, ImportedAux } from '@polkadot/types/interfaces/engine';
import { Account, Log } from '@polkadot/types/interfaces/evm';
@@ -410,9 +410,24 @@ declare module '@polkadot/types/types/registry' {
TrieId: TrieId;
'Option<TrieId>': Option<TrieId>;
'Vec<TrieId>': Vec<TrieId>;
AccountVote: AccountVote;
'Option<AccountVote>': Option<AccountVote>;
'Vec<AccountVote>': Vec<AccountVote>;
AccountVoteSplit: AccountVoteSplit;
'Option<AccountVoteSplit>': Option<AccountVoteSplit>;
'Vec<AccountVoteSplit>': Vec<AccountVoteSplit>;
AccountVoteStandard: AccountVoteStandard;
'Option<AccountVoteStandard>': Option<AccountVoteStandard>;
'Vec<AccountVoteStandard>': Vec<AccountVoteStandard>;
Conviction: Conviction;
'Option<Conviction>': Option<Conviction>;
'Vec<Conviction>': Vec<Conviction>;
Delegations: Delegations;
'Option<Delegations>': Option<Delegations>;
'Vec<Delegations>': Vec<Delegations>;
PriorLock: PriorLock;
'Option<PriorLock>': Option<PriorLock>;
'Vec<PriorLock>': Vec<PriorLock>;
PropIndex: PropIndex;
'Compact<PropIndex>': Compact<PropIndex>;
'Option<PropIndex>': Option<PropIndex>;
@@ -427,9 +442,33 @@ declare module '@polkadot/types/types/registry' {
'Compact<ReferendumIndex>': Compact<ReferendumIndex>;
'Option<ReferendumIndex>': Option<ReferendumIndex>;
'Vec<ReferendumIndex>': Vec<ReferendumIndex>;
ReferendumInfoTo239: ReferendumInfoTo239;
'Option<ReferendumInfoTo239>': Option<ReferendumInfoTo239>;
'Vec<ReferendumInfoTo239>': Vec<ReferendumInfoTo239>;
ReferendumInfo: ReferendumInfo;
'Option<ReferendumInfo>': Option<ReferendumInfo>;
'Vec<ReferendumInfo>': Vec<ReferendumInfo>;
ReferendumInfoFinished: ReferendumInfoFinished;
'Option<ReferendumInfoFinished>': Option<ReferendumInfoFinished>;
'Vec<ReferendumInfoFinished>': Vec<ReferendumInfoFinished>;
ReferendumStatus: ReferendumStatus;
'Option<ReferendumStatus>': Option<ReferendumStatus>;
'Vec<ReferendumStatus>': Vec<ReferendumStatus>;
Tally: Tally;
'Option<Tally>': Option<Tally>;
'Vec<Tally>': Vec<Tally>;
Voting: Voting;
'Option<Voting>': Option<Voting>;
'Vec<Voting>': Vec<Voting>;
VotingDirect: VotingDirect;
'Option<VotingDirect>': Option<VotingDirect>;
'Vec<VotingDirect>': Vec<VotingDirect>;
VotingDirectVote: VotingDirectVote;
'Option<VotingDirectVote>': Option<VotingDirectVote>;
'Vec<VotingDirectVote>': Vec<VotingDirectVote>;
VotingDelegating: VotingDelegating;
'Option<VotingDelegating>': Option<VotingDelegating>;
'Vec<VotingDelegating>': Vec<VotingDelegating>;
ApprovalFlag: ApprovalFlag;
'Compact<ApprovalFlag>': Compact<ApprovalFlag>;
'Option<ApprovalFlag>': Option<ApprovalFlag>;
@@ -26,9 +26,28 @@ export { AllConvictions };
export default {
rpc: {},
types: {
AccountVote: {
_enum: {
Standard: 'AccountVoteStandard',
Split: 'AccountVoteSplit'
}
},
AccountVoteSplit: {
aye: 'Balance',
nay: 'Balance'
},
AccountVoteStandard: {
vote: 'Vote',
balance: 'Balance'
},
Conviction: {
_enum: AllConvictions
},
Delegations: {
votes: 'Balance',
capital: 'Balance'
},
PriorLock: '(BlockNumber, Balance)',
PropIndex: 'u32',
Proposal: 'Call',
ProxyState: {
@@ -36,11 +55,52 @@ export default {
Active: 'AccountId'
},
ReferendumIndex: 'u32',
ReferendumInfo: {
ReferendumInfoTo239: {
end: 'BlockNumber',
proposalHash: 'Hash',
threshold: 'VoteThreshold',
delay: 'BlockNumber'
},
ReferendumInfo: {
_enum: {
Ongoing: 'ReferendumStatus',
Finished: 'ReferendumInfoFinished'
}
},
ReferendumInfoFinished: {
approved: 'bool',
end: 'BlockNumber'
},
ReferendumStatus: {
end: 'BlockNumber',
proposalHash: 'Hash',
threshold: 'VoteThreshold',
delay: 'BlockNumber',
tally: 'Tally'
},
Tally: {
ayes: 'Balance',
nays: 'Balance',
turnout: 'Balance'
},
Voting: {
_enum: {
Direct: 'VotingDirect',
Delegating: 'VotingDelegating'
}
},
VotingDirect: {
votes: 'Vec<VotingDirectVote>',
delegations: 'Delegations',
prior: 'PriorLock'
},
VotingDirectVote: '(ReferendumIndex, AccountVote)',
VotingDelegating: {
balance: 'Balance',
target: 'AccountId',
conviction: 'Conviction',
delegations: 'Delegations',
prior: 'PriorLock'
}
}
} as Definitions;
@@ -1,10 +1,31 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { Enum, Struct } from '@polkadot/types/codec';
import { u32 } from '@polkadot/types/primitive';
import { VoteThreshold } from '@polkadot/types/interfaces/elections';
import { AccountId, BlockNumber, Call, Hash } from '@polkadot/types/interfaces/runtime';
import { ITuple } from '@polkadot/types/types';
import { Enum, Struct, Vec } from '@polkadot/types/codec';
import { bool, u32 } from '@polkadot/types/primitive';
import { Vote, VoteThreshold } from '@polkadot/types/interfaces/elections';
import { AccountId, Balance, BlockNumber, Call, Hash } from '@polkadot/types/interfaces/runtime';
/** @name AccountVote */
export interface AccountVote extends Enum {
readonly isStandard: boolean;
readonly asStandard: AccountVoteStandard;
readonly isSplit: boolean;
readonly asSplit: AccountVoteSplit;
}
/** @name AccountVoteSplit */
export interface AccountVoteSplit extends Struct {
readonly aye: Balance;
readonly nay: Balance;
}
/** @name AccountVoteStandard */
export interface AccountVoteStandard extends Struct {
readonly vote: Vote;
readonly balance: Balance;
}
/** @name Conviction */
export interface Conviction extends Enum {
@@ -17,6 +38,15 @@ export interface Conviction extends Enum {
readonly isLocked6X: boolean;
}
/** @name Delegations */
export interface Delegations extends Struct {
readonly votes: Balance;
readonly capital: Balance;
}
/** @name PriorLock */
export interface PriorLock extends ITuple<[BlockNumber, Balance]> {}
/** @name PropIndex */
export interface PropIndex extends u32 {}
@@ -33,11 +63,68 @@ export interface ProxyState extends Struct {
export interface ReferendumIndex extends u32 {}
/** @name ReferendumInfo */
export interface ReferendumInfo extends Struct {
export interface ReferendumInfo extends Enum {
readonly isOngoing: boolean;
readonly asOngoing: ReferendumStatus;
readonly isFinished: boolean;
readonly asFinished: ReferendumInfoFinished;
}
/** @name ReferendumInfoFinished */
export interface ReferendumInfoFinished extends Struct {
readonly approved: bool;
readonly end: BlockNumber;
}
/** @name ReferendumInfoTo239 */
export interface ReferendumInfoTo239 extends Struct {
readonly end: BlockNumber;
readonly proposalHash: Hash;
readonly threshold: VoteThreshold;
readonly delay: BlockNumber;
}
/** @name ReferendumStatus */
export interface ReferendumStatus extends Struct {
readonly end: BlockNumber;
readonly proposalHash: Hash;
readonly threshold: VoteThreshold;
readonly delay: BlockNumber;
readonly tally: Tally;
}
/** @name Tally */
export interface Tally extends Struct {
readonly ayes: Balance;
readonly nays: Balance;
readonly turnout: Balance;
}
/** @name Voting */
export interface Voting extends Enum {
readonly isDirect: boolean;
readonly asDirect: VotingDirect;
readonly isDelegating: boolean;
readonly asDelegating: VotingDelegating;
}
/** @name VotingDelegating */
export interface VotingDelegating extends Struct {
readonly balance: Balance;
readonly target: AccountId;
readonly conviction: Conviction;
readonly delegations: Delegations;
readonly prior: PriorLock;
}
/** @name VotingDirect */
export interface VotingDirect extends Struct {
readonly votes: Vec<VotingDirectVote>;
readonly delegations: Delegations;
readonly prior: PriorLock;
}
/** @name VotingDirectVote */
export interface VotingDirectVote extends ITuple<[ReferendumIndex, AccountVote]> {}
export type PHANTOM_DEMOCRACY = 'democracy';
+21 -2
View File
@@ -53,9 +53,17 @@ const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
}
},
{
minmax: [1004, undefined],
minmax: [1004, 1004],
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5',
ReferendumInfo: 'ReferendumInfoTo239'
}
},
{
minmax: [1005, undefined],
types: {
Address: 'AccountId',
Keys: 'SessionKeys5'
}
@@ -71,6 +79,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
BalanceLock: 'BalanceLockTo212',
DispatchError: 'DispatchErrorTo198',
Keys: 'SessionKeys5',
ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
@@ -82,6 +91,7 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
@@ -94,15 +104,24 @@ const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
ReferendumInfo: 'ReferendumInfoTo239',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
}
},
{
// actual at 1050 (1046-1049 is dev)
minmax: [1046, undefined],
minmax: [1046, 1054],
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5',
ReferendumInfo: 'ReferendumInfoTo239'
}
},
{
minmax: [1055, undefined],
types: {
Address: 'AccountId',
Keys: 'SessionKeys5'
}