Skip to content

Commit

Permalink
fix: update eraElectionStatus for runtime upgrade v30 (#485)
Browse files Browse the repository at this point in the history
* fix: docs

* build: docs

* fix: return message

* fix: remove try catch, and replace with simple conditional

* Update docs/src/openapi-v1.yaml

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>

* Update src/services/pallets/PalletsStakingProgressService.ts

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>

* Update src/services/pallets/PalletsStakingProgressService.ts

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>

* add string to union type for electionStatus

* fix: return value for electionStatus

* fix: build docs

* fix: remove `erElectionStatus &&`

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>
  • Loading branch information
TarikGul and emostov committed Mar 29, 2021
1 parent b8e8292 commit 415f030
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,8 @@ components:
status:
type: object
description: >-
[Deprecated](Works for polkadot runtimes before v0.8.30).
Era election status: either `Close: null` or `Open: <BlockNumber>`.
A status of `Close` indicates that the submission window for solutions
from off-chain Phragmen is not open. A status of `Open` indicates that the
Expand Down
25 changes: 18 additions & 7 deletions src/services/pallets/PalletsStakingProgressService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ export class PalletsStakingProgressService extends AbstractService {
const [
validatorCount,
forceEra,
eraElectionStatus,
validators,
{ number },
] = await Promise.all([
api.query.staking.validatorCount.at(hash),
api.query.staking.forceEra.at(hash),
api.query.staking.eraElectionStatus.at(hash),
api.query.session.validators.at(hash),
api.rpc.chain.getHeader(hash),
]);

let eraElectionStatus;
/**
* Polkadot runtimes v0.8.30 and above do not support eraElectionStatus, so we check
* to see if eraElectionStatus is mounted to the api, and if were running on a
* runtime less than v0.8.30 it will return a successful result. If it doesn't
* we do nothing and let `eraElectionStatus` stay undefined.
*/
if (api.query.staking.eraElectionStatus) {
eraElectionStatus = await api.query.staking.eraElectionStatus.at(hash);
}

const {
eraLength,
eraProgress,
Expand Down Expand Up @@ -88,7 +97,7 @@ export class PalletsStakingProgressService extends AbstractService {
if (electionLookAhead.eq(new BN(0))) {
// no offchain solutions accepted
toggle = null;
} else if ((eraElectionStatus as { isClose?: boolean }).isClose) {
} else if ((eraElectionStatus as { isClose?: boolean })?.isClose) {
// election window is yet to open
toggle = nextCurrentEra.sub(electionLookAhead);
} else {
Expand All @@ -99,10 +108,12 @@ export class PalletsStakingProgressService extends AbstractService {
return {
...baseResponse,
nextActiveEraEstimate: nextActiveEra.toString(10),
electionStatus: {
status: eraElectionStatus.toJSON(),
toggleEstimate: toggle?.toString(10) ?? null,
},
electionStatus: eraElectionStatus
? {
status: eraElectionStatus.toJSON(),
toggleEstimate: toggle?.toString(10) ?? null,
}
: 'Deprecated, see docs',
idealValidatorCount: validatorCount.toString(10),
validatorSet: validators.map((accountId) => accountId.toString()),
};
Expand Down
10 changes: 6 additions & 4 deletions src/types/responses/PalletStakingProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export interface IPalletStakingProgress {
nextActiveEraEstimate?: AnyJson;
nextSessionEstimate: string | null;
unappliedSlashes: AnyJson[] | null;
electionStatus?: {
status: AnyJson;
toggleEstimate: string | null;
};
electionStatus?:
| {
status: AnyJson;
toggleEstimate: string | null;
}
| string;
validatorSet?: string[] | null;
}

0 comments on commit 415f030

Please sign in to comment.