|
| 1 | +// Copyright 2017-2020 @polkadot/api-derive authors & contributors |
| 2 | +// This software may be modified and distributed under the terms |
| 3 | +// of the Apache-2.0 license. See the LICENSE file for details. |
| 4 | + |
| 5 | +import { CollatorId, ParaId } from '@polkadot/types/interfaces'; |
| 6 | +import { DeriveParachainInfo, DeriveParachainFull, DeriveParachainActive } from '../types'; |
| 7 | +import { Active, DidUpdate, Heads, ParaInfoResult, PendingSwap, RelayDispatchQueue, RetryQueue, SelectedThreads, Watermarks } from './types'; |
| 8 | + |
| 9 | +import { Observable, of } from 'rxjs'; |
| 10 | +import { map } from 'rxjs/operators'; |
| 11 | +import { ApiInterfaceRx } from '@polkadot/api/types'; |
| 12 | +import { createType } from '@polkadot/types'; |
| 13 | + |
| 14 | +import { memo } from '../util'; |
| 15 | + |
| 16 | +type Result = [ |
| 17 | + Active, |
| 18 | + RetryQueue, |
| 19 | + SelectedThreads, |
| 20 | + DidUpdate, |
| 21 | + ParaInfoResult, |
| 22 | + PendingSwap, |
| 23 | + Heads, |
| 24 | + RelayDispatchQueue, |
| 25 | + Watermarks |
| 26 | +]; |
| 27 | + |
| 28 | +function parseActive (id: ParaId, active: Active): DeriveParachainActive | null { |
| 29 | + const found = active.find(([paraId]): boolean => paraId === id); |
| 30 | + |
| 31 | + if (found && found[1].isSome) { |
| 32 | + const [collatorId, retriable] = found[1].unwrap(); |
| 33 | + |
| 34 | + return { |
| 35 | + collatorId, |
| 36 | + ...( |
| 37 | + retriable.isWithRetries |
| 38 | + ? { |
| 39 | + isRetriable: true, |
| 40 | + retries: retriable.asWithRetries.toNumber() |
| 41 | + } |
| 42 | + : { |
| 43 | + isRetriable: false, |
| 44 | + retries: 0 |
| 45 | + } |
| 46 | + ) |
| 47 | + }; |
| 48 | + } |
| 49 | + return null; |
| 50 | +} |
| 51 | + |
| 52 | +function parseCollators (id: ParaId, collatorQueue: SelectedThreads | RetryQueue): (CollatorId | null)[] { |
| 53 | + return collatorQueue |
| 54 | + .map((queue): CollatorId | null => { |
| 55 | + const found = queue.find(([paraId]): boolean => paraId === id); |
| 56 | + |
| 57 | + return found ? found[1] : null; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function parse (id: ParaId, [active, retryQueue, selectedThreads, didUpdate, info, pendingSwap, heads, relayDispatchQueue, watermarks]: Result): DeriveParachainFull { |
| 62 | + return { |
| 63 | + active: parseActive(id, active), |
| 64 | + didUpdate: didUpdate.isSome |
| 65 | + ? !!didUpdate.unwrap().some((paraId): boolean => paraId.eq(id)) |
| 66 | + : false, |
| 67 | + heads, |
| 68 | + id, |
| 69 | + info: { id, ...info.unwrapOr(null) } as DeriveParachainInfo, |
| 70 | + pendingSwapId: pendingSwap.unwrapOr(null), |
| 71 | + relayDispatchQueue, |
| 72 | + retryCollators: parseCollators(id, retryQueue), |
| 73 | + selectedCollators: parseCollators(id, selectedThreads), |
| 74 | + watermark: watermarks.unwrapOr(null) |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +export function info (api: ApiInterfaceRx): (id: ParaId | number) => Observable<DeriveParachainFull | null> { |
| 79 | + return memo((id: ParaId | number): Observable<DeriveParachainFull | null> => |
| 80 | + api.query.registrar && api.query.parachains |
| 81 | + ? api.queryMulti<Result>([ |
| 82 | + api.query.registrar.active, |
| 83 | + api.query.registrar.retryQueue, |
| 84 | + api.query.registrar.selectedThreads, |
| 85 | + api.query.parachains.didUpdate, |
| 86 | + [api.query.registrar.paras, id], |
| 87 | + [api.query.registrar.pendingSwap, id], |
| 88 | + [api.query.parachains.heads, id], |
| 89 | + [api.query.parachains.relayDispatchQueue, id], |
| 90 | + [api.query.parachains.watermarks, id] |
| 91 | + ]) |
| 92 | + .pipe( |
| 93 | + map((result: Result): DeriveParachainFull => |
| 94 | + parse(createType(api.registry, 'ParaId', id), result) |
| 95 | + ) |
| 96 | + ) |
| 97 | + : of(null) |
| 98 | + ); |
| 99 | +} |
0 commit comments