diff --git a/packages/api-derive/src/session/indexes.ts b/packages/api-derive/src/session/indexes.ts index 199a358a24..a1150a2661 100644 --- a/packages/api-derive/src/session/indexes.ts +++ b/packages/api-derive/src/session/indexes.ts @@ -6,19 +6,24 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { EraIndex, SessionIndex } from '@polkadot/types/interfaces'; import { DeriveSessionIndexes } from '../types'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; -import { u32 } from '@polkadot/types'; +import { createType, u32 as U32 } from '@polkadot/types'; import { drr } from '../util'; export function indexes (api: ApiInterfaceRx): () => Observable { return (): Observable => - api.queryMulti<[SessionIndex, EraIndex, u32]>([ - api.query.session.currentIndex, - api.query.staking.currentEra, - api.query.staking.validatorCount - ]).pipe( + ( + // Some chains (eg. very limited node-template), does not have session + api.query.session && api.query.staking + ? api.queryMulti<[SessionIndex, EraIndex, U32]>([ + api.query.session.currentIndex, + api.query.staking.currentEra, + api.query.staking.validatorCount + ]) + : of([createType('SessionIndex', 1), createType('EraIndex', 1), new U32()]) + ).pipe( map(([currentIndex, currentEra, validatorCount]): DeriveSessionIndexes => ({ currentIndex, currentEra, validatorCount })), diff --git a/packages/api-derive/src/staking/overview.ts b/packages/api-derive/src/staking/overview.ts index 5becb59928..08a5352c80 100644 --- a/packages/api-derive/src/staking/overview.ts +++ b/packages/api-derive/src/staking/overview.ts @@ -25,7 +25,7 @@ export function overview (api: ApiInterfaceRx): () => Observable(currentEra) : of(createType('EraPoints')) ]) diff --git a/packages/api-derive/src/staking/validators.ts b/packages/api-derive/src/staking/validators.ts index cca6f7e55a..6c2d0e3c2a 100644 --- a/packages/api-derive/src/staking/validators.ts +++ b/packages/api-derive/src/staking/validators.ts @@ -6,7 +6,7 @@ import { AccountId } from '@polkadot/types/interfaces'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { DeriveStakingValidators } from '../types'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; import { Vec } from '@polkadot/types'; @@ -17,10 +17,16 @@ import { drr } from '../util'; */ export function validators (api: ApiInterfaceRx): () => Observable { return (): Observable => - api.queryMulti<[Vec, Vec]>([ - api.query.session.validators, - api.query.staking.currentElected - ]).pipe( + ( + // Sadly the node-template is (for some obscure reason) not comprehensive, so while the derive works + // in all actual real-world deployed chains, it does create some confusion for limited template chains + api.query.session && api.query.staking + ? api.queryMulti<[Vec, Vec]>([ + api.query.session.validators, + api.query.staking.currentElected + ]) + : of([[], []] as [AccountId[], AccountId[]]) + ).pipe( map(([validators, currentElected]): DeriveStakingValidators => ({ currentElected, validators })),