Skip to content

Commit

Permalink
Usage cleanups (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr authored Apr 15, 2023
1 parent 6408c05 commit d9a9e5d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 143 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"test": "yarn run:api --help && yarn run:json --help && yarn run:metadata --help && yarn run:monitor --help && yarn run:signer --help && yarn run:vanity --help"
},
"devDependencies": {
"@polkadot/dev": "^0.72.39",
"@polkadot/dev": "^0.72.43",
"@types/node": "^18.15.11",
"@types/yargs": "^17.0.24"
},
Expand Down
31 changes: 13 additions & 18 deletions packages/api-cli/src/runcli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import { hideBin } from 'yargs/helpers';

import { ApiPromise, SubmittableResult, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
import { assert, isFunction, stringify } from '@polkadot/util';
import { assert, isCodec, stringify } from '@polkadot/util';

import { hexMiddleware, jsonMiddleware, parseParams } from './cli.js';

type ApiOptionsTypes = ApiOptions['types'];
type ApiOptionsRpc = ApiOptions['rpc'];

// the function signature for our catch-any result logger
// eslint-disable-next-line no-use-before-define
type LogFn = (result: SubmittableResult | Codec | ApiCallFn) => void;
Expand Down Expand Up @@ -166,45 +163,43 @@ const params = parseParams(paramsInline, paramsFile);

const ALLOWED = ['consts', 'derive', 'query', 'rpc', 'tx'];

function readFile <T> (src: string): T {
function readFile <T> (src: string): NonNullable<T> {
if (!src) {
return {} as T;
return {} as NonNullable<T>;
}

assert(fs.existsSync(src), `Unable to read .json file at ${src}`);

return JSON.parse(fs.readFileSync(src, 'utf8')) as T;
return JSON.parse(fs.readFileSync(src, 'utf8')) as NonNullable<T>;
}

// parse the arguments and retrieve the details of what we want to do
async function getCallInfo (): Promise<CallInfo> {
assert(endpoint && endpoint.includes('.'), 'You need to specify the command to execute, e.g. query.system.account');

const rpc: ApiOptionsRpc = readFile(argv.rpc);
const types: ApiOptionsTypes = readFile(argv.types);
const rpc = readFile<ApiOptions['rpc']>(argv.rpc);
const types = readFile<ApiOptions['types']>(argv.types);
const provider = new WsProvider(ws);
const api = await ApiPromise.create({ provider, rpc, types });
const apiExt = (api as unknown) as ApiExt;
const [type, section, method] = endpoint.split('.') as [keyof ApiExt, string, string];

assert(ALLOWED.includes(type), `Expected one of ${ALLOWED.join(', ')}, found ${type}`);
assert(apiExt[type][section], `Cannot find ${type}.${section}, your chain does not have the ${section} pallet exposed in the runtime`);
assert(apiExt[type][section][method], `Cannot find ${type}.${section}.${method}, your chain doesn't have the ${method} exposed in the ${section} pallet`);

const fn = apiExt[type][section][method];

assert(fn, `Cannot find ${type}.${section}.${method}, your chain doesn't have the ${method} exposed in the ${section} pallet`);

return {
api,
fn,
log: (result: SubmittableResult | Codec | ApiCallFn): void =>
console.log(
stringify({
// eslint-disable-next-line @typescript-eslint/unbound-method
[method]: isFunction((result as Codec).toHuman)
? (result as Codec).toHuman()
: result
}, 2)
),
console.log(stringify({
[method]: isCodec(result)
? (result as Codec).toHuman()
: result
}, 2)),
method,
section,
type
Expand Down
2 changes: 1 addition & 1 deletion packages/json-serve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@polkadot/api-derive": "^10.3.2",
"@polkadot/types": "^10.3.2",
"@polkadot/util": "^11.1.3",
"koa": "^2.14.1",
"koa": "^2.14.2",
"koa-route": "^3.2.0",
"tslib": "^2.5.0",
"yargs": "^17.7.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/monitor-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@polkadot/api": "^10.3.2",
"@polkadot/types": "^10.3.2",
"@polkadot/util": "^11.1.3",
"koa": "^2.14.1",
"koa": "^2.14.2",
"koa-route": "^3.2.0",
"tslib": "^2.5.0",
"yargs": "^17.7.1"
Expand Down
9 changes: 2 additions & 7 deletions packages/vanitygen/src/runcli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { KeypairType } from '@polkadot/util-crypto/types';
import type { GeneratorOptions } from './types.js';
import type { GeneratorMatch, GeneratorOptions } from './types.js';

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
Expand All @@ -14,12 +14,7 @@ import { cryptoWaitReady } from '@polkadot/util-crypto';
import generator from './generator.js';
import matchRegex from './regex.js';

interface Best {
address: string;
count: number;
mnemonic?: string;
offset: number;
seed?: Uint8Array;
interface Best extends GeneratorMatch {
withCase?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vanitygen/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface GeneratorCalculation {
export interface GeneratorMatch extends GeneratorCalculation {
address: string;
mnemonic?: string;
seed: Uint8Array;
seed?: Uint8Array;
}

export type GeneratorMatches = GeneratorMatch[];
Expand Down
Loading

0 comments on commit d9a9e5d

Please sign in to comment.