Add derive functions (#564)
* Add drr * Update headers * Lint * Copy other functions * Account indexes * accountIndexToId * accountIdAndIndex * Fees * votingBalance * votingBalances * votingBalancesNominatorsFor * validatingBalance * validatingBalances * bestNumberFinalised * eraLength * Add drr * eraProgress * votes * referendumInfos * referendumVotesFor * referendums * Clean code * Add tests * Fix lint * Fix bug eraProgress * Fix bug fees * combineLatest without array * Fix lint
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// 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, of } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { decodeAddress } from '@polkadot/keyring';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/index';
|
||||
|
||||
import { accountIdToIndex } from './accountIdToIndex';
|
||||
import { accountIndexToId } from './accountIndexToId';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export type IdAndIndex = [AccountId | undefined, AccountIndex | undefined];
|
||||
|
||||
export function accountIdAndIndex (api: ApiRx) {
|
||||
return (address: AccountId | AccountIndex | string | null | undefined)
|
||||
: Observable<(AccountIndex | AccountId | undefined)[]> => {
|
||||
try {
|
||||
// yes, this can fail, don't care too much, catch will catch it
|
||||
const { length } = decodeAddress((address as any).toString());
|
||||
|
||||
if (length === 32) {
|
||||
const accountId = new AccountId(address as string);
|
||||
|
||||
return accountIdToIndex(api)(accountId).pipe(
|
||||
map((accountIndex) => [accountId, accountIndex]),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
const accountIndex = new AccountIndex(address as string);
|
||||
|
||||
return accountIndexToId(api)(accountIndex).pipe(
|
||||
map((accountId) => [accountId, accountIndex]),
|
||||
drr()
|
||||
);
|
||||
} catch (error) {
|
||||
return of([undefined, undefined]).pipe(drr());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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 { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/index';
|
||||
|
||||
import { accountIndexes, AccountIndexes } from './accountIndexes';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function accountIdToIndex (api: ApiRx) {
|
||||
return (accountId: AccountId | string): Observable<AccountIndex> =>
|
||||
accountIndexes(api)()
|
||||
.pipe(
|
||||
map((indexes: AccountIndexes) => (indexes || {})[accountId.toString()]),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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 { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex';
|
||||
import { Vector } from '@polkadot/types/codec';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function accountIndexToId (api: ApiRx) {
|
||||
return (accountIndex: AccountIndex | string): Observable<AccountId> => {
|
||||
const _accountIndex = accountIndex instanceof AccountIndex
|
||||
? accountIndex
|
||||
: new AccountIndex(accountIndex);
|
||||
|
||||
return (api.query.balances.enumSet(accountIndex) as Observable<Vector<AccountId>>)
|
||||
.pipe(
|
||||
map((accounts) => (accounts || [])[_accountIndex.mod(ENUMSET_SIZE).toNumber()]),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { ENUMSET_SIZE } from '@polkadot/types/AccountIndex';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export type AccountIndexes = { [index: string]: AccountIndex };
|
||||
|
||||
export function accountIndexes (api: ApiRx) {
|
||||
return (): Observable<AccountIndexes> =>
|
||||
(api.query.balances.nextEnumSet() as Observable<AccountIndex>)
|
||||
.pipe(
|
||||
map((next: AccountIndex) => {
|
||||
const enumRange = [...Array((next || new BN(0)).div(ENUMSET_SIZE).toNumber() + 1).keys()];
|
||||
|
||||
return enumRange;
|
||||
}),
|
||||
switchMap((enumRange) => combineLatest(
|
||||
enumRange.map((index) => (api.query.balances.enumSet(index) as Observable<any>))
|
||||
)),
|
||||
map((all: Array<Array<AccountId> | undefined>) =>
|
||||
(all || []).reduce((result, list, outerIndex) => {
|
||||
(list || []).forEach((accountId, innerIndex) => {
|
||||
const index = (outerIndex * ENUMSET_SIZE.toNumber()) + innerIndex;
|
||||
|
||||
result[accountId.toString()] = new AccountIndex(index);
|
||||
});
|
||||
|
||||
return result;
|
||||
}, {} as AccountIndexes)),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function fees (api: ApiRx) {
|
||||
return () =>
|
||||
combineLatest(
|
||||
api.query.balances.creationFee(),
|
||||
api.query.balances.existentialDeposit(),
|
||||
api.query.balances.transactionBaseFee(),
|
||||
api.query.balances.transactionByteFee(),
|
||||
api.query.balances.transferFee()
|
||||
).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)
|
||||
})),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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 './accountIdAndIndex';
|
||||
export * from './accountIdToIndex';
|
||||
export * from './accountIndexToId';
|
||||
export * from './accountIndexes';
|
||||
export * from './fees';
|
||||
export * from './validatingBalance';
|
||||
export * from './validatingBalances';
|
||||
export * from './votingBalance';
|
||||
export * from './votingBalances';
|
||||
export * from './votingBalancesNominatorsFor';
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId, Balance } from '@polkadot/types/index';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votingBalance } from './votingBalance';
|
||||
import { votingBalancesNominatorsFor } from './votingBalancesNominatorsFor';
|
||||
|
||||
export function validatingBalance (api: ApiRx) {
|
||||
return (address: AccountId | string): Observable<DerivedBalances> =>
|
||||
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));
|
||||
|
||||
return {
|
||||
...balance,
|
||||
nominators,
|
||||
nominatedBalance: new Balance(nominatedBalance),
|
||||
stakingBalance: new Balance(
|
||||
nominatedBalance.add(balance.votingBalance)
|
||||
)
|
||||
};
|
||||
}),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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 { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId } from '@polkadot/types/index';
|
||||
|
||||
import { DerivedBalancesMap } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { validatingBalance } from './validatingBalance';
|
||||
|
||||
export function validatingBalances (api: ApiRx) {
|
||||
return (...params: Array<any>): Observable<DerivedBalancesMap> => {
|
||||
const accountIds: Array<AccountId | string> = params.slice(0, params.length - 1);
|
||||
|
||||
return combineLatest(
|
||||
accountIds.map(validatingBalance(api))
|
||||
).pipe(
|
||||
map((result) =>
|
||||
result.reduce((balances, balance) => {
|
||||
balances[balance.accountId.toString()] = balance;
|
||||
|
||||
return balances;
|
||||
}, {} as DerivedBalancesMap)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 { combineLatest, of, Observable } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId, AccountIndex, Balance } from '@polkadot/types/index';
|
||||
|
||||
import { accountIdAndIndex } from './accountIdAndIndex';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
const EMPTY_ACCOUNT = new AccountId(new Uint8Array(32));
|
||||
|
||||
export function votingBalance (api: ApiRx) {
|
||||
return (address: AccountIndex | AccountId | string): Observable<DerivedBalances> =>
|
||||
accountIdAndIndex(api)(address).pipe(
|
||||
switchMap(([accountId]) =>
|
||||
(accountId
|
||||
? combineLatest(
|
||||
of(accountId),
|
||||
api.query.balances.freeBalance(accountId),
|
||||
api.query.balances.reservedBalance(accountId)
|
||||
)
|
||||
: 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))
|
||||
)
|
||||
})),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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 { combineLatest, Observable } from 'rxjs';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/index';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votingBalance } from './votingBalance';
|
||||
|
||||
export function votingBalances (api: ApiRx) {
|
||||
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());
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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 { map, switchMap } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId } from '@polkadot/types/index';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votingBalances } from './votingBalances';
|
||||
|
||||
export function votingBalancesNominatorsFor (api: ApiRx) {
|
||||
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()
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import { filter, map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber, Header } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
* Get the latest block number.
|
||||
*/
|
||||
@@ -15,6 +17,7 @@ export function bestNumber (api: ApiRx) {
|
||||
api.rpc.chain.subscribeNewHead()
|
||||
.pipe(
|
||||
filter((header: Header) => header && !!header.blockNumber),
|
||||
map(({ blockNumber }: Header) => blockNumber)
|
||||
map(({ blockNumber }: Header) => blockNumber),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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 { filter, map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber, Header } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
/**
|
||||
* Get the latest finalised block number.
|
||||
*/
|
||||
export function bestNumberFinalised (api: ApiRx) {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeFinalisedHeads()
|
||||
.pipe(
|
||||
filter((header: Header) => header && !!header.blockNumber),
|
||||
map(({ blockNumber }: Header) => blockNumber),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './bestNumber';
|
||||
export * from './bestNumberFinalised';
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// 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 './referendumInfos';
|
||||
export * from './referendums';
|
||||
export * from './referendumVotesFor';
|
||||
export * from './votes';
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { filter } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { ReferendumInfo } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function referendumInfos (api: ApiRx) {
|
||||
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)
|
||||
)
|
||||
)
|
||||
).pipe(
|
||||
drr()
|
||||
);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { Vector } from '@polkadot/types/codec';
|
||||
import { AccountId, Balance, Vote } from '@polkadot/types/index';
|
||||
|
||||
import { DerivedReferendumVote } from '../types';
|
||||
import { drr } from '../util/drr';
|
||||
import { votes } from './votes';
|
||||
import { votingBalances } from '../balances/votingBalances';
|
||||
|
||||
export function referendumVotesFor (api: ApiRx) {
|
||||
return (referendumId: BN | number): Observable<Array<DerivedReferendumVote>> =>
|
||||
(api.query.democracy.votersFor(referendumId) as Observable<Vector<AccountId>>).pipe(
|
||||
switchMap((votersFor) =>
|
||||
combineLatest(
|
||||
of(votersFor),
|
||||
votes(api)(referendumId as BN, ...votersFor),
|
||||
votingBalances(api)(...votersFor)
|
||||
)
|
||||
),
|
||||
map(([votersFor, votes, balances]) =>
|
||||
votersFor.map((accountId, index): DerivedReferendumVote => ({
|
||||
accountId,
|
||||
balance: balances[index].votingBalance || new Balance(0),
|
||||
vote: votes[index] || new Vote(0)
|
||||
}))
|
||||
),
|
||||
drr()
|
||||
);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 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 { combineLatest, Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/index';
|
||||
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function referendums (api: ApiRx) {
|
||||
return (): Observable<Array<ReferendumInfo>> =>
|
||||
(combineLatest(
|
||||
api.query.democracy.nextTally(),
|
||||
api.query.democracy.referendumCount()
|
||||
) as Observable<[ReferendumIndex?, ReferendumIndex?]>).pipe(
|
||||
switchMap(([nextTally, referendumCount]) =>
|
||||
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
|
||||
? referendumInfos(api)(
|
||||
...[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i) =>
|
||||
nextTally.addn(i)
|
||||
))
|
||||
: of([])
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { AccountId, Vote } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function votes (api: ApiRx) {
|
||||
return (referendumId: BN, ...accountIds: Array<AccountId>): Observable<Array<Vote>> =>
|
||||
combineLatest(
|
||||
accountIds.map(
|
||||
(accountId) => api.query.democracy.voteOf([referendumId, accountId]) as Observable<Vote>
|
||||
)
|
||||
).pipe(
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -37,6 +37,26 @@ describe('derive', () => {
|
||||
|
||||
beforeAll((done) => api.isReady.subscribe(() => done()));
|
||||
|
||||
testFunction(api)('balances', 'accountIdAndIndex', []);
|
||||
testFunction(api)('balances', 'accountIdToIndex', []);
|
||||
testFunction(api)('balances', 'accountIndexes', []);
|
||||
testFunction(api)('balances', 'accountIndexToId', []);
|
||||
testFunction(api)('balances', 'fees', []);
|
||||
testFunction(api)('balances', 'validatingBalance', []);
|
||||
testFunction(api)('balances', 'validatingBalances', []);
|
||||
testFunction(api)('balances', 'votingBalance', []);
|
||||
testFunction(api)('balances', 'votingBalances', []);
|
||||
testFunction(api)('balances', 'votingBalancesNominatorsFor', []);
|
||||
|
||||
testFunction(api)('chain', 'bestNumber', []);
|
||||
testFunction(api)('chain', 'bestNumberFinalised', []);
|
||||
|
||||
testFunction(api)('democracy', 'referendumInfos', []);
|
||||
testFunction(api)('democracy', 'referendums', []);
|
||||
testFunction(api)('democracy', 'referendumVotesFor', []);
|
||||
testFunction(api)('democracy', 'votes', []);
|
||||
|
||||
testFunction(api)('session', 'eraLength', []);
|
||||
testFunction(api)('session', 'eraProgress', []);
|
||||
testFunction(api)('session', 'sessionProgress', []);
|
||||
});
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
|
||||
import * as balances from './balances';
|
||||
import * as chain from './chain';
|
||||
import * as democracy from './democracy';
|
||||
import * as session from './session';
|
||||
import { cache } from './util/cache';
|
||||
|
||||
// Put all derived functions in an object, for easier Object.keys()-ing.
|
||||
const functions = { chain, session };
|
||||
const functions = { balances, chain, democracy, session };
|
||||
|
||||
/**
|
||||
* T represents the section here (chain, balances...), and P represents
|
||||
@@ -20,7 +22,9 @@ type ReturnTypes<T extends Record<keyof T, (...args: any[]) => any>> = {
|
||||
};
|
||||
|
||||
export interface Derive {
|
||||
balances: ReturnTypes<typeof balances>;
|
||||
chain: ReturnTypes<typeof chain>;
|
||||
democracy: ReturnTypes<typeof democracy>;
|
||||
session: ReturnTypes<typeof session>;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber } from '@polkadot/types/index';
|
||||
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function eraLength (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
(combineLatest(
|
||||
api.query.session.sessionLength(),
|
||||
api.query.staking.sessionsPerEra()
|
||||
) as Observable<[BlockNumber?, BlockNumber?]>).pipe(
|
||||
map(
|
||||
([sessionLength, sessionsPerEra]) =>
|
||||
(sessionLength || new BN(1)).mul(sessionsPerEra || new BN(1))
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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 BN from 'bn.js';
|
||||
import { combineLatest, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import ApiRx from '@polkadot/api/rx';
|
||||
import { BlockNumber } from '@polkadot/types/index';
|
||||
|
||||
import { sessionProgress } from './sessionProgress';
|
||||
import { drr } from '../util/drr';
|
||||
|
||||
export function eraProgress (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
(combineLatest(
|
||||
sessionProgress(api)(),
|
||||
api.query.session.currentIndex(),
|
||||
api.query.session.sessionLength(),
|
||||
api.query.staking.lastEraLengthChange(),
|
||||
api.query.staking.sessionsPerEra()
|
||||
) as Observable<[BN, BlockNumber?, BlockNumber?, BlockNumber?, BlockNumber?]>).pipe(
|
||||
map(
|
||||
([sessionProgress, currentIndex, sessionLength, lastEraLengthChange, sessionsPerEra]) =>
|
||||
(currentIndex || new BN(0))
|
||||
.sub(lastEraLengthChange || new BN(0))
|
||||
.mod(sessionsPerEra || new BN(1))
|
||||
.mul(sessionLength || new BN(1))
|
||||
.add(sessionProgress || new BN(0)
|
||||
)
|
||||
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -2,4 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './eraLength';
|
||||
export * from './eraProgress';
|
||||
export * from './sessionProgress';
|
||||
|
||||
@@ -13,14 +13,14 @@ import { drr } from '../util/drr';
|
||||
|
||||
export function sessionProgress (api: ApiRx) {
|
||||
return (): Observable<BN> =>
|
||||
combineLatest([
|
||||
combineLatest(
|
||||
bestNumber(api)(),
|
||||
api.query.session.sessionLength(),
|
||||
api.query.session.lastLengthChange()
|
||||
]).pipe(
|
||||
).pipe(
|
||||
map(
|
||||
([bestNumber, sessionLength, lastLengthChange]) =>
|
||||
(bestNumber as BlockNumber || new BN(0))
|
||||
(bestNumber || new BN(0))
|
||||
.sub(lastLengthChange as BlockNumber || new BN(0))
|
||||
.add(sessionLength as BlockNumber || new BN(1))
|
||||
.mod(sessionLength as BlockNumber || new BN(1))
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 { AccountId, Balance, Vote } from '@polkadot/types/index';
|
||||
|
||||
export type DerivedBalances = {
|
||||
accountId: AccountId,
|
||||
freeBalance: Balance,
|
||||
nominatedBalance: Balance,
|
||||
reservedBalance: Balance,
|
||||
votingBalance: Balance,
|
||||
stakingBalance: Balance,
|
||||
nominators?: Array<DerivedBalances>
|
||||
};
|
||||
|
||||
export type DerivedBalancesMap = {
|
||||
[index: string]: DerivedBalances
|
||||
};
|
||||
|
||||
export type DerivedReferendumVote = {
|
||||
accountId: AccountId,
|
||||
balance: Balance,
|
||||
vote: Vote
|
||||
};
|
||||
Reference in New Issue
Block a user