-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pallets/consts endpoint (#1210)
* 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
Showing
28 changed files
with
782 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.