From 2ac36409004608325e2ec34b4c33e14bbfb50219 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 7 Feb 2020 15:43:21 +0100 Subject: [PATCH] Update AccountIndex derive (#1820) --- packages/api-derive/src/accounts/indexToId.ts | 48 ++++++++++----- packages/api-derive/src/accounts/indexes.ts | 61 +++++++++++-------- 2 files changed, 68 insertions(+), 41 deletions(-) diff --git a/packages/api-derive/src/accounts/indexToId.ts b/packages/api-derive/src/accounts/indexToId.ts index 5641173a47..65ad27455a 100644 --- a/packages/api-derive/src/accounts/indexToId.ts +++ b/packages/api-derive/src/accounts/indexToId.ts @@ -2,16 +2,39 @@ // 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 } from '@polkadot/types/interfaces'; +import { AccountId, AccountIndex, BalanceOf } from '@polkadot/types/interfaces'; +import { ITuple } from '@polkadot/types/types'; import { Observable, of } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { ENUMSET_SIZE } from '@polkadot/types/primitive/Generic/AccountIndex'; -import { ClassOf, Vec, createType } from '@polkadot/types'; +import { ClassOf, Option, Vec, createType } from '@polkadot/types'; import { memo } from '../util'; +function queryEnumSet (api: ApiInterfaceRx, _accountIndex: AccountIndex | string): Observable { + const accountIndex = _accountIndex instanceof ClassOf(api.registry, 'AccountIndex') + ? _accountIndex + : createType(api.registry, 'AccountIndex', _accountIndex); + + return api.query.indices.enumSet>(accountIndex.div(ENUMSET_SIZE)).pipe( + startWith([]), + map((accounts): AccountId | undefined => + (accounts || [])[accountIndex.mod(ENUMSET_SIZE).toNumber()] + ) + ); +} + +// current +function query (api: ApiInterfaceRx, accountIndex: AccountIndex | string): Observable { + return api.query.indices.accounts>>(accountIndex).pipe( + map((optResult): AccountId | undefined => + optResult.unwrapOr([])[0] + ) + ); +} + /** * @name indexToId * @param {( AccountIndex | string )} accountIndex - An accounts index in different formats. @@ -26,18 +49,11 @@ import { memo } from '../util'; * ``` */ export function indexToId (api: ApiInterfaceRx): (accountIndex: AccountIndex | string) => Observable { - return memo((_accountIndex: AccountIndex | string): Observable => { - const accountIndex = _accountIndex instanceof ClassOf(api.registry, 'AccountIndex') - ? _accountIndex - : createType(api.registry, 'AccountIndex', _accountIndex); - - return api.query.indices.enumSet - ? api.query.indices.enumSet>(accountIndex.div(ENUMSET_SIZE)).pipe( - startWith([]), - map((accounts): AccountId | undefined => - (accounts || [])[accountIndex.mod(ENUMSET_SIZE).toNumber()] - ) - ) - : of(undefined); - }); + return memo((accountIndex: AccountIndex | string): Observable => + api.query.indices + ? api.query.indices.accounts + ? query(api, accountIndex) + : queryEnumSet(api, accountIndex) + : of(undefined) + ); } diff --git a/packages/api-derive/src/accounts/indexes.ts b/packages/api-derive/src/accounts/indexes.ts index 607b8b7f57..0282329ecb 100644 --- a/packages/api-derive/src/accounts/indexes.ts +++ b/packages/api-derive/src/accounts/indexes.ts @@ -15,6 +15,36 @@ import { memo } from '../util'; const enumsetSize = ENUMSET_SIZE.toNumber(); +function queryEnumSet (api: ApiInterfaceRx): Observable { + 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 + // specific index - each query can return up to ENUMSET_SIZE (64) records, each + // containing an AccountId + switchMap((next: AccountIndex): Observable[]> => + api.query.indices.enumSet.multi>([...Array(next.toNumber() + 1).keys()]) + ), + map((all: AccountId[][]): AccountIndexes => + all.reduce((result: AccountIndexes, list, outerIndex): AccountIndexes => { + (list || []).forEach((accountId, innerIndex): void => { + // re-create the index based on position 0 is [0][0] and likewise + // 64 (0..63 in first) is [1][0] (the first index value in set 2) + const index = (outerIndex * enumsetSize) + innerIndex; + + result[accountId.toString()] = createType(api.registry, 'AccountIndex', index); + }); + + return result; + }, {}) + ) + ); +} + +// TODO Reverse lookup (via query keys) +function query (): Observable { + return of({} as AccountIndexes); +} + /** * @name indexes * @returns Returns all the indexes on the system. @@ -32,29 +62,10 @@ const enumsetSize = ENUMSET_SIZE.toNumber(); */ export function indexes (api: ApiInterfaceRx): () => Observable { return memo((): Observable => - (api.query.indices.nextEnumSet - ? 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 - // specific index - each query can return up to ENUMSET_SIZE (64) records, each - // containing an AccountId - switchMap((next: AccountIndex): Observable[]> => - api.query.indices.enumSet.multi>([...Array(next.toNumber() + 1).keys()]) - ) - ) - : of([] as AccountId[][]) - ).pipe( - map((all: AccountId[][]): AccountIndexes => - all.reduce((result: AccountIndexes, list, outerIndex): AccountIndexes => { - (list || []).forEach((accountId, innerIndex): void => { - // re-create the index based on position 0 is [0][0] and likewise - // 64 (0..63 in first) is [1][0] (the first index value in set 2) - const index = (outerIndex * enumsetSize) + innerIndex; - - result[accountId.toString()] = createType(api.registry, 'AccountIndex', index); - }); - - return result; - }, {})) - )); + api.query.indices + ? api.query.indices.accounts + ? query() + : queryEnumSet(api) + : of({} as AccountIndexes) + ); }