diff --git a/packages/api-observable/src/Combined.ts b/packages/api-observable/src/Combined.ts index f800491b77..11d76ab9bd 100644 --- a/packages/api-observable/src/Combined.ts +++ b/packages/api-observable/src/Combined.ts @@ -10,7 +10,7 @@ import { switchMap, defaultIfEmpty, map } from 'rxjs/operators'; import { decodeAddress } from '@polkadot/keyring'; import { AccountId, AccountIndex, Balance, bool as Bool, BlockNumber, Moment, ReferendumIndex } from '@polkadot/types/index'; import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex'; -import { assert, isString } from '@polkadot/util'; +import { assert } from '@polkadot/util'; import ApiCalls from './Calls'; import { RxProposal, RxReferendum } from './classes'; @@ -384,27 +384,33 @@ export default class ApiCombined extends ApiCalls { ); } - votingBalance = (_address: AccountId | string): Observable => { - const address = isString(_address) - ? new AccountId(_address) - : _address; + votingBalance = (address: AccountIndex | AccountId | string): Observable => { + return this + .accountIdAndIndex(address) + .pipe( + switchMap(([accountId]: [AccountId | undefined, AccountIndex | undefined]): Observable => { + if (!accountId) { + return EMPTY; + } - return this.combine( - [ - this.balanceFree(address), - this.balanceReserved(address) - ], - ([freeBalance, reservedBalance]: [Balance | undefined, Balance | undefined]): RxBalance => ({ - address, - freeBalance: freeBalance || new Balance(0), - nominatedBalance: new Balance(0), - reservedBalance: reservedBalance || new Balance(0), - stakingBalance: new Balance(0), - votingBalance: new Balance( - (freeBalance || new Balance(0)).add(reservedBalance || new Balance(0)) - ) - }) - ); + return this.combine( + [ + this.balanceFree(accountId), + this.balanceReserved(accountId) + ], + ([freeBalance, reservedBalance]: [Balance | undefined, Balance | undefined]): RxBalance => ({ + address: accountId, + freeBalance: freeBalance || new Balance(0), + nominatedBalance: new Balance(0), + reservedBalance: reservedBalance || new Balance(0), + stakingBalance: new Balance(0), + votingBalance: new Balance( + (freeBalance || new Balance(0)).add(reservedBalance || new Balance(0)) + ) + }) + ); + }) + ); } votingBalancesNominatorsFor = (address: AccountId | string) => {