Skip to content

Commit

Permalink
feat: add pallets/consts endpoint (#1210)
Browse files Browse the repository at this point in the history
* add endpoint for pallet constants
add unit and e2e tests

* update docs for consts endpoint

* merged master
added statemint consts e2e tests

* update abstractpalletservice

* add newline in response mocks

* yarn lint

* update docs
  • Loading branch information
marshacb committed Mar 1, 2023
1 parent 190c713 commit 97c1ca6
Show file tree
Hide file tree
Showing 28 changed files with 782 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,113 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/consts:
get:
tags:
- pallets
summary: Get a list of constants for a pallet.
description: Returns a list of const item metadata for constant items of the
specified palletId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read constant metadata for. 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 const items instead of every constant's metadata.
required: false
schema:
type: boolean
- name: at
in: query
description: Block at which to retrieve a list of
the pallet's constant 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 constantItemIds.
items:
$ref: '#/components/schemas/PalletConstants'
"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}/consts/{constantItemId}:
get:
tags:
- pallets
summary: Get the value of a constant item.
description: Returns the value stored under the constantItemId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read constant metadata for. Note: the pallet name must match what is
specified in the runtime metadata.'
required: true
schema:
type: string
- name: constantItemId
in: path
description: Id of the const item to query for.
required: true
schema:
type: string
- name: at
in: query
description: Block at which to query the const item at.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
- name: metadata
in: query
description: Include the const items metadata (including documentation)
if set to true.
required: false
schema:
default: false
type: boolean
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/PalletConstantsItem'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
"404":
description: could not find resource with with id
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/errors:
get:
tags:
Expand Down Expand Up @@ -2650,6 +2757,59 @@ components:
$ref: '#/components/schemas/AssetInfo'
assetMetadata:
$ref: '#/components/schemas/AssetMetadata'
PalletConstants:
type: object
properties:
at:
$ref: '#/components/schemas/BlockIdentifiers'
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up constants.
example: "14"
items:
type: array
items:
$ref: '#/components/schemas/PalletConstantsItemMetadata'
description: Array containing metadata for each constant entry of the pallet.
PalletConstantsItem:
type: object
properties:
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up constants.
example: "14"
errorItem:
type: string
description: Name of the constant item.
example: "EnactmentPeriod"
metadata:
$ref: '#/components/schemas/PalletConstantsItemMetadata'
PalletConstantsItemMetadata:
type: object
properties:
name:
type: string
example: "VotingPeriod"
description: The constant item's name (which is the same as the constant item's ID).
type:
type: string
example: "4"
value:
type: string
example: "0x00270600"
description: The hex value of the constant
docs:
type: string
example: "Information concerning any given constant.\n\n TWOX-NOTE: SAFE as indexes are not under an attacker’s control."
description: Metadata of an constant item from a FRAME pallet.
PalletErrors:
type: object
properties:
Expand Down
12 changes: 12 additions & 0 deletions e2e-tests/latest/endpoints/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ export const polkadot: IConfig = {
path: '/paras/head/backed-candidates',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/consts': {
path: '/pallets/democracy/consts',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/consts?onlyIds=true': {
path: '/pallets/14/consts',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/consts/{constItemId}': {
path: '/pallets/democracy/consts/EnactmentPeriod',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/errors': {
path: '/pallets/balances/errors',
queryParams: ['at={blockId}'],
Expand Down
8 changes: 8 additions & 0 deletions e2e-tests/latest/endpoints/statemint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export const statemint = {
path: '/pallets/assets/123/asset-info',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/consts': {
path: '/pallets/assets/consts',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/consts/{constantItemId}': {
path: '/pallets/assets/consts/AssetDeposit',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/errors': {
path: '/pallets/assets/errors',
queryParams: ['at={blockId}'],
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/defaultControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const defaultControllers: ControllerConfig = {
'NodeTransactionPool',
'NodeVersion',
'PalletsAssets',
'PalletsConsts',
'PalletsErrors',
'PalletsEvents',
'PalletsStakingProgress',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/kusamaControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const kusamaControllers: ControllerConfig = {
'NodeNetwork',
'NodeTransactionPool',
'NodeVersion',
'PalletsConsts',
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/polkadotControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const polkadotControllers: ControllerConfig = {
'NodeNetwork',
'NodeTransactionPool',
'NodeVersion',
'PalletsConsts',
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/statemineControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const statemineControllers: ControllerConfig = {
'NodeTransactionPool',
'NodeVersion',
'PalletsAssets',
'PalletsConsts',
'PalletsErrors',
'PalletsEvents',
'RuntimeCode',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/statemintControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const statemintControllers: ControllerConfig = {
'NodeTransactionPool',
'NodeVersion',
'PalletsAssets',
'PalletsConsts',
'PalletsEvents',
'PalletsErrors',
'RuntimeCode',
Expand Down
1 change: 1 addition & 0 deletions src/chains-config/westendControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const westendControllers: ControllerConfig = {
'NodeNetwork',
'NodeTransactionPool',
'NodeVersion',
'PalletsConsts',
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ContractsInk } from './contracts';
import { NodeNetwork, NodeTransactionPool, NodeVersion } from './node';
import {
PalletsAssets,
PalletsConsts,
PalletsErrors,
PalletsEvents,
PalletsNominationPools,
Expand Down Expand Up @@ -60,6 +61,7 @@ export const controllers = {
AccountsStakingPayouts,
ContractsInk,
PalletsAssets,
PalletsConsts,
PalletsErrors,
PalletsEvents,
PalletsNominationPools,
Expand Down
91 changes: 91 additions & 0 deletions src/controllers/pallets/PalletsConstsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { ApiPromise } from '@polkadot/api';
import { stringCamelCase } from '@polkadot/util';
import { RequestHandler } from 'express-serve-static-core';

import { PalletsConstantsService } from '../../services';
import { IPalletsConstantsParam } from '../../types/requests';
import AbstractController from '../AbstractController';

/**
* `/pallets/{palletId}/consts`
*
* Returns the metadata for each constant item of the pallet.
*
* `/pallets/{palletId}/consts/{constantItemId}`
*
* Returns the info for the constantItemId.
*
* See `docs/src/openapi-v1.yaml` for usage information.
*/
export default class PalletsConstantsController extends AbstractController<PalletsConstantsService> {
constructor(api: ApiPromise) {
super(api, '/pallets/:palletId/consts', new PalletsConstantsService(api));

this.initRoutes();
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([
['/:constantItemId', this.getConstById as RequestHandler],
['/', this.getConsts],
]);
}

private getConstById: RequestHandler<
IPalletsConstantsParam,
unknown,
unknown
> = async (
{ query: { at, metadata }, params: { palletId, constantItemId } },
res
): Promise<void> => {
const metadataArg = metadata === 'true';
const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsConstantsController.sanitizedSend(
res,
await this.service.fetchConstantItem(historicApi, {
hash,
// stringCamelCase ensures we don't have snake case or kebab case
palletId: stringCamelCase(palletId),
constantItemId: stringCamelCase(constantItemId),
metadata: metadataArg,
})
);
};

private getConsts: RequestHandler = async (
{ params: { palletId }, query: { at, onlyIds } },
res
): Promise<void> => {
const onlyIdsArg = onlyIds === 'true';
const hash = await this.getHashFromAt(at);
const historicApi = await this.api.at(hash);

PalletsConstantsController.sanitizedSend(
res,
await this.service.fetchConstants(historicApi, {
hash,
palletId: stringCamelCase(palletId),
onlyIds: onlyIdsArg,
})
);
};
}
1 change: 1 addition & 0 deletions src/controllers/pallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export { default as PalletsAssets } from './PalletsAssetsController';
export { default as PalletsConsts } from './PalletsConstsController';
export { default as PalletsErrors } from './PalletsErrorsController';
export { default as PalletsEvents } from './PalletsEventsController';
export { default as PalletsNominationPools } from './PalletsNominationPoolsController';
Expand Down
Loading

0 comments on commit 97c1ca6

Please sign in to comment.