Add derive.staking.intentionsBalances (#595)

* Add derive.staking.intentionsBalances

* Skip e2e test

* ts-lint?

* Alignment with new signatures, remove all slices
This commit is contained in:
Jaco Greeff
2019-01-17 09:55:08 +01:00
committed by GitHub
parent 668ec1dd13
commit a69cff1c64
16 changed files with 157 additions and 92 deletions
@@ -12,20 +12,19 @@ import { accountIdToIndex } from './accountIdToIndex';
import { accountIndexToId } from './accountIndexToId';
import { drr } from '../util/drr';
export type IdAndIndex = [AccountId | undefined, AccountIndex | undefined];
export type IdAndIndex = [AccountId?, AccountIndex?];
export function accountIdAndIndex (api: ApiInterface$Rx) {
return (address: AccountId | AccountIndex | string | null | undefined)
: Observable<(AccountIndex | AccountId | undefined)[]> => {
return (address?: AccountId | AccountIndex | string | null): Observable<IdAndIndex> => {
try {
// yes, this can fail, don't care too much, catch will catch it
const { length } = decodeAddress((address as any).toString());
const decoded = decodeAddress(address as any);
if (length === 32) {
const accountId = new AccountId(address as string);
if (decoded.length === 32) {
const accountId = new AccountId(decoded);
return accountIdToIndex(api)(accountId).pipe(
map((accountIndex) => [accountId, accountIndex]),
map((accountIndex) => [accountId, accountIndex] as IdAndIndex),
drr()
);
}
@@ -33,11 +32,11 @@ export function accountIdAndIndex (api: ApiInterface$Rx) {
const accountIndex = new AccountIndex(address as string);
return accountIndexToId(api)(accountIndex).pipe(
map((accountId) => [accountId, accountIndex]),
map((accountId) => [accountId, accountIndex] as IdAndIndex),
drr()
);
} catch (error) {
return of([undefined, undefined]).pipe(drr());
return of([undefined, undefined] as IdAndIndex).pipe(drr());
}
};
}
+13 -11
View File
@@ -2,29 +2,31 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { combineLatest } from 'rxjs';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import BN from 'bn.js';
import { DerivedFees } from '../types';
import { drr } from '../util/drr';
export function fees (api: ApiInterface$Rx) {
return () =>
combineLatest(
return (): Observable<DerivedFees> => {
return (combineLatest(
api.query.balances.creationFee(),
api.query.balances.existentialDeposit(),
api.query.balances.transactionBaseFee(),
api.query.balances.transactionByteFee(),
api.query.balances.transferFee()
).pipe(
) as any as Observable<[BN, BN, BN, BN, BN]>).pipe(
map(([creationFee, existentialDeposit, transactionBaseFee, transactionByteFee, transferFee]) => ({
creationFee: creationFee || new BN(0),
existentialDeposit: existentialDeposit || new BN(0),
transactionBaseFee: transactionBaseFee || new BN(0),
transactionByteFee: transactionByteFee || new BN(0),
transferFee: transferFee || new BN(0)
creationFee,
existentialDeposit,
transactionBaseFee,
transactionByteFee,
transferFee
})),
drr()
);
) ;
};
}
@@ -14,15 +14,17 @@ import { votingBalance } from './votingBalance';
import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor';
export function validatingBalance (api: ApiInterface$Rx) {
return (address: AccountId | string): Observable<DerivedBalances> =>
combineLatest(
return (address: AccountId | string): Observable<DerivedBalances> => {
return combineLatest(
votingBalance(api)(address),
votingBalancesNominatorsFor(api)(address)
).pipe(
map(([balance, nominators]) => {
const nominatedBalance = nominators.reduce(
(total: BN, nominatorBalance: DerivedBalances) => total.add(nominatorBalance.votingBalance),
new BN(0));
(total: BN, nominatorBalance: DerivedBalances) =>
total.add(nominatorBalance.votingBalance),
new BN(0)
);
return {
...balance,
@@ -35,4 +37,5 @@ export function validatingBalance (api: ApiInterface$Rx) {
}),
drr()
);
};
}
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { combineLatest, Observable } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/index';
@@ -12,21 +12,20 @@ import { drr } from '../util/drr';
import { validatingBalance } from './validatingBalance';
export function validatingBalances (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<DerivedBalancesMap> => {
const accountIds: Array<AccountId | string> = params.slice(0, params.length - 1);
return (accountIds: Array<AccountId | string>): Observable<DerivedBalancesMap> => {
return !accountIds || !accountIds.length
? of({}).pipe(drr())
: combineLatest(
accountIds.map(validatingBalance(api))
).pipe(
map((result) => {
return result.reduce((balances, balance) => {
balances[balance.accountId.toString()] = balance;
return combineLatest(
accountIds.map(validatingBalance(api))
).pipe(
map((result) =>
result.reduce((balances, balance) => {
balances[balance.accountId.toString()] = balance;
return balances;
}, {} as DerivedBalancesMap)
),
drr()
);
return balances;
}, {} as DerivedBalancesMap);
}),
drr()
);
};
}
@@ -14,8 +14,8 @@ 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> =>
accountIdAndIndex(api)(address).pipe(
return (address: AccountIndex | AccountId | string): Observable<DerivedBalances> => {
return accountIdAndIndex(api)(address).pipe(
switchMap(([accountId]) =>
(accountId
? combineLatest(
@@ -26,16 +26,19 @@ export function votingBalance (api: ApiInterface$Rx) {
: of([undefined, undefined, undefined])
) as Observable<[AccountId?, Balance?, Balance?]>
),
map(([accountId, freeBalance, reservedBalance]) => ({
accountId: accountId || EMPTY_ACCOUNT,
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))
)
})),
map(([accountId, freeBalance = new Balance(0), reservedBalance = new Balance(0)]) => {
return {
accountId: accountId || EMPTY_ACCOUNT,
freeBalance,
nominatedBalance: new Balance(0),
reservedBalance,
stakingBalance: new Balance(0),
votingBalance: new Balance(
freeBalance.add(reservedBalance)
)
};
}),
drr()
);
};
}
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { combineLatest, Observable } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, AccountIndex } from '@polkadot/types/index';
@@ -11,9 +11,9 @@ import { drr } from '../util/drr';
import { votingBalance } from './votingBalance';
export function votingBalances (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<Array<DerivedBalances>> => {
const addresses: Array<AccountIndex | AccountId | string> = params.slice(0, params.length - 1);
return combineLatest(...addresses.map(votingBalance(api))).pipe(drr());
return (addresses?: Array<AccountIndex | AccountId | string>): Observable<Array<DerivedBalances>> => {
return !addresses || !addresses.length
? of([]).pipe(drr())
: combineLatest(...addresses.map(votingBalance(api))).pipe(drr());
};
}
@@ -3,8 +3,9 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
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 { DerivedBalances } from '../types';
@@ -12,10 +13,12 @@ import { drr } from '../util/drr';
import { votingBalances } from './votingBalances';
export function votingBalancesNominatorsFor (api: ApiInterface$Rx) {
return (accountId: AccountId | string): Observable<Array<DerivedBalances>> =>
(api.query.staking.nominatorsFor(accountId) as Observable<Array<AccountId | undefined | null> | undefined>).pipe(
map((nominators) => nominators || []),
switchMap(votingBalances(api)),
drr()
);
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>>;
};
}