Add api.derive.registrar.parachains (#1856)

* api.derive.registrar.parachains

* remove api parse

* suggestions

* some

* Update packages/api-derive/src/parachains/all.ts

* Update packages/api-derive/src/index.ts

* Update packages/api-derive/src/index.ts

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
Keith Ingram
2020-02-15 08:52:40 +01:00
committed by GitHub
co-authored by Jaco Greeff
parent ae350aae6f
commit ee622f3d2b
4 changed files with 148 additions and 3 deletions
+2 -1
View File
@@ -15,6 +15,7 @@ import * as council from './council';
import * as democracy from './democracy'; import * as democracy from './democracy';
import * as elections from './elections'; import * as elections from './elections';
import * as imOnline from './imOnline'; import * as imOnline from './imOnline';
import * as parachains from './parachains';
import * as session from './session'; import * as session from './session';
import * as society from './society'; import * as society from './society';
import * as staking from './staking'; import * as staking from './staking';
@@ -23,7 +24,7 @@ import * as treasury from './treasury';
export * from './type'; export * from './type';
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, society, staking, technicalCommittee, treasury }; export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury };
type DeriveSection<Section> = { type DeriveSection<Section> = {
[Method in keyof Section]: Section[Method] extends AnyFunction [Method in keyof Section]: Section[Method] extends AnyFunction
+120
View File
@@ -0,0 +1,120 @@
// 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 { BlockNumber, CollatorId, ParaId, ParaInfo, Retriable, UpwardMessage } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveParachain, DeriveParachainActive } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Option, Vec } from '@polkadot/types';
import { memo } from '../util';
type ParaIds = ParaId[];
type ParaInfoResult = Option<ParaInfo>;
type PendingSwap = Option<ParaId>;
type Active = ITuple<[ParaId, Option<ITuple<[CollatorId, Retriable]>>]>[];
type RetryQueue = ITuple<[ParaId, CollatorId]>[][]
type SelectedThreads = ITuple<[ParaId, CollatorId]>[][];
type Heads = Bytes;
type RelayDispatchQueue = Vec<UpwardMessage>
type Watermarks = Option<BlockNumber>;
type DidUpdate = Option<Vec<ParaId>>;
type Result = [
ParaId[],
[
Active,
RetryQueue,
SelectedThreads,
DidUpdate
],
ParaInfoResult[],
PendingSwap[],
Heads[],
RelayDispatchQueue[],
Watermarks[],
];
function parseActive (id: ParaId, active: Active): DeriveParachainActive | null {
const found = active.find(([paraId]): boolean => paraId === id);
if (found && found[1].isSome) {
const [collatorId, retriable] = found[1].unwrap();
return {
collatorId,
...(
retriable.isWithRetries
? {
isRetriable: true,
retries: retriable.asWithRetries.toNumber()
}
: {
isRetriable: false,
retries: 0
}
)
};
}
return null;
}
function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] {
return collatorQueue
.map((queue): CollatorId | null => {
const found = queue.find(([paraId]): boolean => paraId === id);
return found ? found[1] : null;
});
}
function parse ([ids, [active, retryQueue, selectedThreads, didUpdate], infos, pendingSwaps, heads, relayDispatchQueues, watermarks]: Result): DeriveParachain[] {
return ids.map((id, index): DeriveParachain => {
return {
active: parseActive(id, active),
didUpdate: didUpdate.isSome
? didUpdate.unwrap().some((paraId): boolean => paraId === id)
: false,
heads: heads[index],
id,
info: infos[index].unwrapOr(null),
pendingSwapId: pendingSwaps[index].unwrapOr(null),
relayDispatchQueue: relayDispatchQueues[index],
retryCollators: parseCollators(id, retryQueue),
selectedCollators: parseCollators(id, selectedThreads),
watermark: watermarks[index].unwrapOr(null)
};
});
}
export function all (api: ApiInterfaceRx): () => Observable<DeriveParachain[]> {
return memo((): Observable<DeriveParachain[]> =>
api.query.registrar?.parachains && api.query.parachains
? api.query.registrar.parachains<ParaIds>().pipe(
switchMap((paraIds) =>
combineLatest([
of(paraIds),
api.queryMulti([
api.query.registrar.active,
api.query.registrar.retryQueue,
api.query.registrar.selectedThreads,
api.query.parachains.didUpdate
]),
api.query.registrar.paras.multi<ParaInfo>(paraIds),
api.query.registrar.pendingSwap.multi<PendingSwap>(paraIds),
api.query.parachains.heads.multi<Heads>(paraIds),
api.query.parachains.relayDispatchQueue.multi<RelayDispatchQueue>(paraIds),
api.query.parachains.watermarks.multi<Watermarks>(paraIds)
]) as Observable<Result>
),
map((result: Result): DeriveParachain[] =>
parse(result)
)
)
: of([] as DeriveParachain[])
);
}
@@ -0,0 +1,5 @@
// 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.
export * from './all';
+21 -2
View File
@@ -2,10 +2,10 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, EraIndex, EraPoints, Exposure, Hash, Index, Keys, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, RewardDestination, SessionIndex, SetIndex, SocietyVote, StakingLedger, StrikeCount, TreasuryProposal, ValidatorPrefs, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces'; import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, CollatorId, EraIndex, EraPoints, Exposure, Hash, Index, Keys, ParaId, ParaInfo, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, RewardDestination, SessionIndex, SetIndex, SocietyVote, StakingLedger, StrikeCount, TreasuryProposal, UpwardMessage, ValidatorPrefs, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces';
import BN from 'bn.js'; import BN from 'bn.js';
import { u32, Vec } from '@polkadot/types'; import { Bytes, u32, Vec } from '@polkadot/types';
export type AccountIndexes = Record<string, AccountIndex>; export type AccountIndexes = Record<string, AccountIndex>;
@@ -107,6 +107,25 @@ export interface RecentlyOffline {
count: BN; count: BN;
} }
export interface DeriveParachainActive {
collatorId: CollatorId;
isRetriable: boolean;
retries: number;
}
export interface DeriveParachain {
active: DeriveParachainActive | null;
didUpdate: boolean;
pendingSwapId: ParaId | null;
heads: Bytes | null;
id: ParaId;
info: ParaInfo | null;
relayDispatchQueue: UpwardMessage[];
retryCollators: (CollatorId | null)[];
selectedCollators: (CollatorId | null)[];
watermark: BlockNumber | null;
}
export interface DeriveProposalPreImage { export interface DeriveProposalPreImage {
at: BlockNumber; at: BlockNumber;
balance: Balance; balance: Balance;