Add 6x to conviction (#1623)

* Add 6x to conviction

* Add derive.democracy.proposals

* Proposal decoding

* Don't return empty proposals

* Rework referendumInfo

* Update referendumInfo extraction
This commit is contained in:
Jaco Greeff
2019-12-02 10:32:20 +01:00
committed by GitHub
parent c9352af81c
commit 906732bcdc
8 changed files with 166 additions and 40 deletions
@@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './proposals';
export * from './referendumInfos';
export * from './referendums';
export * from './referendumVotesFor';
@@ -0,0 +1,71 @@
// Copyright 2017-2019 @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 { AccountId, Balance, BlockNumber, Hash, PropIndex } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveProposal } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Option, Vec, createType } from '@polkadot/types';
import { memo } from '../util';
export type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
type Depositors = Option<ITuple<[Balance, Vec<AccountId>]>>;
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveProposal[]> {
return memo((): Observable<DeriveProposal[]> =>
api.query.democracy
.publicProps<Vec<ITuple<[PropIndex, Hash, AccountId]>>>()
.pipe(
switchMap((proposals) =>
combineLatest([
of(proposals),
combineLatest(
...proposals.map(([, hash]) =>
api.query.democracy.preimages<PreImage>(hash)
)
)
])
),
switchMap(([proposals, preimages]) =>
combineLatest([
of(proposals),
of(preimages),
combineLatest(
...proposals.map(([index]) =>
api.query.democracy.depositOf<Depositors>(index)
)
)
])
),
map(([proposals, _preImages, _depositors]): DeriveProposal[] =>
proposals
.filter(([, , proposer], index): boolean =>
!!(_preImages[index]?.isSome) && !!(_depositors[index]?.isSome) && !proposer.isEmpty
)
.map(([propIndex, hash, proposer], index): DeriveProposal => {
const preImage = _preImages[index].unwrap();
const depositors = _depositors[index].unwrap();
return {
balance: depositors[0],
hash,
index: propIndex,
preimage: {
at: preImage[3],
balance: preImage[2],
proposer: preImage[1]
},
proposal: createType(api.registry, 'Proposal', preImage[0].toU8a(true)),
proposer,
seconds: depositors[1]
};
})
)
)
);
}
@@ -3,36 +3,58 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import { PreImage } from './proposals';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { isNull } from '@polkadot/util';
import { Option, createType } from '@polkadot/types';
import { ReferendumInfoExtended } from '../type';
import { memo } from '../util';
export function constructInfo (api: ApiInterfaceRx, index: BN | number, optionInfo?: Option<ReferendumInfo>): Option<ReferendumInfoExtended> {
const info = optionInfo
? optionInfo.unwrapOr(null)
function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<ReferendumInfo>, _preimage?: PreImage): DerivedReferendum | null {
const preImage = _preimage?.isSome
? _preimage.unwrap()
: null;
const info = (_preimage && _info.unwrapOr(null)) || null;
return new Option<ReferendumInfoExtended>(
api.registry,
ReferendumInfoExtended,
isNull(info)
? null
: new ReferendumInfoExtended(api.registry, info, index)
if (!info || !preImage) {
return null;
}
return {
index: createType(api.registry, 'PropIndex', index),
info,
hash: info.proposalHash,
proposal: createType(api.registry, 'Proposal', preImage[0].toU8a(true)),
preimage: {
at: preImage[3],
balance: preImage[2],
proposer: preImage[1]
}
};
}
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo>): Observable<DerivedReferendum | null> {
return ((
info?.isSome
? api.query.democracy.preimages<PreImage>(info.unwrap().proposalHash)
: of(undefined)
) as Observable<PreImage | undefined>).pipe(
map((preimage?: PreImage): DerivedReferendum | null =>
constructInfo(api, index, info, preimage)
)
);
}
export function referendumInfo (api: ApiInterfaceRx): (index: BN | number) => Observable<Option<ReferendumInfoExtended>> {
return memo((index: BN | number): Observable<Option<ReferendumInfoExtended>> =>
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(
map((optionInfo): Option<ReferendumInfoExtended> =>
constructInfo(api, index, optionInfo)
switchMap((info): Observable<DerivedReferendum | null> =>
retrieveInfo(api, index, info)
)
));
)
);
}
@@ -3,28 +3,33 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, Vec } from '@polkadot/types';
import { ReferendumInfoExtended } from '../type';
import { memo } from '../util';
import { constructInfo } from './referendumInfo';
import { retrieveInfo } from './referendumInfo';
export function referendumInfos (api: ApiInterfaceRx): (ids?: (BN | number)[]) => Observable<Option<ReferendumInfoExtended>[]> {
return memo((ids: (BN | number)[] = []): Observable<Option<ReferendumInfoExtended>[]> =>
export function referendumInfos (api: ApiInterfaceRx): (ids?: (BN | number)[]) => Observable<DerivedReferendum[]> {
return memo((ids: (BN | number)[] = []): Observable<DerivedReferendum[]> =>
(
!ids || !ids.length
? of([] as Option<ReferendumInfo>[])
: api.query.democracy.referendumInfoOf.multi(ids) as Observable<Vec<Option<ReferendumInfo>>>
).pipe(
map((infos): Option<ReferendumInfoExtended>[] =>
ids.map((id, index): Option<ReferendumInfoExtended> =>
constructInfo(api, id, infos[index])
switchMap((infos): Observable<(DerivedReferendum | null)[]> =>
combineLatest(
...ids.map((id, index): Observable<DerivedReferendum | null> =>
retrieveInfo(api, id, infos[index])
)
)
),
map((infos): DerivedReferendum[] =>
infos.filter((referendum): boolean => !!referendum) as DerivedReferendum[]
)
));
}
@@ -3,23 +3,22 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { ReferendumInfoExtended } from '../type';
import { memo } from '../util';
export function referendums (api: ApiInterfaceRx): () => Observable<Option<ReferendumInfoExtended>[]> {
return memo((): Observable<Option<ReferendumInfoExtended>[]> =>
export function referendums (api: ApiInterfaceRx): () => Observable<DerivedReferendum[]> {
return memo((): Observable<DerivedReferendum[]> =>
api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
api.query.democracy.nextTally,
api.query.democracy.referendumCount
]).pipe(
switchMap(([nextTally, referendumCount]): Observable<Option<ReferendumInfoExtended>[]> =>
switchMap(([nextTally, referendumCount]): Observable<DerivedReferendum[]> =>
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
? api.derive.democracy.referendumInfos(
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i): BN =>
+26 -2
View File
@@ -2,10 +2,10 @@
// 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, BlockNumber, EraIndex, EraPoints, Exposure, Index, Keys, RewardDestination, SessionIndex, SetIndex, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types/interfaces';
import { AccountId, AccountIndex, Balance, BalanceLock, BlockNumber, EraIndex, EraPoints, Exposure, Hash, Index, Keys, Proposal, PropIndex, ReferendumInfo, RewardDestination, SessionIndex, SetIndex, StakingLedger, ValidatorPrefs, Vote, VoteIndex } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { u32 } from '@polkadot/types';
import { u32, Vec } from '@polkadot/types';
export type AccountIndexes = Record<string, AccountIndex>;
@@ -79,6 +79,30 @@ export interface RecentlyOffline {
count: BN;
}
export interface DeriveProposalPreImage {
at: BlockNumber;
balance: Balance;
proposer: AccountId;
}
export interface DeriveProposal {
balance?: Balance;
hash: Hash;
index: PropIndex;
preimage: DeriveProposalPreImage;
proposal: Proposal;
proposer: AccountId;
seconds: Vec<AccountId>;
}
export interface DerivedReferendum {
hash: Hash;
index: PropIndex;
info: ReferendumInfo;
preimage: DeriveProposalPreImage;
proposal: Proposal;
}
export type DerivedRecentlyOffline = Record<string, RecentlyOffline[]>;
export interface DerivedReferendumVote {