Files
api/packages/api-derive/src/accounts/indexToId.ts
T
Jaco 9fe2798b70 Use .js imports in src (#5526)
* Use .js imports in src

* Adjust

* Fix imports

* Bump dev

* Adjust with .js imports for typegen

* Adjust

* Adjust

* Remove target override
2023-03-09 12:19:12 +02:00

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)
);
}