Skip to content

Commit

Permalink
fix: confirm session module before block retrieval (#744)
Browse files Browse the repository at this point in the history
* fix session module failing for moonbeam

* cleanup

* Update src/services/blocks/BlocksService.ts
  • Loading branch information
TarikGul committed Nov 1, 2021
1 parent 30d4467 commit e6613a0
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { extractAuthor } from '@polkadot/api-derive/type/util';
import { Compact, GenericCall, Struct, Vec } from '@polkadot/types';
import { AbstractInt } from '@polkadot/types/codec/AbstractInt';
import {
AccountId32,
Block,
BlockHash,
BlockNumber,
Expand Down Expand Up @@ -102,7 +103,7 @@ export class BlocksService extends AbstractService {

const [{ block }, validators, events, finalizedHead] = await Promise.all([
api.rpc.chain.getBlock(hash),
historicApi.query.session.validators(),
this.fetchValidators(historicApi),
this.fetchEvents(historicApi),
queryFinalizedHead
? api.rpc.chain.getFinalizedHead()
Expand Down Expand Up @@ -631,8 +632,7 @@ export class BlocksService extends AbstractService {
/**
* Fetch events for the specified block.
*
* @param api ApiPromise to use for query
* @param hash `BlockHash` to make query at
* @param historicApi ApiDecoration to use for the query
*/
private async fetchEvents(
historicApi: ApiDecoration<'promise'>
Expand All @@ -644,6 +644,20 @@ export class BlocksService extends AbstractService {
}
}

/**
* Checks to see if the current chain has the session module, then retrieve all
* validators.
*
* @param historicApi ApiDecoration to use for the query
*/
private async fetchValidators(
historicApi: ApiDecoration<'promise'>
): Promise<Vec<AccountId32>> {
return historicApi.query.session
? await historicApi.query.session.validators()
: ([] as unknown as Vec<AccountId32>);
}

/**
* Helper function for `parseGenericCall`.
*
Expand Down

0 comments on commit e6613a0

Please sign in to comment.