diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c3af6031e2..18c1fe2f90de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - **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) - **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` +- **Important** Substrate has made changes to democracy, if using an older chain add the `ReferendumInfo: 'ReferendumInfoTo239'` type if using referendums +- Adjust referendum derives to cater for new and old democracy (`referendumInfo` now includes `status` field, not `info`) - The Substrate extrinsic phase definitions has been expanded with `Initialization` to align with the latest versions # 1.7.1 Mar 17, 2020 diff --git a/packages/api-derive/src/democracy/referendumInfo.ts b/packages/api-derive/src/democracy/referendumInfo.ts index ed0eb0abd17f..caef363323a7 100644 --- a/packages/api-derive/src/democracy/referendumInfo.ts +++ b/packages/api-derive/src/democracy/referendumInfo.ts @@ -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, _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): 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): Observable { - return (( - info?.isSome - ? api.query.democracy.preimages(info.unwrap().proposalHash) - : of(undefined) - ) as Observable).pipe( - map((preimage?: PreImage): DerivedReferendum | null => - constructInfo(api, index, info, preimage) +export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option): Observable { + 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 { return memo((index: BN | number): Observable => - api.query.democracy.referendumInfoOf>(index).pipe( + api.query.democracy.referendumInfoOf>(index).pipe( switchMap((info): Observable => retrieveInfo(api, index, info) ) diff --git a/packages/api-derive/src/type/ReferendumInfoExtended.ts b/packages/api-derive/src/type/ReferendumInfoExtended.ts deleted file mode 100644 index a08dff00ed09..000000000000 --- a/packages/api-derive/src/type/ReferendumInfoExtended.ts +++ /dev/null @@ -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 = 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() - }; - } -} diff --git a/packages/api-derive/src/type/index.ts b/packages/api-derive/src/type/index.ts index 48854aa25d54..0040bfc0e37f 100644 --- a/packages/api-derive/src/type/index.ts +++ b/packages/api-derive/src/type/index.ts @@ -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'; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 3e9ffa0febce..e52badd85e8c 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -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; diff --git a/packages/types/src/augment/registry.ts b/packages/types/src/augment/registry.ts index 02053d518bd1..c412ebcb7f97 100644 --- a/packages/types/src/augment/registry.ts +++ b/packages/types/src/augment/registry.ts @@ -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': Option; 'Vec': Vec; + AccountVote: AccountVote; + 'Option': Option; + 'Vec': Vec; + AccountVoteSplit: AccountVoteSplit; + 'Option': Option; + 'Vec': Vec; + AccountVoteStandard: AccountVoteStandard; + 'Option': Option; + 'Vec': Vec; Conviction: Conviction; 'Option': Option; 'Vec': Vec; + Delegations: Delegations; + 'Option': Option; + 'Vec': Vec; + PriorLock: PriorLock; + 'Option': Option; + 'Vec': Vec; PropIndex: PropIndex; 'Compact': Compact; 'Option': Option; @@ -427,9 +442,33 @@ declare module '@polkadot/types/types/registry' { 'Compact': Compact; 'Option': Option; 'Vec': Vec; + ReferendumInfoTo239: ReferendumInfoTo239; + 'Option': Option; + 'Vec': Vec; ReferendumInfo: ReferendumInfo; 'Option': Option; 'Vec': Vec; + ReferendumInfoFinished: ReferendumInfoFinished; + 'Option': Option; + 'Vec': Vec; + ReferendumStatus: ReferendumStatus; + 'Option': Option; + 'Vec': Vec; + Tally: Tally; + 'Option': Option; + 'Vec': Vec; + Voting: Voting; + 'Option': Option; + 'Vec': Vec; + VotingDirect: VotingDirect; + 'Option': Option; + 'Vec': Vec; + VotingDirectVote: VotingDirectVote; + 'Option': Option; + 'Vec': Vec; + VotingDelegating: VotingDelegating; + 'Option': Option; + 'Vec': Vec; ApprovalFlag: ApprovalFlag; 'Compact': Compact; 'Option': Option; diff --git a/packages/types/src/interfaces/democracy/definitions.ts b/packages/types/src/interfaces/democracy/definitions.ts index 175eae0ea885..650b48cb828d 100644 --- a/packages/types/src/interfaces/democracy/definitions.ts +++ b/packages/types/src/interfaces/democracy/definitions.ts @@ -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', + delegations: 'Delegations', + prior: 'PriorLock' + }, + VotingDirectVote: '(ReferendumIndex, AccountVote)', + VotingDelegating: { + balance: 'Balance', + target: 'AccountId', + conviction: 'Conviction', + delegations: 'Delegations', + prior: 'PriorLock' } } } as Definitions; diff --git a/packages/types/src/interfaces/democracy/types.ts b/packages/types/src/interfaces/democracy/types.ts index 1ef5aa7b1341..c5a431c600b4 100644 --- a/packages/types/src/interfaces/democracy/types.ts +++ b/packages/types/src/interfaces/democracy/types.ts @@ -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; + readonly delegations: Delegations; + readonly prior: PriorLock; +} + +/** @name VotingDirectVote */ +export interface VotingDirectVote extends ITuple<[ReferendumIndex, AccountVote]> {} + export type PHANTOM_DEMOCRACY = 'democracy'; diff --git a/packages/types/src/known/overrides.ts b/packages/types/src/known/overrides.ts index 811b341398da..d15c3d451960 100644 --- a/packages/types/src/known/overrides.ts +++ b/packages/types/src/known/overrides.ts @@ -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' }