Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 36 additions & 20 deletions packages/api-derive/src/democracy/referendumInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand All @@ -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)
)
Expand Down
57 changes: 0 additions & 57 deletions packages/api-derive/src/type/ReferendumInfoExtended.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/api-derive/src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 2 additions & 2 deletions packages/api-derive/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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[]>;
Expand Down
41 changes: 40 additions & 1 deletion packages/types/src/augment/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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>;
Expand All @@ -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>;
Expand Down
62 changes: 61 additions & 1 deletion packages/types/src/interfaces/democracy/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,81 @@ 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: {
Open: 'AccountId',
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;
Loading