* Use .js imports in src * Adjust * Fix imports * Bump dev * Adjust with .js imports for typegen * Adjust * Adjust * Remove target override
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
// Copyright 2017-2023 @polkadot/api-derive authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Observable } from 'rxjs';
|
|
import type { AccountId, AccountIndex } from '@polkadot/types/interfaces';
|
|
import type { DeriveApi } from '../types.js';
|
|
|
|
import { map, of } from 'rxjs';
|
|
|
|
import { memo } from '../util/index.js';
|
|
|
|
/**
|
|
* @name indexToId
|
|
* @param {( AccountIndex | string )} accountIndex - An accounts index in different formats.
|
|
* @returns Returns the corresponding AccountId.
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* api.derive.accounts.indexToId('F7Hs', (accountId) => {
|
|
* console.log(`The AccountId of F7Hs is ${accountId}`);
|
|
* });
|
|
* ```
|
|
*/
|
|
export function indexToId (instanceId: string, api: DeriveApi): (accountIndex: AccountIndex | string) => Observable<AccountId | undefined> {
|
|
return memo(instanceId, (accountIndex: AccountIndex | string): Observable<AccountId | undefined> =>
|
|
api.query.indices
|
|
? api.query.indices.accounts(accountIndex).pipe(
|
|
map((optResult): AccountId | undefined =>
|
|
optResult.unwrapOr([])[0]
|
|
)
|
|
)
|
|
: of(undefined)
|
|
);
|
|
}
|