Skip to content

Commit

Permalink
feat: Impl /pallets/{pallets}/storage; Impl index lookup for `/pall…
Browse files Browse the repository at this point in the history
…ets/{palletId}/storage/{, storageItemId}` (#356)

* feat(endpoint): Add `pallets/{pallets}/storage`

* feat(endpoint): Add onlyIds query param to pallets storage

* feat(endpoint): Support index look up for pallets

* chore: Test `fetchStorage`

* chore: Test `fetchStorageItem`

* Apply suggestions from code review

Co-authored-by: David <[email protected]>

* Lint

Co-authored-by: David <[email protected]>
  • Loading branch information
emostov and dvdplm committed Dec 2, 2020
1 parent d128e54 commit a8387df
Show file tree
Hide file tree
Showing 41 changed files with 1,018 additions and 526 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module.exports = {
// Sort imports
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
// https://github.com/eslint/eslint/issues/2321#issuecomment-134665757
'no-unused-vars': [2, { args: 'all', argsIgnorePattern: '^_' }],
// // https://github.com/eslint/eslint/issues/2321#issuecomment-134665757
// 'no-unused-vars': [2, { args: 'all', argsIgnorePattern: '^_' }],
'@typescript-eslint/no-unused-vars': [
2,
{ args: 'all', argsIgnorePattern: '^_' }
Expand Down
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

45 changes: 0 additions & 45 deletions docs/src/openapi-v1-todo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -392,51 +392,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/storage:
get:
tags:
- pallets
summary: Get a list of storage items for a pallet.
description: Returns a list of storage item metadata for storage items of the
specified palletId.
parameters:
- name: palletId
in: path
description: Name or index of the pallet to query the storage of.
required: true
schema:
type: string
- name: at
in: query
description: Block at which to retrieve a list of
the pallet's storage items.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: array
description: Array of storageItemIds.
items:
$ref: '#/components/schemas/PalletStorageItem'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: could not find pallet with palletId
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
BlockIdentifiers:
Expand Down
73 changes: 72 additions & 1 deletion docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,59 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RuntimeSpec'
/pallets/{palletId}/storage:
get:
tags:
- pallets
summary: Get a list of storage items for a pallet.
description: Returns a list of storage item metadata for storage items of the
specified palletId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to query the storage of. Note: the pallet name must match what is
specified in the runtime metadata.'
required: true
schema:
type: string
- name: onlyIds
in: query
description: Only return the names (IDs) of the storage items instead of all of each storage
item's metadata.
required: false
schema:
type: boolean
- name: at
in: query
description: Block at which to retrieve a list of
the pallet's storage items.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: array
description: Pallet info and Array of storageItemIds.
items:
$ref: '#/components/schemas/PalletStorage'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: could not find pallet with palletId
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/storage/{storageItemId}:
get:
tags:
Expand All @@ -575,7 +628,8 @@ paths:
parameters:
- name: palletId
in: path
description: Name or index of the pallet to query the storage of.
description: 'Name or index of the pallet to query the storage of. Note: pallet name aligns with
pallet name as specified in runtime metadata.'
required: true
schema:
type: string
Expand Down Expand Up @@ -1457,6 +1511,7 @@ components:
name:
type: string
example: "ReferendumInfoOf"
description: The storage item's name (which is the same as the storage item's ID).
modifier:
type: string
example: "Optional"
Expand Down Expand Up @@ -1509,6 +1564,22 @@ components:
turnout: "34485320658000000"
metadata:
$ref: '#/components/schemas/PalletStorageItemMetadata'
PalletStorage:
type: object
properties:
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up storage.
example: "15"
items:
type: array
items:
$ref: '#/components/schemas/PalletStorageItemMetadata'
description: Array containing metadata for each storage entry of the pallet.
requestBodies:
Transaction:
content:
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"test": "jest --silent"
},
"dependencies": {
"@polkadot/api": "^2.7.1",
"@polkadot/util-crypto": "^4.1.1",
"@polkadot/api": "^2.9.1",
"@polkadot/util-crypto": "^4.2.1",
"@substrate/calc": "^0.1.2",
"confmgr": "^1.0.6",
"express": "^4.17.1",
Expand All @@ -47,24 +47,24 @@
},
"devDependencies": {
"@types/express": "^4.17.9",
"@types/express-serve-static-core": "^4.17.13",
"@types/express-serve-static-core": "^4.17.14",
"@types/http-errors": "^1.6.3",
"@types/jest": "^26.0.15",
"@types/jest": "^26.0.16",
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"eslint": "^7.13.0",
"@typescript-eslint/eslint-plugin": "4.9.0",
"@typescript-eslint/parser": "4.9.0",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-simple-import-sort": "^6.0.0",
"eslint-plugin-simple-import-sort": "^6.0.1",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"standard-version": "^9.0.0",
"ts-jest": "^26.4.4",
"tsc-watch": "^4.2.9",
"typescript": "^4.0.5"
"typescript": "^4.1.2"
},
"resolutions": {
"node-forge": ">=0.10.0",
Expand Down
1 change: 1 addition & 0 deletions src/controllers/AbstractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default abstract class AbstractController<T extends AbstractService> {
next
): Promise<void> => {
try {
// eslint-disable-next-line @typescript-eslint/await-thenable
await cb(req, res, next);
} catch (err) {
next(err);
Expand Down
9 changes: 6 additions & 3 deletions src/controllers/AbstractControllers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const api = {
},
};

const MockController = class MockController extends AbstractController<
AbstractService
> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const MockController = class MockController extends AbstractController<AbstractService> {
protected initRoutes(): void {
throw new Error('Method not implemented.');
}
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const MockService = new (class MockService extends AbstractService {})(
(api as unknown) as ApiPromise
);
Expand All @@ -71,6 +71,7 @@ describe('AbstractController', () => {
});

await expect(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
AbstractController['catchWrap'](throws)(req, res, next)
).resolves.toBe(undefined);
expect(next).toBeCalledTimes(1);
Expand All @@ -87,6 +88,7 @@ describe('AbstractController', () => {
};

await expect(
// eslint-disable-next-line @typescript-eslint/await-thenable
AbstractController['catchWrap'](throws)(req, res, next)
).resolves.toBe(undefined);
expect(next).toBeCalledTimes(1);
Expand All @@ -99,6 +101,7 @@ describe('AbstractController', () => {
Promise.resolve().then(() => 'Great success!');

await expect(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
AbstractController['catchWrap'](success)(req, res, next)
).resolves.toBe(undefined);
expect(next).not.toBeCalled();
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/accounts/AccountsBalanceInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ import AbstractController from '../AbstractController';
* - `AccountData`: https://crates.parity.io/pallet_balances/struct.AccountData.html
* - `BalanceLock`: https://crates.parity.io/pallet_balances/struct.BalanceLock.html
*/
export default class AccountsBalanceController extends AbstractController<
AccountsBalanceInfoService
> {
export default class AccountsBalanceController extends AbstractController<AccountsBalanceInfoService> {
constructor(api: ApiPromise) {
super(
api,
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/accounts/AccountsStakingInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ import AbstractController from '../AbstractController';
* - `Bonded`: https://crates.parity.io/pallet_staking/struct.Bonded.html
* - `StakingLedger`: https://crates.parity.io/pallet_staking/struct.StakingLedger.html
*/
export default class AccountsStakingInfoController extends AbstractController<
AccountsStakingInfoService
> {
export default class AccountsStakingInfoController extends AbstractController<AccountsStakingInfoService> {
constructor(api: ApiPromise) {
super(
api,
Expand Down
8 changes: 2 additions & 6 deletions src/controllers/accounts/AccountsStakingPayoutsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ import AbstractController from '../AbstractController';
* aformentioned payouts.
*
*/
export default class AccountsStakingPayoutsController extends AbstractController<
AccountsStakingPayoutsService
> {
export default class AccountsStakingPayoutsController extends AbstractController<AccountsStakingPayoutsService> {
constructor(api: ApiPromise) {
super(
api,
Expand All @@ -86,9 +84,7 @@ export default class AccountsStakingPayoutsController extends AbstractController
* @param req Express Request
* @param res Express Response
*/
private getStakingPayoutsByAccountId: RequestHandler<
IAddressParam
> = async (
private getStakingPayoutsByAccountId: RequestHandler<IAddressParam> = async (
{ params: { address }, query: { depth, era, unclaimedOnly } },
res
): Promise<void> => {
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/accounts/AccountsVestingInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import AbstractController from '../AbstractController';
* - Vesting Pallet: https://crates.parity.io/pallet_vesting/index.html
* - `VestingInfo`: https://crates.parity.io/pallet_vesting/struct.VestingInfo.html
*/
export default class AccountsVestingInfoController extends AbstractController<
AccountsVestingInfoService
> {
export default class AccountsVestingInfoController extends AbstractController<AccountsVestingInfoService> {
constructor(api: ApiPromise) {
super(
api,
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/blocks/BlocksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ import AbstractController from '../AbstractController';
* - `OnInitialize`: https://crates.parity.io/frame_support/traits/trait.OnInitialize.html
* - `OnFinalize`: https://crates.parity.io/frame_support/traits/trait.OnFinalize.html
*/
export default class BlocksController extends AbstractController<
BlocksService
> {
export default class BlocksController extends AbstractController<BlocksService> {
constructor(api: ApiPromise) {
super(api, '/blocks', new BlocksService(api));
this.initRoutes();
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/node/NodeNetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import AbstractController from '../AbstractController';
* References:
* - `NodeRole`: https://github.com/paritytech/substrate/blob/master/client/rpc-api/src/system/helpers.rs#L80
*/
export default class NodeNetworkController extends AbstractController<
NodeNetworkService
> {
export default class NodeNetworkController extends AbstractController<NodeNetworkService> {
constructor(api: ApiPromise) {
super(api, '/node/network', new NodeNetworkService(api));
this.initRoutes();
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/node/NodeTransactionPoolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import AbstractController from '../AbstractController';
* - `hash`: H256 hash of the extrinsic.
* - `encodedExtrinsic`: Scale encoded extrinsic.
*/
export default class NodeTransactionPoolController extends AbstractController<
NodeTransactionPoolService
> {
export default class NodeTransactionPoolController extends AbstractController<NodeTransactionPoolService> {
constructor(api: ApiPromise) {
super(
api,
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/node/NodeVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import AbstractController from '../AbstractController';
* - `clientImplName`: Node's implementation name.
* - `chain`: Node's chain name.
*/
export default class NodeVersionController extends AbstractController<
NodeVersionService
> {
export default class NodeVersionController extends AbstractController<NodeVersionService> {
constructor(api: ApiPromise) {
super(api, '/node/version', new NodeVersionService(api));
this.initRoutes();
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/pallets/PalletsStakingProgressController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ import AbstractController from '../AbstractController';
* - `Forcing`: https://crates.parity.io/pallet_staking/enum.Forcing.html
* - `ElectionStatus`: https://crates.parity.io/pallet_staking/enum.ElectionStatus.html
*/
export default class PalletsStakingProgressController extends AbstractController<
PalletsStakingProgressService
> {
export default class PalletsStakingProgressController extends AbstractController<PalletsStakingProgressService> {
constructor(api: ApiPromise) {
super(
api,
Expand Down
Loading

0 comments on commit a8387df

Please sign in to comment.