Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pallets/dispatchables endpoint #1209

Merged
merged 14 commits into from
Mar 13, 2023
2 changes: 1 addition & 1 deletion docs/dist/app.bundle.js

Large diffs are not rendered by default.

148 changes: 148 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,96 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/{palletId}/dispatchables:
get:
tags:
- pallets
summary: Get a list of dispatchables for a pallet.
description: Returns a list of dispatchable item metadata for distpachable items of the
specified palletId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read dispatchable 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 dispatchable items instead of every dispatchable's metadata.
required: false
schema:
type: boolean
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: array
description: Pallet info and Array of dispatchableItemIds.
items:
$ref: '#/components/schemas/PalletDispatchables'
"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}/dispatchables/{dispatchableItemId}:
get:
tags:
- pallets
summary: Get the value of an dispatchable item.
marshacb marked this conversation as resolved.
Show resolved Hide resolved
description: Returns the value stored under the dispatchableItemId.
parameters:
- name: palletId
in: path
description: 'Name or index of the pallet to read dispatchable metadata for. Note: the pallet name must match what is
specified in the runtime metadata.'
required: true
schema:
type: string
- name: dispatchableItemId
in: path
description: Id of the dispatchable item to query for.
required: true
schema:
type: string
- name: metadata
in: query
description: Include the dispatchable 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/PalletDispatchableItem'
marshacb marked this conversation as resolved.
Show resolved Hide resolved
"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 +2740,64 @@ components:
$ref: '#/components/schemas/AssetInfo'
assetMetadata:
$ref: '#/components/schemas/AssetMetadata'
PalletDispatchables:
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 dispatchables.
example: "14"
items:
type: array
items:
$ref: '#/components/schemas/PalletDispatchablesItemMetadata'
description: Array containing metadata for each dispatchable entry of the pallet.
PalletDispatchablesItem:
type: object
properties:
pallet:
type: string
description: Name of the pallet.
example: "democracy"
palletIndex:
type: string
description: Index of the pallet for looking up dispatchables.
example: "14"
dispatchableItem:
type: string
description: Name of the dispatchable item.
example: "vote"
metadata:
$ref: '#/components/schemas/PalletDispatchablesItemMetadata'
PalletDispatchablesItemMetadata:
type: object
properties:
name:
type: string
example: "propose"
description: The dispatchable item's name (which is the same as the dispatchable item's ID).
fields:
type: array
items:
type: string
index:
type: string
example: "0"
description: The index of the dispatchable item in the lists of pallet dispatchables
marshacb marked this conversation as resolved.
Show resolved Hide resolved
docs:
type: string
example: " Information concerning any given dispatchable.\n\n TWOX-NOTE: SAFE as indexes are not under an attacker’s control."
marshacb marked this conversation as resolved.
Show resolved Hide resolved
args:
type: array
items:
type: string
description: Metadata of an dispatchable item from a FRAME pallet.
marshacb marked this conversation as resolved.
Show resolved Hide resolved
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}/dispatchables': {
path: '/pallets/democracy/dispatchables',
queryParams: [],
},
'/pallets/{palletId}/dispatchables?onlyIds=true': {
path: '/pallets/14/dispatchables',
marshacb marked this conversation as resolved.
Show resolved Hide resolved
queryParams: [],
},
'/pallets/{palletId}/dispatchables/{dispatchableItemId}': {
path: '/pallets/democracy/dispatchables/vote',
queryParams: [],
},
marshacb marked this conversation as resolved.
Show resolved Hide resolved
'/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}/dispatchables': {
path: '/pallets/assets/dispatchables',
queryParams: ['at={blockId}'],
},
'/pallets/{palletId}/dispatchables/{dispatchableItemId}': {
path: '/pallets/assets/dispatchables/create',
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',
'PalletsDispatchables',
'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',
'PalletsDispatchables',
'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',
'PalletsDispatchables',
'PalletsErrors',
'PalletsEvents',
'PalletsNominationPools',
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',
'PalletsDispatchables',
'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',
'PalletsDispatchables',
'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,
PalletsDispatchables,
PalletsErrors,
PalletsEvents,
PalletsNominationPools,
Expand Down Expand Up @@ -60,6 +61,7 @@ export const controllers = {
AccountsStakingPayouts,
ContractsInk,
PalletsAssets,
PalletsDispatchables,
PalletsErrors,
PalletsEvents,
PalletsNominationPools,
Expand Down
97 changes: 97 additions & 0 deletions src/controllers/pallets/PalletsDispatchablesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 { PalletsDispatchablesService } from '../../services';
import { IPalletsDispatchablesParam } from '../../types/requests';
import AbstractController from '../AbstractController';

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

this.initRoutes();
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([
['/:dispatchableItemId', this.getDispatchableById as RequestHandler],
['/', this.getDispatchables],
]);
}

private getDispatchableById: RequestHandler<
IPalletsDispatchablesParam,
unknown,
unknown
> = async (
{ query: { metadata }, params: { palletId, dispatchableItemId } },
res
): Promise<void> => {
const at = undefined;
marshacb marked this conversation as resolved.
Show resolved Hide resolved
const hash = await this.getHashFromAt(at);
const metadataArg = metadata === 'true';
const historicApi = await this.api.at(hash);

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

private getDispatchables: RequestHandler = async (
{ params: { palletId }, query: { onlyIds } },
res
): Promise<void> => {
const at = undefined;
marshacb marked this conversation as resolved.
Show resolved Hide resolved
const hash = await this.getHashFromAt(at);
const onlyIdsArg = onlyIds === 'true';
const historicApi = await this.api.at(hash);

PalletsDispatchablesController.sanitizedSend(
res,
await this.service.fetchDispatchables(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 PalletsDispatchables } from './PalletsDispatchablesController';
export { default as PalletsErrors } from './PalletsErrorsController';
export { default as PalletsEvents } from './PalletsEventsController';
export { default as PalletsNominationPools } from './PalletsNominationPoolsController';
Expand Down
Loading