derive.balances.* handle Address & AccountIndex everywhere (#639)

This commit is contained in:
Jaco Greeff
2019-01-28 11:22:11 +01:00
committed by GitHub
parent 22897fa3c2
commit bfe5661d8f
6 changed files with 27 additions and 20 deletions
@@ -6,7 +6,8 @@ import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { decodeAddress } from '@polkadot/keyring';
import { AccountId, AccountIndex } from '@polkadot/types/index';
import { AccountId, AccountIndex, Address } from '@polkadot/types/index';
import { isU8a } from '@polkadot/util';
import { idToIndex } from './idToIndex';
import { indexToId } from './indexToId';
@@ -15,10 +16,12 @@ import { drr } from '../util/drr';
export type AccountIdAndIndex = [AccountId?, AccountIndex?];
export function idAndIndex (api: ApiInterface$Rx) {
return (address?: AccountId | AccountIndex | string | null): Observable<AccountIdAndIndex> => {
return (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountIdAndIndex> => {
try {
// yes, this can fail, don't care too much, catch will catch it
const decoded = decodeAddress(address as any);
const decoded = isU8a(address)
? address
: decodeAddress((address || '').toString());
if (decoded.length === 32) {
const accountId = new AccountId(decoded);
@@ -6,7 +6,7 @@ import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Balance } from '@polkadot/types/index';
import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/index';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
@@ -14,7 +14,7 @@ import { votingBalance } from './votingBalance';
import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor';
export function validatingBalance (api: ApiInterface$Rx) {
return (address: AccountId | string): Observable<DerivedBalances> => {
return (address: AccountId | AccountIndex | Address | string): Observable<DerivedBalances> => {
return combineLatest(
votingBalance(api)(address),
votingBalancesNominatorsFor(api)(address)
@@ -5,14 +5,14 @@
import { combineLatest, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/index';
import { AccountId, Address } from '@polkadot/types/index';
import { DerivedBalancesMap } from '../types';
import { drr } from '../util/drr';
import { validatingBalance } from './validatingBalance';
export function validatingBalances (api: ApiInterface$Rx) {
return (accountIds: Array<AccountId | string>): Observable<DerivedBalancesMap> => {
return (accountIds: Array<AccountId | Address | string>): Observable<DerivedBalancesMap> => {
return !accountIds || !accountIds.length
? of({}).pipe(drr())
: combineLatest(
@@ -5,7 +5,7 @@
import { combineLatest, of, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex, Balance } from '@polkadot/types/index';
import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/index';
import { idAndIndex } from '../accounts/idAndIndex';
import { DerivedBalances } from '../types';
@@ -14,7 +14,7 @@ import { drr } from '../util/drr';
const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32));
export function votingBalance (api: ApiInterface$Rx) {
return (address: AccountIndex | AccountId | string): Observable<DerivedBalances> => {
return (address: AccountIndex | AccountId | Address | string): Observable<DerivedBalances> => {
return idAndIndex(api)(address).pipe(
switchMap(([accountId]) =>
(accountId
@@ -4,14 +4,14 @@
import { combineLatest, Observable, of } from 'rxjs';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex } from '@polkadot/types/index';
import { AccountId, AccountIndex, Address } from '@polkadot/types/index';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
import { votingBalance } from './votingBalance';
export function votingBalances (api: ApiInterface$Rx) {
return (addresses?: Array<AccountIndex | AccountId | string>): Observable<Array<DerivedBalances>> => {
return (addresses?: Array<AccountId | AccountIndex | Address | string>): Observable<Array<DerivedBalances>> => {
return !addresses || !addresses.length
? of([]).pipe(drr())
: combineLatest(...addresses.map(votingBalance(api))).pipe(drr());
@@ -2,23 +2,27 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { Vector } from '@polkadot/types/codec';
import { AccountId } from '@polkadot/types/index';
import { AccountId, AccountIndex, Address } from '@polkadot/types/index';
import { idAndIndex } from '../accounts/idAndIndex';
import { DerivedBalances } from '../types';
import { drr } from '../util/drr';
import { votingBalances } from './votingBalances';
export function votingBalancesNominatorsFor (api: ApiInterface$Rx) {
return (accountId: AccountId | string): Observable<Array<DerivedBalances>> => {
// tslint:disable-next-line
return (api.query.staking.nominatorsFor(accountId) as Observable<Vector<AccountId>>)
.pipe(
switchMap(votingBalances(api)),
drr()
) as Observable<Array<DerivedBalances>>;
return (address: AccountId | AccountIndex | Address | string): Observable<Array<DerivedBalances>> => {
return idAndIndex(api)(address).pipe(
switchMap(([accountId]) =>
accountId
? (api.query.staking.nominatorsFor(accountId) as Observable<Vector<AccountId>>)
: of([] as Array<AccountId>)
),
switchMap(votingBalances(api)),
drr()
);
};
}