Skip to content

Commit

Permalink
add: formalize era as an mandatory option
Browse files Browse the repository at this point in the history
  • Loading branch information
pLabarta committed Jan 16, 2024
1 parent 1ed4f13 commit ffe2769
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 19 additions & 0 deletions cli/src/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ function positiveBigNumberFromString(amount: any) {
return parsedValue;
}

// Session
export const eraOption = new Option('--era [era]', 'Specify era to distribute rewards for').argParser(parseEra);

// Era parsing
export function parseEra(value: string): number {
// Only positive integers are allowed
const parsedEra = parseInt(value, 10);

if (isNaN(parsedEra)) {
throw new InvalidArgumentError('Not a valid era.');
}

if (parsedEra < 0) {
throw new InvalidArgumentError('Era must be a positive integer.');
}

return parsedEra;
}

// I/O
export const jsonOption = new Option('--json', 'Output as JSON');
export const noInputOption = new Option('--no-input', 'Do not prompt for input');
Expand Down
13 changes: 3 additions & 10 deletions cli/src/commands/staking/distribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { initCallerKeyring } from '../../lib/account/keyring';
import { requiredInput, parseIntegerOrExit } from '../../lib/parsing';

Check warning on line 4 in cli/src/commands/staking/distribute.ts

View workflow job for this annotation

GitHub Actions / javascript-lint / cli

'requiredInput' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in cli/src/commands/staking/distribute.ts

View workflow job for this annotation

GitHub Actions / javascript-lint / cli

'parseIntegerOrExit' is defined but never used. Allowed unused vars must match /^_/u
import { requireEnoughFundsToSend, signSendAndWatch } from '../../lib/tx';
import { checkEraIsInHistory } from '../../lib/staking/era';
import { substrateAddressOption } from '../options';
import { eraOption, substrateAddressOption } from '../options';

export function makeDistributeRewardsCommand() {
const cmd = new Command('distribute-rewards');
cmd.description('Distribute all pending rewards for a particular validator');
cmd.addOption(substrateAddressOption.makeOptionMandatory());
cmd.option('-e, --era [era]', 'Specify era to distribute rewards for');
cmd.addOption(eraOption.makeOptionMandatory());
cmd.action(distributeRewardsAction);
return cmd;
}
Expand Down Expand Up @@ -43,13 +43,6 @@ async function distributeRewardsAction(options: OptionValues) {

function parseOptions(options: OptionValues) {
const validator = options.substrateAddress as string;

const era = parseIntegerOrExit(requiredInput(options.era, 'Failed to distribute rewards: Must specify an era'));

if (era < 0) {
console.error(`Failed to distribute rewards: Era ${era} is invalid; must be a positive integer`);
process.exit(1);
}

const era = options.era as number;
return { validator, era };
}

0 comments on commit ffe2769

Please sign in to comment.