derive.accounts.info returns accountId/Index (#1533)

* derive.accounts.info returns accountId/Index

* Swap all idAndIndex to account.info
This commit is contained in:
Jaco Greeff
2019-10-31 10:48:29 +01:00
committed by GitHub
parent 2f350e3bc2
commit 453aacb669
4 changed files with 37 additions and 31 deletions
+26 -22
View File
@@ -3,39 +3,43 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Balance } from '@polkadot/types/interfaces';
import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import { DeriveAccountInfo } from '../types';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { map, switchMap } from 'rxjs/operators';
import { Bytes, Option, u32 } from '@polkadot/types';
import { u8aToString } from '@polkadot/util';
import { drr } from '../util/drr';
type Result = Option<[Bytes, Balance] & Codec>;
function formatNickname (api: ApiInterfaceRx, result: Result): string | undefined {
return result.isSome
? u8aToString(result.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber())
: undefined;
}
import { idAndIndex } from './idAndIndex';
/**
* @name info
* @description Returns aux. info with regards to an account, current that includes the nickname
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
*/
export function info (api: ApiInterfaceRx): (address?: AccountId | string | null) => Observable<DeriveAccountInfo> {
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
const idAndIndexCall = idAndIndex(api);
// TODO We would really like to pass in an Address or AccountIndex here as well
return (address?: AccountId | string | null): Observable<DeriveAccountInfo> => {
return address && api.query.nicks
? api.query.nicks.nameOf<Result>(address).pipe(
map((result): DeriveAccountInfo => ({
nickname: formatNickname(api, result)
})),
drr()
)
: of({} as DeriveAccountInfo);
};
return (address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
idAndIndexCall(address).pipe(
switchMap(([accountId, accountIndex]): Observable<DeriveAccountInfo> =>
accountId && api.query.nicks
? api.query.nicks
.nameOf<Option<[Bytes, Balance] & Codec>>(accountId)
.pipe(
map((result): DeriveAccountInfo => ({
accountId,
accountIndex,
nickname: result.isSome
? u8aToString(result.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber())
: undefined
}))
)
: of({ accountId, accountIndex })
),
drr()
);
}