diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index fc2a70fd25..f4e49ad2a0 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -46,6 +46,7 @@ const deriveAvail: Record = { democracy: ['democracy'], elections: ['electionsPhragmen', 'elections'], imOnline: ['imOnline'], + parachains: ['parachains', 'registrar'], session: ['session'], society: ['society'], staking: ['staking'], diff --git a/packages/api-derive/src/parachains/all.ts b/packages/api-derive/src/parachains/all.ts deleted file mode 100644 index 45ac5e0255..0000000000 --- a/packages/api-derive/src/parachains/all.ts +++ /dev/null @@ -1,120 +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 { 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; -type PendingSwap = Option; -type Active = ITuple<[ParaId, Option>]>[]; -type RetryQueue = ITuple<[ParaId, CollatorId]>[][] -type SelectedThreads = ITuple<[ParaId, CollatorId]>[][]; -type Heads = Bytes; -type RelayDispatchQueue = Vec -type Watermarks = Option; -type DidUpdate = Option>; - -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 { - return memo((): Observable => - api.query.registrar?.parachains && api.query.parachains - ? api.query.registrar.parachains().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(paraIds), - api.query.registrar.pendingSwap.multi(paraIds), - api.query.parachains.heads.multi(paraIds), - api.query.parachains.relayDispatchQueue.multi(paraIds), - api.query.parachains.watermarks.multi(paraIds) - ]) as Observable - ), - map((result: Result): DeriveParachain[] => - parse(result) - ) - ) - : of([] as DeriveParachain[]) - ); -} diff --git a/packages/api-derive/src/parachains/index.ts b/packages/api-derive/src/parachains/index.ts index 2992bebd69..4fad73b16d 100644 --- a/packages/api-derive/src/parachains/index.ts +++ b/packages/api-derive/src/parachains/index.ts @@ -2,4 +2,5 @@ // 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'; +export * from './info'; +export * from './overview'; diff --git a/packages/api-derive/src/parachains/info.ts b/packages/api-derive/src/parachains/info.ts new file mode 100644 index 0000000000..26cc9b34ee --- /dev/null +++ b/packages/api-derive/src/parachains/info.ts @@ -0,0 +1,99 @@ +// 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 { CollatorId, ParaId } from '@polkadot/types/interfaces'; +import { DeriveParachainInfo, DeriveParachainFull, DeriveParachainActive } from '../types'; +import { Active, DidUpdate, Heads, ParaInfoResult, PendingSwap, RelayDispatchQueue, RetryQueue, SelectedThreads, Watermarks } from './types'; + +import { Observable, of } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { ApiInterfaceRx } from '@polkadot/api/types'; +import { createType } from '@polkadot/types'; + +import { memo } from '../util'; + +type Result = [ + 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 (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue, watermarks]: Result): DeriveParachainFull { + return { + active: parseActive(id, active), + didUpdate: didUpdate.isSome + ? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id)) + : false, + heads, + id, + info: { id, ...info.unwrapOr(null) } as DeriveParachainInfo, + pendingSwapId: pendingSwap.unwrapOr(null), + relayDispatchQueue, + retryCollators: parseCollators(id, retryQueue), + selectedCollators: parseCollators(id, selectedThreads), + watermark: watermarks.unwrapOr(null) + }; +} + +export function info (api: ApiInterfaceRx): (id: ParaId | number) => Observable { + return memo((id: ParaId | number): Observable => + api.query.registrar && api.query.parachains + ? api.queryMulti([ + api.query.registrar.active, + api.query.registrar.retryQueue, + api.query.registrar.selectedThreads, + api.query.parachains.didUpdate, + [api.query.registrar.paras, id], + [api.query.registrar.pendingSwap, id], + [api.query.parachains.heads, id], + [api.query.parachains.relayDispatchQueue, id], + [api.query.parachains.watermarks, id] + ]) + .pipe( + map((result: Result): DeriveParachainFull => + parse(createType(api.registry, 'ParaId', id), result) + ) + ) + : of(null) + ); +} diff --git a/packages/api-derive/src/parachains/overview.ts b/packages/api-derive/src/parachains/overview.ts new file mode 100644 index 0000000000..4681cb4a1e --- /dev/null +++ b/packages/api-derive/src/parachains/overview.ts @@ -0,0 +1,59 @@ +// 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 { ParaId } from '@polkadot/types/interfaces'; +import { DeriveParachain, DeriveParachainInfo } from '../types'; +import { DidUpdate, ParaInfoResult, PendingSwap, RelayDispatchQueueSize, Watermarks } from './types'; + +import { Observable, combineLatest, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import { ApiInterfaceRx } from '@polkadot/api/types'; + +import { memo } from '../util'; + +type Result = [ + ParaId[], + DidUpdate, + ParaInfoResult[], + PendingSwap[], + RelayDispatchQueueSize[], + Watermarks[], +]; + +function parse ([ids, didUpdate, infos, pendingSwaps, relayDispatchQueueSizes, watermarks]: Result): DeriveParachain[] { + return ids.map((id, index): DeriveParachain => { + return { + didUpdate: didUpdate.isSome + ? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id)) + : false, + id, + info: { id, ...infos[index].unwrapOr(null) } as DeriveParachainInfo, + pendingSwapId: pendingSwaps[index].unwrapOr(null), + relayDispatchQueueSize: relayDispatchQueueSizes[index][0].toNumber(), + watermark: watermarks[index].unwrapOr(null) + }; + }); +} + +export function overview (api: ApiInterfaceRx): () => Observable { + return memo((): Observable => + api.query.registrar?.parachains && api.query.parachains + ? api.query.registrar.parachains().pipe( + switchMap((paraIds) => + combineLatest([ + of(paraIds), + api.query.parachains.didUpdate(), + api.query.registrar.paras.multi(paraIds), + api.query.registrar.pendingSwap.multi(paraIds), + api.query.parachains.relayDispatchQueueSize.multi(paraIds), + api.query.parachains.watermarks.multi(paraIds) + ]) + ), + map((result: Result): DeriveParachain[] => + parse(result) + ) + ) + : of([] as DeriveParachain[]) + ); +} diff --git a/packages/api-derive/src/parachains/types.ts b/packages/api-derive/src/parachains/types.ts new file mode 100644 index 0000000000..fc7ff08cd0 --- /dev/null +++ b/packages/api-derive/src/parachains/types.ts @@ -0,0 +1,20 @@ +// 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 { Bytes, Option, u32, Vec } from '@polkadot/types'; + +export type ParaInfoResult = Option; +export type PendingSwap = Option; +export type Active = Vec>]>>; +export type RetryQueue = Vec>> +export type SelectedThreads = Vec>> +export type Code = Bytes; +export type Heads = Bytes; +export type RelayDispatchQueue = Vec +export type RelayDispatchQueueSize = ITuple<[u32, u32]>; +export type Watermarks = Option; +export type DidUpdate = Option>; diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index 0ae552572b..d7169ab4e2 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -114,17 +114,28 @@ export interface DeriveParachainActive { retries: number; } +export interface DeriveParachainInfo extends ParaInfo { + id: ParaId; + icon?: string; + name?: string; + owner?: string; +} + export interface DeriveParachain { - active: DeriveParachainActive | null; didUpdate: boolean; pendingSwapId: ParaId | null; - heads: Bytes | null; id: ParaId; - info: ParaInfo | null; + info: DeriveParachainInfo | null; + relayDispatchQueueSize?: number; + watermark: BlockNumber | null; +} + +export interface DeriveParachainFull extends DeriveParachain { + active: DeriveParachainActive | null; + heads: Bytes | null; relayDispatchQueue: UpwardMessage[]; retryCollators: (CollatorId | null)[]; selectedCollators: (CollatorId | null)[]; - watermark: BlockNumber | null; } export interface DeriveProposalPreImage {