diff --git a/packages/api-derive/src/accounts/idToIndex.ts b/packages/api-derive/src/accounts/idToIndex.ts index 000e0e08eb..84f6470b12 100644 --- a/packages/api-derive/src/accounts/idToIndex.ts +++ b/packages/api-derive/src/accounts/idToIndex.ts @@ -27,12 +27,11 @@ import { drr } from '../util/drr'; */ export function idToIndex (api: ApiInterfaceRx): (accountId: AccountId | string) => Observable { return (accountId: AccountId | string): Observable => - indexes(api)() - .pipe( - startWith({}), - map((indexes: AccountIndexes): AccountIndex | undefined => - (indexes || {})[accountId.toString()] - ), - drr() - ); + indexes(api)().pipe( + startWith({}), + map((indexes: AccountIndexes): AccountIndex | undefined => + (indexes || {})[accountId.toString()] + ), + drr() + ); } diff --git a/packages/api-derive/src/accounts/indexToId.ts b/packages/api-derive/src/accounts/indexToId.ts index 5118813bd0..5da8eb6a36 100644 --- a/packages/api-derive/src/accounts/indexToId.ts +++ b/packages/api-derive/src/accounts/indexToId.ts @@ -32,7 +32,8 @@ export function indexToId (api: ApiInterfaceRx): (accountIndex: AccountIndex | s ? _accountIndex : createType('AccountIndex', _accountIndex); - return (querySection.enumSet>(accountIndex.div(ENUMSET_SIZE))) + return querySection + .enumSet>(accountIndex.div(ENUMSET_SIZE)) .pipe( startWith([]), map((accounts): AccountId | undefined => diff --git a/packages/api-derive/src/accounts/indexes.ts b/packages/api-derive/src/accounts/indexes.ts index adbfd87342..a8fb4d095d 100644 --- a/packages/api-derive/src/accounts/indexes.ts +++ b/packages/api-derive/src/accounts/indexes.ts @@ -33,7 +33,8 @@ const enumsetSize = ENUMSET_SIZE.toNumber(); */ export function indexes (api: ApiInterfaceRx): () => Observable { return (): Observable => { - return (api.query.indices.nextEnumSet()) + return api.query.indices + .nextEnumSet() .pipe( // use the nextEnumSet (which is a counter of the number of sets) to construct // a range of values to query [0, 1, 2, ...]. Retrieve the full enum set for the diff --git a/packages/api-derive/src/imOnline/receivedHeartbeats.ts b/packages/api-derive/src/imOnline/receivedHeartbeats.ts index 4178d6e5f3..440715842d 100644 --- a/packages/api-derive/src/imOnline/receivedHeartbeats.ts +++ b/packages/api-derive/src/imOnline/receivedHeartbeats.ts @@ -3,8 +3,9 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { AccountId, SessionIndex } from '@polkadot/types/interfaces'; +import { DerivedHeartbeats } from '../types'; -import { of, Observable } from 'rxjs'; +import { of, Observable, combineLatest } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Bytes, Vec } from '@polkadot/types'; @@ -14,25 +15,29 @@ import { drr } from '../util/drr'; /** * @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session */ -export function receivedHeartbeats (api: ApiInterfaceRx): (addresses: (AccountId | string)[]) => Observable { - return (addresses: (AccountId | string)[]): Observable => { +export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable { + return (): Observable => { return api.query.imOnline && api.query.imOnline.receivedHeartbeats - ? api.queryMulti<[SessionIndex, Vec]>([ - api.query.session.currentIndex, - api.query.imOnline.keys + ? api.queryMulti<[Vec, SessionIndex]>([ + api.query.imOnline.keys, + api.query.session.currentIndex ]).pipe( - switchMap(([currentIndex, keys]): Observable => - api.query.imOnline.receivedHeartbeats.multi( - addresses.map((address): [SessionIndex, number] => - [currentIndex, keys.indexOf(address)] + switchMap(([keys, currentIndex]): Observable<[AccountId[], Bytes[]]> => + combineLatest([ + of(keys), + api.query.imOnline.receivedHeartbeats.multi( + keys.map((_address, index): [SessionIndex, number] => [currentIndex, index]) ) - ) + ]) ), - map((heartbeats): boolean[] => - heartbeats.map((heartbeat): boolean => !heartbeat.isEmpty) + map(([keys, heartbeats]): DerivedHeartbeats => + keys.reduce((result: DerivedHeartbeats, key, index): DerivedHeartbeats => ({ + ...result, + [key.toString()]: !!heartbeats[index] && !heartbeats[index].isEmpty + }), {}) ), drr() ) - : of([...new Array(addresses.length).keys()].map((): boolean => false)); + : of({}); }; } diff --git a/packages/api-derive/src/types.ts b/packages/api-derive/src/types.ts index d27a133f0a..70defb5365 100644 --- a/packages/api-derive/src/types.ts +++ b/packages/api-derive/src/types.ts @@ -56,6 +56,8 @@ export interface DerivedFees { transferFee: Balance; } +export type DerivedHeartbeats = Record; + export interface RecentlyOffline { blockNumber: BlockNumber; count: BN;