Swap internal derives to use api.derive.* (#1553)
* Swap internal derives to use api.derive.* * Rx vs normal decorate * Small cleanups * Res-use sessions * Cleanup types * Fix refCount lost subs * Small cleanups
This commit is contained in:
@@ -11,18 +11,11 @@ import { isU8a } from '@polkadot/util';
|
||||
import { decodeAddress } from '@polkadot/util-crypto';
|
||||
import { createType } from '@polkadot/types';
|
||||
|
||||
import { idToIndex } from './idToIndex';
|
||||
import { indexToId } from './indexToId';
|
||||
import { drr } from '../util';
|
||||
|
||||
interface Calls {
|
||||
idToIndexCall (accountId: string | AccountId): Observable<AccountIndex | undefined>;
|
||||
indexToIdCall (accountIndex: string | AccountIndex): Observable<AccountId | undefined>;
|
||||
}
|
||||
|
||||
export type AccountIdAndIndex = [AccountId?, AccountIndex?];
|
||||
|
||||
function retrieve (address: Address | AccountId | AccountIndex | string | null | undefined, { idToIndexCall, indexToIdCall }: Calls): Observable<AccountIdAndIndex> {
|
||||
function retrieve (api: ApiInterfaceRx, address: Address | AccountId | AccountIndex | string | null | undefined): Observable<AccountIdAndIndex> {
|
||||
try {
|
||||
// yes, this can fail, don't care too much, catch will catch it
|
||||
const decoded = isU8a(address)
|
||||
@@ -32,14 +25,14 @@ function retrieve (address: Address | AccountId | AccountIndex | string | null |
|
||||
if (decoded.length === 32) {
|
||||
const accountId = createType('AccountId', decoded);
|
||||
|
||||
return idToIndexCall(accountId).pipe(
|
||||
return api.derive.accounts.idToIndex(accountId).pipe(
|
||||
map((accountIndex): AccountIdAndIndex => [accountId, accountIndex] as AccountIdAndIndex)
|
||||
);
|
||||
}
|
||||
|
||||
const accountIndex = createType('AccountIndex', decoded);
|
||||
|
||||
return indexToIdCall(accountIndex).pipe(
|
||||
return api.derive.accounts.indexToId(accountIndex).pipe(
|
||||
map((accountId): AccountIdAndIndex => [accountId, accountIndex] as AccountIdAndIndex)
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -61,11 +54,6 @@ function retrieve (address: Address | AccountId | AccountIndex | string | null |
|
||||
* ```
|
||||
*/
|
||||
export function idAndIndex (api: ApiInterfaceRx): (address?: Address | AccountId | AccountIndex | string | null) => Observable<AccountIdAndIndex> {
|
||||
const calls = {
|
||||
idToIndexCall: idToIndex(api),
|
||||
indexToIdCall: indexToId(api)
|
||||
};
|
||||
|
||||
return (address?: Address | AccountId | AccountIndex | string | null): Observable<AccountIdAndIndex> =>
|
||||
retrieve(address, calls).pipe(drr());
|
||||
retrieve(api, address).pipe(drr());
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/interfaces';
|
||||
import { AccountIndexes } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map, startWith } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { indexes, AccountIndexes } from './indexes';
|
||||
import { drr } from '../util';
|
||||
|
||||
/**
|
||||
@@ -26,10 +26,8 @@ import { drr } from '../util';
|
||||
* ```
|
||||
*/
|
||||
export function idToIndex (api: ApiInterfaceRx): (accountId: AccountId | string) => Observable<AccountIndex | undefined> {
|
||||
const indexesCall = indexes(api);
|
||||
|
||||
return (accountId: AccountId | string): Observable<AccountIndex | undefined> =>
|
||||
indexesCall().pipe(
|
||||
api.derive.accounts.indexes().pipe(
|
||||
startWith({}),
|
||||
map((indexes: AccountIndexes): AccountIndex | undefined =>
|
||||
(indexes || {})[accountId.toString()]
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { AccountId, AccountIndex } from '@polkadot/types/interfaces';
|
||||
import { AccountIndexes } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
@@ -12,8 +13,6 @@ import { createType } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
|
||||
export type AccountIndexes = Record<string, AccountIndex>;
|
||||
|
||||
const enumsetSize = ENUMSET_SIZE.toNumber();
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,19 +7,25 @@ import { AccountId, AccountIndex, Address, Balance } from '@polkadot/types/inter
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { DeriveAccountInfo } from '../types';
|
||||
|
||||
import { Observable, of, combineLatest } from 'rxjs';
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { Bytes, Option, u32 } from '@polkadot/types';
|
||||
import { u8aToString } from '@polkadot/util';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { idAndIndex } from './idAndIndex';
|
||||
|
||||
function retrieveNick (api: ApiInterfaceRx): (accountId?: AccountId) => Observable<Option<[Bytes, Balance] & Codec> | undefined> {
|
||||
return (accountId?: AccountId): Observable<Option<[Bytes, Balance] & Codec> | undefined> =>
|
||||
accountId && api.query.nicks
|
||||
? api.query.nicks.nameOf<Option<[Bytes, Balance] & Codec>>(accountId)
|
||||
: of(undefined);
|
||||
function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<string | undefined> {
|
||||
return accountId && api.query.nicks
|
||||
? api.query.nicks
|
||||
.nameOf<Option<[Bytes, Balance] & Codec>>(accountId)
|
||||
.pipe(
|
||||
map((nameOf): string | undefined =>
|
||||
nameOf?.isSome
|
||||
? u8aToString(nameOf.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber())
|
||||
: undefined
|
||||
)
|
||||
)
|
||||
: of(undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,23 +33,16 @@ function retrieveNick (api: ApiInterfaceRx): (accountId?: AccountId) => Observab
|
||||
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
|
||||
*/
|
||||
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
|
||||
const idAndIndexCall = idAndIndex(api);
|
||||
const nickCall = retrieveNick(api);
|
||||
|
||||
return (address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
|
||||
idAndIndexCall(address).pipe(
|
||||
switchMap(([accountId, accountIndex]): Observable<[DeriveAccountInfo, Option<[Bytes, Balance] & Codec>?]> =>
|
||||
api.derive.accounts.idAndIndex(address).pipe(
|
||||
switchMap(([accountId, accountIndex]): Observable<[DeriveAccountInfo, string?]> =>
|
||||
combineLatest([
|
||||
of({ accountId, accountIndex }),
|
||||
nickCall(accountId)
|
||||
retrieveNick(api, accountId)
|
||||
])
|
||||
),
|
||||
map(([{ accountId, accountIndex }, nameOf]): DeriveAccountInfo => ({
|
||||
accountId,
|
||||
accountIndex,
|
||||
nickname: nameOf && nameOf.isSome
|
||||
? u8aToString(nameOf.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber())
|
||||
: undefined
|
||||
map(([{ accountId, accountIndex }, nickname]): DeriveAccountInfo => ({
|
||||
accountId, accountIndex, nickname
|
||||
})),
|
||||
drr()
|
||||
);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BlockNumber, Index, VestingSchedule } from '@polkadot/types/interfaces';
|
||||
import { DerivedBalances } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { combineLatest, of, Observable } from 'rxjs';
|
||||
@@ -11,9 +12,6 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Option, Vec, createType } from '@polkadot/types';
|
||||
import { bnMax } from '@polkadot/util';
|
||||
|
||||
import { info } from '../accounts/info';
|
||||
import { bestNumber } from '../chain/bestNumber';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util';
|
||||
|
||||
type ResultBalance = [Balance, Balance, BalanceLock[], Option<VestingSchedule>];
|
||||
@@ -78,16 +76,13 @@ function queryBalances (api: ApiInterfaceRx, accountId: AccountId): Observable<R
|
||||
* ```
|
||||
*/
|
||||
export function all (api: ApiInterfaceRx): (address: AccountIndex | AccountId | Address | string) => Observable<DerivedBalances> {
|
||||
const bestNumberCall = bestNumber(api);
|
||||
const infoCall = info(api);
|
||||
|
||||
return (address: AccountIndex | AccountId | Address | string): Observable<DerivedBalances> =>
|
||||
infoCall(address).pipe(
|
||||
api.derive.accounts.info(address).pipe(
|
||||
switchMap(({ accountId }): Observable<Result> =>
|
||||
(accountId
|
||||
? combineLatest([
|
||||
of(accountId),
|
||||
bestNumberCall(),
|
||||
api.derive.chain.bestNumber(),
|
||||
queryBalances(api, accountId),
|
||||
// FIXME This is having issues with Kusama, only use accountNonce atm
|
||||
// api.rpc.account && api.rpc.account.nextIndex
|
||||
|
||||
@@ -9,14 +9,16 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util';
|
||||
import { all } from './all';
|
||||
|
||||
export function votingBalances (api: ApiInterfaceRx): (addresses?: (AccountId | AccountIndex | Address | string)[]) => Observable<DerivedBalances[]> {
|
||||
return (addresses?: (AccountId | AccountIndex | Address | string)[]): Observable<DerivedBalances[]> =>
|
||||
(
|
||||
!addresses || !addresses.length
|
||||
? of([] as DerivedBalances[])
|
||||
: combineLatest(addresses.map(all(api)))
|
||||
: combineLatest(
|
||||
addresses.map((accountId): Observable<DerivedBalances> =>
|
||||
api.derive.balances.all(accountId))
|
||||
)
|
||||
).pipe(
|
||||
drr()
|
||||
);
|
||||
|
||||
@@ -3,28 +3,26 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
import { DerivedBalances } from '../types';
|
||||
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Vec } from '@polkadot/types';
|
||||
|
||||
import { info } from '../accounts/info';
|
||||
import { DerivedBalances } from '../types';
|
||||
import { drr } from '../util';
|
||||
import { votingBalances } from './votingBalances';
|
||||
|
||||
export function votingBalancesNominatorsFor (api: ApiInterfaceRx): (address: AccountId | AccountIndex | Address | string) => Observable<DerivedBalances[]> {
|
||||
const infoCall = info(api);
|
||||
|
||||
return (address: AccountId | AccountIndex | Address | string): Observable<DerivedBalances[]> =>
|
||||
infoCall(address).pipe(
|
||||
api.derive.accounts.info(address).pipe(
|
||||
switchMap(({ accountId }): Observable<AccountId[]> =>
|
||||
accountId
|
||||
? (api.query.staking.nominatorsFor<Vec<AccountId>>(accountId))
|
||||
? api.query.staking.nominatorsFor<Vec<AccountId>>(accountId)
|
||||
: of([] as AccountId[])
|
||||
),
|
||||
switchMap(votingBalances(api)),
|
||||
switchMap((accounts): Observable<DerivedBalances[]> =>
|
||||
api.derive.balances.votingBalances(accounts)
|
||||
),
|
||||
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 { BlockNumber } from '@polkadot/types/interfaces';
|
||||
import { BlockNumber, Header } from '@polkadot/types/interfaces';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
@@ -24,8 +24,8 @@ import { drr } from '../util';
|
||||
*/
|
||||
export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
return (): Observable<BlockNumber> =>
|
||||
api.rpc.chain.subscribeNewHeads().pipe(
|
||||
map((header): BlockNumber => header.number.unwrap()),
|
||||
api.derive.chain.subscribeNewHeads().pipe(
|
||||
map((header: Header): BlockNumber => header.number.unwrap()),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { createType } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { bestNumber } from './bestNumber';
|
||||
import { bestNumberFinalized } from './bestNumberFinalized';
|
||||
|
||||
/**
|
||||
* @name bestNumberLag
|
||||
@@ -27,13 +25,10 @@ import { bestNumberFinalized } from './bestNumberFinalized';
|
||||
* ```
|
||||
*/
|
||||
export function bestNumberLag (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
const bestNumberCall = bestNumber(api);
|
||||
const bestNumberFinalizedCall = bestNumberFinalized(api);
|
||||
|
||||
return (): Observable<BlockNumber> =>
|
||||
combineLatest([
|
||||
bestNumberCall(),
|
||||
bestNumberFinalizedCall()
|
||||
api.derive.chain.bestNumber(),
|
||||
api.derive.chain.bestNumberFinalized()
|
||||
]).pipe(
|
||||
map(([bestNumber, bestNumberFinalized]): BlockNumber =>
|
||||
createType('BlockNumber', bestNumber.sub(bestNumberFinalized))
|
||||
|
||||
@@ -31,7 +31,9 @@ export function getHeader (api: ApiInterfaceRx): (hash: Uint8Array | string) =>
|
||||
return (hash: Uint8Array | string): Observable<HeaderExtended | undefined> =>
|
||||
combineLatest([
|
||||
api.rpc.chain.getHeader(hash),
|
||||
api.query.session.validators.at(hash) as Observable<Vec<AccountId>>
|
||||
api.query.session
|
||||
? api.query.session.validators.at(hash) as Observable<Vec<AccountId>>
|
||||
: of([])
|
||||
]).pipe(
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId } from '@polkadot/types/interfaces';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { Observable, combineLatest } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { Vec } from '@polkadot/types';
|
||||
|
||||
import { HeaderExtended } from '../type';
|
||||
import { drr } from '../util';
|
||||
@@ -29,9 +27,9 @@ export function subscribeNewHeads (api: ApiInterfaceRx): () => Observable<Header
|
||||
return (): Observable<HeaderExtended> =>
|
||||
combineLatest([
|
||||
api.rpc.chain.subscribeNewHeads(),
|
||||
api.query.session.validators<Vec<AccountId>>()
|
||||
api.derive.staking.validators()
|
||||
]).pipe(
|
||||
map(([header, validators]): HeaderExtended =>
|
||||
map(([header, { validators }]): HeaderExtended =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
|
||||
@@ -12,20 +12,15 @@ import { Vec, createType } from '@polkadot/types';
|
||||
|
||||
import { DerivedBalances, DerivedReferendumVote } from '../types';
|
||||
import { drr } from '../util';
|
||||
import { votes } from './votes';
|
||||
import { votingBalances } from '../balances/votingBalances';
|
||||
|
||||
export function referendumVotesFor (api: ApiInterfaceRx): (referendumId: BN | number) => Observable<DerivedReferendumVote[]> {
|
||||
const votesCall = votes(api);
|
||||
const votingBalancesCall = votingBalances(api);
|
||||
|
||||
return (referendumId: BN | number): Observable<DerivedReferendumVote[]> =>
|
||||
api.query.democracy.votersFor<Vec<AccountId>>(referendumId).pipe(
|
||||
switchMap((votersFor): Observable<[Vec<AccountId>, Vote[], DerivedBalances[]]> =>
|
||||
combineLatest([
|
||||
of(votersFor),
|
||||
votesCall(referendumId as BN, votersFor),
|
||||
votingBalancesCall(votersFor)
|
||||
api.derive.democracy.votes(referendumId as BN, votersFor),
|
||||
api.derive.balances.votingBalances(votersFor)
|
||||
])
|
||||
),
|
||||
map(([votersFor, votes, balances]): DerivedReferendumVote[] =>
|
||||
|
||||
@@ -12,11 +12,8 @@ import { Option } from '@polkadot/types';
|
||||
|
||||
import { ReferendumInfoExtended } from '../type';
|
||||
import { drr } from '../util';
|
||||
import { referendumInfos } from './referendumInfos';
|
||||
|
||||
export function referendums (api: ApiInterfaceRx): () => Observable<Option<ReferendumInfoExtended>[]> {
|
||||
const referendumInfosCall = referendumInfos(api);
|
||||
|
||||
return (): Observable<Option<ReferendumInfoExtended>[]> =>
|
||||
api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
|
||||
api.query.democracy.nextTally,
|
||||
@@ -24,7 +21,7 @@ export function referendums (api: ApiInterfaceRx): () => Observable<Option<Refer
|
||||
]).pipe(
|
||||
switchMap(([nextTally, referendumCount]): Observable<Option<ReferendumInfoExtended>[]> =>
|
||||
referendumCount && nextTally && referendumCount.gt(nextTally) && referendumCount.gtn(0)
|
||||
? referendumInfosCall(
|
||||
? api.derive.democracy.referendumInfos(
|
||||
[...Array(referendumCount.sub(nextTally).toNumber())].map((_, i): BN =>
|
||||
nextTally.addn(i)
|
||||
)
|
||||
|
||||
@@ -35,6 +35,7 @@ export function voterPositions (api: ApiInterfaceRx): () => Observable<DerivedVo
|
||||
)),
|
||||
map((result: [BN, Vec<Option<AccountId>>[]]): DerivedVoterPositions => {
|
||||
const [setSize, voters] = result;
|
||||
|
||||
return voters.reduce((result: DerivedVoterPositions, vec, setIndex): DerivedVoterPositions => {
|
||||
vec.forEach((e, index): void => {
|
||||
// re-create the index based on position 0 is [0][0] and likewise
|
||||
|
||||
@@ -11,7 +11,6 @@ import { map } from 'rxjs/operators';
|
||||
import { createType, Vec } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { voterPositions } from './voterPositions';
|
||||
|
||||
/**
|
||||
* @name voters
|
||||
@@ -26,10 +25,8 @@ import { voterPositions } from './voterPositions';
|
||||
* ```
|
||||
*/
|
||||
export function voters (api: ApiInterfaceRx): () => Observable<Vec<AccountId>> {
|
||||
const voterPositionsCall = voterPositions(api);
|
||||
|
||||
return (): Observable<Vec<AccountId>> =>
|
||||
voterPositionsCall().pipe(
|
||||
api.derive.elections.voterPositions().pipe(
|
||||
map((voterPositions: DerivedVoterPositions): Vec<AccountId> =>
|
||||
createType(
|
||||
'Vec<AccountId>',
|
||||
|
||||
@@ -10,18 +10,15 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Bytes, Option, u32 } from '@polkadot/types';
|
||||
|
||||
import { overview as stakingOverview } from '../staking';
|
||||
import { drr } from '../util';
|
||||
|
||||
/**
|
||||
* @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session
|
||||
*/
|
||||
export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DerivedHeartbeats> {
|
||||
const stakingOverviewCall = stakingOverview(api);
|
||||
|
||||
return (): Observable<DerivedHeartbeats> =>
|
||||
api.query.imOnline && api.query.imOnline.receivedHeartbeats && api.query.imOnline.authoredBlocks
|
||||
? stakingOverviewCall().pipe(
|
||||
api.query.imOnline?.receivedHeartbeats && api.query.imOnline.authoredBlocks
|
||||
? api.derive.staking.overview().pipe(
|
||||
switchMap(({ currentIndex, validators }): Observable<[AccountId[], Option<Bytes>[], u32[]]> =>
|
||||
combineLatest([
|
||||
of(validators),
|
||||
|
||||
@@ -3,20 +3,18 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
import { DerivedSessionInfo } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { info } from './info';
|
||||
|
||||
export function eraLength (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
const infoCall = info(api);
|
||||
|
||||
return (): Observable<BlockNumber> =>
|
||||
infoCall().pipe(
|
||||
map(({ eraLength }): BlockNumber => eraLength),
|
||||
api.derive.session.info().pipe(
|
||||
map(({ eraLength }: DerivedSessionInfo): BlockNumber => eraLength),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,20 +3,18 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
import { DerivedSessionInfo } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { info } from './info';
|
||||
|
||||
export function eraProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
const infoCall = info(api);
|
||||
|
||||
return (): Observable<BlockNumber> =>
|
||||
infoCall().pipe(
|
||||
map(({ eraProgress }): BlockNumber => eraProgress),
|
||||
api.derive.session.info().pipe(
|
||||
map(({ eraProgress }: DerivedSessionInfo): BlockNumber => eraProgress),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import { Option, u64, createType } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { bestNumber } from '../chain';
|
||||
import { indexes } from './indexes';
|
||||
|
||||
type ResultV1Session = [Option<BlockNumber>, BlockNumber, BlockNumber, SessionIndex];
|
||||
type ResultV1 = [BlockNumber, DeriveSessionIndexes, ResultV1Session];
|
||||
@@ -21,14 +19,9 @@ type ResultSlots = [u64, u64, u64, SessionIndex];
|
||||
type ResultType = [boolean, u64, SessionIndex];
|
||||
type Result = [ResultType, DeriveSessionIndexes, ResultSlots];
|
||||
|
||||
interface Calls {
|
||||
bestNumberCall (): Observable<BlockNumber>;
|
||||
indexesCall (): Observable<DeriveSessionIndexes>;
|
||||
}
|
||||
|
||||
// internal helper to just split the logic - take all inputs, do the calculations and combine
|
||||
function createDerivedV1 ([bestNumber, { currentIndex, validatorCount }, [_lastLengthChange, sessionLength, lastEraLengthChange, sessionsPerEra]]: ResultV1): DerivedSessionInfo {
|
||||
const lastLengthChange = (_lastLengthChange && _lastLengthChange.unwrapOr(null)) || createType('BlockNumber');
|
||||
const lastLengthChange = _lastLengthChange?.unwrapOr(null) || createType('BlockNumber');
|
||||
const sessionProgress = bestNumber
|
||||
.sub(lastLengthChange)
|
||||
.add(sessionLength)
|
||||
@@ -77,10 +70,10 @@ function createDerivedLatest ([[hasBabe, epochDuration, sessionsPerEra], { curre
|
||||
};
|
||||
}
|
||||
|
||||
function infoV1 (api: ApiInterfaceRx, { bestNumberCall, indexesCall }: Calls): Observable<DerivedSessionInfo> {
|
||||
function infoV1 (api: ApiInterfaceRx): Observable<DerivedSessionInfo> {
|
||||
return combineLatest([
|
||||
bestNumberCall(),
|
||||
indexesCall(),
|
||||
api.derive.chain.bestNumber(),
|
||||
api.derive.session.indexes(),
|
||||
api.queryMulti<ResultV1Session>([
|
||||
api.query.session.lastLengthChange,
|
||||
api.query.session.sessionLength,
|
||||
@@ -92,8 +85,8 @@ function infoV1 (api: ApiInterfaceRx, { bestNumberCall, indexesCall }: Calls): O
|
||||
);
|
||||
}
|
||||
|
||||
function infoLatestAura (api: ApiInterfaceRx, { indexesCall }: Calls): Observable<DerivedSessionInfo> {
|
||||
return indexesCall().pipe(
|
||||
function infoLatestAura (api: ApiInterfaceRx): Observable<DerivedSessionInfo> {
|
||||
return api.derive.session.indexes().pipe(
|
||||
map((indexes): DerivedSessionInfo =>
|
||||
createDerivedLatest([
|
||||
[false, createType('u64', 1), api.consts.staking.sessionsPerEra as SessionIndex],
|
||||
@@ -104,9 +97,9 @@ function infoLatestAura (api: ApiInterfaceRx, { indexesCall }: Calls): Observabl
|
||||
);
|
||||
}
|
||||
|
||||
function infoLatestBabe (api: ApiInterfaceRx, { indexesCall }: Calls): Observable<DerivedSessionInfo> {
|
||||
function infoLatestBabe (api: ApiInterfaceRx): Observable<DerivedSessionInfo> {
|
||||
return combineLatest([
|
||||
indexesCall(),
|
||||
api.derive.session.indexes(),
|
||||
api.queryMulti<ResultSlots>([
|
||||
api.query.babe.currentSlot,
|
||||
api.query.babe.epochIndex,
|
||||
@@ -128,10 +121,6 @@ function infoLatestBabe (api: ApiInterfaceRx, { indexesCall }: Calls): Observabl
|
||||
* @description Retrieves all the session and era info and calculates specific values on it as the length of the session and eras
|
||||
*/
|
||||
export function info (api: ApiInterfaceRx): () => Observable<DerivedSessionInfo> {
|
||||
const calls = {
|
||||
bestNumberCall: bestNumber(api),
|
||||
indexesCall: indexes(api)
|
||||
};
|
||||
const query = api.consts.staking
|
||||
? api.consts.babe
|
||||
? infoLatestBabe // 2.x with Babe
|
||||
@@ -139,5 +128,5 @@ export function info (api: ApiInterfaceRx): () => Observable<DerivedSessionInfo>
|
||||
: infoV1;
|
||||
|
||||
return (): Observable<DerivedSessionInfo> =>
|
||||
query(api, calls).pipe(drr());
|
||||
query(api).pipe(drr());
|
||||
}
|
||||
|
||||
@@ -3,20 +3,18 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { BlockNumber } from '@polkadot/types/interfaces';
|
||||
import { DerivedSessionInfo } from '../types';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { info } from './info';
|
||||
|
||||
export function sessionProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
|
||||
const infoCall = info(api);
|
||||
|
||||
return (): Observable<BlockNumber> =>
|
||||
infoCall().pipe(
|
||||
map(({ sessionProgress }): BlockNumber => sessionProgress),
|
||||
api.derive.session.info().pipe(
|
||||
map(({ sessionProgress }: DerivedSessionInfo): BlockNumber => sessionProgress),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -15,17 +15,8 @@ import { createType, Option, Vec } from '@polkadot/types';
|
||||
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
import { bestNumber } from '../chain/bestNumber';
|
||||
import { eraLength } from '../session/eraLength';
|
||||
import { recentlyOffline } from './recentlyOffline';
|
||||
import { drr } from '../util';
|
||||
|
||||
interface Calls {
|
||||
bestNumberCall (): Observable<BlockNumber>;
|
||||
eraLengthCall (): Observable<BlockNumber>;
|
||||
recentlyOfflineCall (): Observable<DerivedRecentlyOffline>;
|
||||
}
|
||||
|
||||
interface ParseInput {
|
||||
accountId: AccountId;
|
||||
bestNumber: BlockNumber;
|
||||
@@ -147,11 +138,11 @@ function parseResult ({ accountId, controllerId, stashId, eraLength, bestNumber,
|
||||
|
||||
type MultiResultV1 = [Option<AccountId>, Option<StakingLedger>, [Vec<AccountId>], RewardDestination, Exposure, [ValidatorPrefs] & Codec];
|
||||
|
||||
function retrieveInfoV1 (api: ApiInterfaceRx, { bestNumberCall, eraLengthCall, recentlyOfflineCall }: Calls, accountId: AccountId, stashId: AccountId, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
function retrieveInfoV1 (api: ApiInterfaceRx, accountId: AccountId, stashId: AccountId, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
return combineLatest([
|
||||
bestNumberCall(),
|
||||
eraLengthCall(),
|
||||
recentlyOfflineCall(),
|
||||
api.derive.chain.bestNumber(),
|
||||
api.derive.session.eraLength(),
|
||||
api.derive.staking.recentlyOffline(),
|
||||
api.queryMulti([
|
||||
[api.query.session.nextKeyFor, controllerId],
|
||||
[api.query.staking.ledger, controllerId],
|
||||
@@ -172,10 +163,10 @@ function retrieveInfoV1 (api: ApiInterfaceRx, { bestNumberCall, eraLengthCall, r
|
||||
|
||||
type MultiResultV2 = [Option<StakingLedger>, [Vec<AccountId>], RewardDestination, Exposure, [ValidatorPrefs], Option<Keys>];
|
||||
|
||||
function retrieveInfoV2 (api: ApiInterfaceRx, { bestNumberCall, eraLengthCall }: Calls, accountId: AccountId, stashId: AccountId, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
function retrieveInfoV2 (api: ApiInterfaceRx, accountId: AccountId, stashId: AccountId, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
return combineLatest([
|
||||
bestNumberCall(),
|
||||
eraLengthCall(),
|
||||
api.derive.chain.bestNumber(),
|
||||
api.derive.session.eraLength(),
|
||||
api.query.session.queuedKeys<Vec<[AccountId, Keys] & Codec>>(),
|
||||
api.queryMulti([
|
||||
[api.query.staking.ledger, controllerId],
|
||||
@@ -195,25 +186,25 @@ function retrieveInfoV2 (api: ApiInterfaceRx, { bestNumberCall, eraLengthCall }:
|
||||
));
|
||||
}
|
||||
|
||||
function retrieveV1 (api: ApiInterfaceRx, calls: Calls, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
function retrieveV1 (api: ApiInterfaceRx, controllerId: AccountId): Observable<DerivedStaking> {
|
||||
return api.query.staking
|
||||
.ledger<Option<StakingLedger>>(controllerId)
|
||||
.pipe(
|
||||
switchMap((stakingLedger): Observable<DerivedStaking> =>
|
||||
stakingLedger.isSome
|
||||
? retrieveInfoV1(api, calls, controllerId, stakingLedger.unwrap().stash, controllerId)
|
||||
? retrieveInfoV1(api, controllerId, stakingLedger.unwrap().stash, controllerId)
|
||||
: of({ accountId: controllerId, nextSessionIds: [], sessionIds: [] })
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function retrieveV2 (api: ApiInterfaceRx, calls: Calls, stashId: AccountId): Observable<DerivedStaking> {
|
||||
function retrieveV2 (api: ApiInterfaceRx, stashId: AccountId): Observable<DerivedStaking> {
|
||||
return api.query.staking
|
||||
.bonded<Option<AccountId>>(stashId)
|
||||
.pipe(
|
||||
switchMap((controllerId): Observable<DerivedStaking> =>
|
||||
controllerId.isSome
|
||||
? retrieveInfoV2(api, calls, stashId, stashId, controllerId.unwrap())
|
||||
? retrieveInfoV2(api, stashId, stashId, controllerId.unwrap())
|
||||
: of({ accountId: stashId, nextSessionIds: [], sessionIds: [] })
|
||||
)
|
||||
);
|
||||
@@ -223,15 +214,10 @@ function retrieveV2 (api: ApiInterfaceRx, calls: Calls, stashId: AccountId): Obs
|
||||
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
|
||||
*/
|
||||
export function info (api: ApiInterfaceRx): (_accountId: Uint8Array | string) => Observable<DerivedStaking> {
|
||||
const calls = {
|
||||
eraLengthCall: eraLength(api),
|
||||
bestNumberCall: bestNumber(api),
|
||||
recentlyOfflineCall: recentlyOffline(api)
|
||||
};
|
||||
const query = api.consts.session
|
||||
? retrieveV2
|
||||
: retrieveV1;
|
||||
|
||||
return (accountId: Uint8Array | string): Observable<DerivedStaking> =>
|
||||
query(api, calls, createType('AccountId', accountId)).pipe(drr());
|
||||
query(api, createType('AccountId', accountId)).pipe(drr());
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ type ResultV2 = [Option<AccountId>, ITuple<[Vec<AccountId>]>, RewardDestination,
|
||||
function parse (stashId: AccountId, [_controllerId, [nominators], rewardDestination, stakers, validatorPrefs, _nextKeys]: Result): DerivedStakingStash {
|
||||
return {
|
||||
controllerId: _controllerId.unwrapOr(undefined),
|
||||
nextKeys: _nextKeys && _nextKeys.unwrapOr(undefined),
|
||||
nextKeys: _nextKeys?.unwrapOr(undefined),
|
||||
nominators,
|
||||
rewardDestination,
|
||||
stakers,
|
||||
|
||||
@@ -11,20 +11,15 @@ import { map, switchMap } from 'rxjs/operators';
|
||||
import { createType } from '@polkadot/types';
|
||||
|
||||
import { drr } from '../util';
|
||||
import { indexes as sessionIndexes } from '../session';
|
||||
import { validators } from './validators';
|
||||
|
||||
/**
|
||||
* @description Retrieve the staking overview, including elected and points earned
|
||||
*/
|
||||
export function overview (api: ApiInterfaceRx): () => Observable<DerivedStakingOverview> {
|
||||
const sessionIndexesCall = sessionIndexes(api);
|
||||
const validatorsCall = validators(api);
|
||||
|
||||
return (): Observable<DerivedStakingOverview> =>
|
||||
combineLatest([
|
||||
sessionIndexesCall(),
|
||||
validatorsCall()
|
||||
api.derive.session.indexes(),
|
||||
api.derive.staking.validators()
|
||||
]).pipe(
|
||||
switchMap(([{ currentEra, currentIndex, validatorCount }, { currentElected, validators }]) =>
|
||||
combineLatest([
|
||||
|
||||
@@ -7,6 +7,8 @@ import { AccountId, AccountIndex, Balance, BalanceLock, BlockNumber, EraIndex, E
|
||||
import BN from 'bn.js';
|
||||
import { u32 } from '@polkadot/types';
|
||||
|
||||
export type AccountIndexes = Record<string, AccountIndex>;
|
||||
|
||||
export interface DeriveAccountInfo {
|
||||
accountId?: AccountId;
|
||||
accountIndex?: AccountIndex;
|
||||
|
||||
@@ -356,11 +356,15 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
|
||||
);
|
||||
}
|
||||
|
||||
protected decorateDerive (apiRx: ApiInterfaceRx, decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<ApiType, ExactDerive> {
|
||||
protected decorateDeriveRx (decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<'rxjs', ExactDerive> {
|
||||
// Pull in derive from api-derive
|
||||
const derive = decorateDerive(apiRx, this._options.derives);
|
||||
const derive = decorateDerive(this._rx as ApiInterfaceRx, this._options.derives);
|
||||
|
||||
return decorateSections<ApiType, ExactDerive>(derive, decorateMethod);
|
||||
return decorateSections<'rxjs', ExactDerive>(derive, decorateMethod);
|
||||
}
|
||||
|
||||
protected decorateDerive (decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<ApiType, ExactDerive> {
|
||||
return decorateSections<ApiType, ExactDerive>(this._rx.derive as DeriveAllSections<'rxjs', ExactDerive>, decorateMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import { SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { RegistryTypes } from '@polkadot/types/types';
|
||||
import { ApiBase, ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod } from '../types';
|
||||
import { ApiBase, ApiOptions, ApiTypes, DecorateMethod } from '../types';
|
||||
|
||||
import constantsFromMeta from '@polkadot/api-metadata/consts/fromMetadata';
|
||||
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
|
||||
@@ -195,7 +195,8 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
this._rx.consts = constants;
|
||||
|
||||
// derive is last, since it uses the decorated rx
|
||||
this._derive = this.decorateDerive(this._rx as ApiInterfaceRx, this.decorateMethod);
|
||||
this._rx.derive = this.decorateDeriveRx(this.rxDecorateMethod);
|
||||
this._derive = this.decorateDerive(this.decorateMethod);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ import { UserRpc } from '@polkadot/rpc-core/types';
|
||||
import { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
import { AnyFunction, Callback, CallFunction, Codec, CodecArg, RegistryTypes, SignatureOptions, SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
|
||||
import { SubmittableResultImpl, SubmittableExtrinsic } from './submittable/types';
|
||||
import { DeriveAllSections } from './util/decorate';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { DeriveCustom } from '@polkadot/api-derive';
|
||||
import { DeriveCustom, ExactDerive } from '@polkadot/api-derive';
|
||||
import { Constants } from '@polkadot/api-metadata/consts/types';
|
||||
import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types';
|
||||
import { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
|
||||
@@ -216,6 +217,8 @@ export interface ApiOptions {
|
||||
// A smaller interface of ApiRx, used in derive and in SubmittableExtrinsic
|
||||
export interface ApiInterfaceRx {
|
||||
consts: Constants;
|
||||
// TODO This needs to be typed correctly
|
||||
derive: DeriveAllSections<'rxjs', ExactDerive>;
|
||||
extrinsicType: number;
|
||||
genesisHash: Hash;
|
||||
hasSubscriptions: boolean;
|
||||
|
||||
@@ -33,7 +33,7 @@ const NOOP = (): void => {};
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
export const drr = ({ skipChange, skipTimeout }: Options = {}): DrrResult => <T> (source$: Observable<T>): Observable<T> =>
|
||||
export const drr = ({ skipChange = false, skipTimeout = false }: Options = {}): DrrResult => <T> (source$: Observable<T>): Observable<T> =>
|
||||
source$.pipe(
|
||||
catchError(ERR),
|
||||
skipChange
|
||||
|
||||
@@ -2,54 +2,47 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
// From https://medium.com/@volkeron/rxjs-unsubscribe-delay-218a9ab2672e (adapted for rxjs 6)
|
||||
// v6 inspiration from https://github.com/cartant/rxjs-etc/blob/7d7cd21233f67c364fd246ef72077bbe494dd5c2/source/operators/refCountDelay.ts
|
||||
|
||||
import { asapScheduler, ConnectableObservable, MonoTypeOperatorFunction, NEVER, Observable, Subject, Subscription, timer, using } from 'rxjs';
|
||||
import { scan, switchMap, tap } from 'rxjs/operators';
|
||||
import { asapScheduler, ConnectableObservable, MonoTypeOperatorFunction, Observable, Subscription } from 'rxjs';
|
||||
|
||||
const DELAY = 1750;
|
||||
|
||||
// subscribe to a source, tracker and increment the counter
|
||||
function subscribe <T> (source: Observable<T>, counter: Subject<number>, tracker: Observable<number>): [Subscription, Subscription] {
|
||||
const sourceConn = (source as ConnectableObservable<T>).connect();
|
||||
const trackerConn = tracker.subscribe();
|
||||
function refCountDelayInner <T> (source: Observable<T>): Observable<T> {
|
||||
let connected = 0; // 0 = disconnected, 1 = disconnecting, 2 = connecting, 3 = connected
|
||||
let refCount = 0;
|
||||
let con = Subscription.EMPTY;
|
||||
let sched = Subscription.EMPTY;
|
||||
|
||||
counter.next(1);
|
||||
return new Observable((ob) => {
|
||||
source.subscribe(ob);
|
||||
|
||||
return [sourceConn, trackerConn];
|
||||
if (refCount++ === 0) {
|
||||
if (connected === 1) {
|
||||
connected = 3;
|
||||
sched.unsubscribe();
|
||||
} else {
|
||||
con = (source as ConnectableObservable<T>).connect();
|
||||
connected = 3;
|
||||
}
|
||||
}
|
||||
|
||||
return (): void => {
|
||||
if (--refCount === 0) {
|
||||
if (connected === 2) {
|
||||
connected = 0;
|
||||
sched.unsubscribe();
|
||||
} else {
|
||||
// connected === 3
|
||||
connected = 1;
|
||||
sched = asapScheduler.schedule((): void => {
|
||||
con.unsubscribe();
|
||||
connected = 0;
|
||||
}, DELAY);
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// unsubscribe from both the tracker and the source
|
||||
function unsubscribe (sourceConn: Subscription | undefined, trackerConn: Subscription | undefined): void {
|
||||
sourceConn && sourceConn.unsubscribe();
|
||||
trackerConn && trackerConn.unsubscribe();
|
||||
}
|
||||
|
||||
// keep track of references, but only unsubscribe after the configured delay
|
||||
export function refCountDelay <T> (): MonoTypeOperatorFunction<T> {
|
||||
return (source: Observable<T>): Observable<T> => {
|
||||
let sourceConn: Subscription | undefined;
|
||||
let trackerConn: Subscription | undefined;
|
||||
const counter = new Subject<number>();
|
||||
const tracker = counter.pipe(
|
||||
scan((total, change) => change + total, 0),
|
||||
switchMap((count) =>
|
||||
count === 0 // when we have a zero count, schedule a unsubscribe
|
||||
? timer(DELAY, asapScheduler).pipe(tap((): void => unsubscribe(sourceConn, trackerConn)))
|
||||
: NEVER
|
||||
)
|
||||
);
|
||||
|
||||
return using(
|
||||
() => {
|
||||
[sourceConn, trackerConn] = subscribe(source, counter, tracker);
|
||||
|
||||
return {
|
||||
unsubscribe: (): void => counter.next(-1)
|
||||
};
|
||||
},
|
||||
(): Observable<T> => source
|
||||
);
|
||||
};
|
||||
return refCountDelayInner;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user