Skip to content
Snippets Groups Projects
Unverified Commit 4a3d9925 authored by Zeke Mostov's avatar Zeke Mostov
Browse files

Make more robust for all scenarios

parent 71eae4c3
Branches 271
No related merge requests found
import { ApiPromise } from '@polkadot/api';
import { Option } from '@polkadot/types';
import * as BN from 'bn.js';
import { RequestHandler } from 'express';
import { BadRequest, InternalServerError } from 'http-errors';
import { validateAddress } from '../../middleware';
import { AccountsStakingPayoutsService } from '../../services';
import { isOption } from '../../types/polkadot-js';
import { IAddressParam } from '../../types/requests';
import AbstractController from '../AbstractController';
......@@ -130,7 +130,7 @@ export default class AccountsStakingPayoutsController extends AbstractController
const activeEra = activeEraOption.unwrap().index.toNumber();
let currentEra;
if (isOption(currentEraMaybeOption)) {
if (currentEraMaybeOption instanceof Option) {
if (currentEraMaybeOption.isNone) {
throw new InternalServerError(
'CurrentEra is None when Some was expected'
......@@ -138,9 +138,13 @@ export default class AccountsStakingPayoutsController extends AbstractController
}
currentEra = currentEraMaybeOption.unwrap().toNumber();
} else if (currentEraMaybeOption instanceof BN) {
} else if ((currentEraMaybeOption as unknown) instanceof BN) {
// EraIndex extends u32, which extends BN so this should always be true
currentEra = currentEraMaybeOption.toNumber();
currentEra = (currentEraMaybeOption as BN).toNumber();
} else {
throw new InternalServerError(
'Query for current_era returned a non-processable result.'
);
}
if (era !== undefined && era > activeEra - 1) {
......@@ -155,7 +159,7 @@ export default class AccountsStakingPayoutsController extends AbstractController
return {
hash,
eraArg: era === undefined ? activeEra - 1 : era,
currentEra: currentEra as number,
currentEra,
};
}
}
import { Option } from '@polkadot/types';
import { Codec } from '@polkadot/types/types';
export { Option } from '@polkadot/types';
/**
* Check if something is a polkadot-js Option
*
* @param thing something that might be a polkadot-js Option
*/
export function isOption<T extends Codec>(thing: unknown): thing is Option<T> {
return thing instanceof Option;
}
export * from './AnyJson';
export * from './Codec';
export * from './ToJSONable';
export * from './Option';
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment