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>>;
};
}
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { filter } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { ReferendumInfo } from '@polkadot/types/index';
@@ -11,18 +11,18 @@ import { ReferendumInfo } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function referendumInfos (api: ApiInterface$Rx) {
return (...params: Array<any>): Observable<Array<ReferendumInfo>> => {
const ids: Array<BN | number> = params.slice(0, params.length - 1);
return combineLatest(
ids.map(
(id) => (api.query.democracy.referendumInfoOf(id) as Observable<ReferendumInfo>).pipe(
filter((info) => !!info)
return (ids: Array<BN | number> = []): Observable<Array<ReferendumInfo>> => {
return !ids || !ids.length
? of([]).pipe(drr())
: combineLatest(
ids.map(
(id) => (api.query.democracy.referendumInfoOf(id) as Observable<ReferendumInfo>).pipe(
filter((info) => !!info)
)
)
)
).pipe(
drr()
);
).pipe(
drr()
);
};
}
@@ -20,8 +20,8 @@ export function referendumVotesFor (api: ApiInterface$Rx) {
switchMap((votersFor) =>
combineLatest(
of(votersFor),
votes(api)(referendumId as BN, ...votersFor),
votingBalances(api)(...votersFor)
votes(api)(referendumId as BN, votersFor),
votingBalances(api)(votersFor)
)
),
map(([votersFor, votes, balances]) =>
@@ -19,9 +19,10 @@ export function referendums (api: ApiInterface$Rx) {
switchMap(([nextTally, referendumCount]) =>
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
? referendumInfos(api)(
...[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i) =>
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i) =>
nextTally.addn(i)
))
)
)
: of([])
),
drr()
+12 -9
View File
@@ -3,19 +3,22 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { combineLatest, Observable } from 'rxjs';
import { combineLatest, Observable, of } from 'rxjs';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Vote } from '@polkadot/types/index';
import { drr } from '../util/drr';
export function votes (api: ApiInterface$Rx) {
return (referendumId: BN, ...accountIds: Array<AccountId>): Observable<Array<Vote>> =>
combineLatest(
accountIds.map(
(accountId) => api.query.democracy.voteOf([referendumId, accountId]) as Observable<Vote>
)
).pipe(
drr()
);
return (referendumId: BN, accountIds: Array<AccountId> = []): Observable<Array<Vote>> => {
return !accountIds || !accountIds.length
? of([]).pipe(drr())
: combineLatest(
accountIds.map(
(accountId) => api.query.democracy.voteOf([referendumId, accountId]) as Observable<Vote>
)
).pipe(
drr()
);
};
}
+3 -1
View File
@@ -8,10 +8,11 @@ import * as balances from './balances';
import * as chain from './chain';
import * as democracy from './democracy';
import * as session from './session';
import * as staking from './staking';
import { cache } from './util/cache';
// Put all derived functions in an object, for easier Object.keys()-ing.
const functions = { balances, chain, democracy, session };
const functions = { balances, chain, democracy, session, staking };
/**
* T represents the section here (chain, balances...), and P represents
@@ -26,6 +27,7 @@ export interface Derive {
chain: ReturnTypes<typeof chain>;
democracy: ReturnTypes<typeof democracy>;
session: ReturnTypes<typeof session>;
staking: ReturnTypes<typeof staking>;
}
export default function decorateDerive (api: ApiInterface$Rx): Derive {
+5
View File
@@ -0,0 +1,5 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './intentionsBalances';
@@ -0,0 +1,26 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// 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 { switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { Vector } from '@polkadot/types/codec';
import { AccountId } from '@polkadot/types/index';
import { DerivedBalancesMap } from '../types';
import { validatingBalances } from '../balances';
import { drr } from '../util/drr';
/**
* Get the balances for all intentions and their nominators
*/
export function intentionsBalances (api: ApiInterface$Rx) {
return (): Observable<DerivedBalancesMap> =>
// tslint:disable-next-line
(api.query.staking.intentions() as Observable<Vector<AccountId>>)
.pipe(
switchMap(validatingBalances(api)),
drr()
) as Observable<DerivedBalancesMap>;
}
+9
View File
@@ -2,6 +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 BN from 'bn.js';
import { AccountId, Balance, Vote } from '@polkadot/types/index';
export type DerivedBalances = {
@@ -14,6 +15,14 @@ export type DerivedBalances = {
nominators?: Array<DerivedBalances>
};
export type DerivedFees = {
creationFee: BN,
existentialDeposit: BN,
transactionBaseFee: BN,
transactionByteFee: BN,
transferFee: BN
};
export type DerivedBalancesMap = {
[index: string]: DerivedBalances
};
+12 -2
View File
@@ -6,16 +6,19 @@ import BN from 'bn.js';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { BlockNumber } from '@polkadot/types/index';
import { WsProvider } from '@polkadot/rpc-provider/index';
const WS = 'wss://poc3-rpc.polkadot.io/';
describe.skip('derive e2e', () => {
let api: ApiInterface$Rx;
beforeAll(() => {
jest.setTimeout(30000);
jest.setTimeout(10000);
});
beforeEach(async (done) => {
api = await ApiRx.create().toPromise();
api = await ApiRx.create(new WsProvider(WS)).toPromise();
done();
});
@@ -34,6 +37,13 @@ describe.skip('derive e2e', () => {
});
});
it('returns the intentions with balances', async (done) => {
api.derive.staking.intentionsBalances().subscribe((balances) => {
expect(Object.keys(balances as object)).not.toHaveLength(0);
done();
});
});
afterAll(() => {
jest.setTimeout(5000);
});