-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add route /blocks/{blockId}/extrinsics/{extrinsicIndex} (#400)
* Add new /blocks/{blockId}/extrinsics/{extrinsicsIndex} endpoint * Add endpoint to chains-config endpoints * Add exports to entry files * Add request validations * Abstract logic from BlocksExtrinsicsController into BlocksServices * Refactor index, and add await t async call * Add types * fix: revert back to original method and cleanup index param fix: destructure fix: reorder index * fix: add options for PoW chains, adjust options passed into fetchBlock * feat: block 789629 extrinsic responce json file * fix: modify types for at[object] * fix: revert types for IExtrinsicIndex at:[object] * fix: mock json extrinsic data * fix: working fetchExtrinsicByIndex test * feat: Test extrinisics error * fix: change error type * fix: async/await functionality across fetch extrinsics, fix tests, lint * feat: docs * fix: docs * fix: (docs) events, extrinsics * fix: docs responses * fix: docs ExtrinsicIndex * fix: bugs in docs * fix: change thrown error to BadRequest for 400 error * fix: lint * fix: docs description for ExtrinsicIndex, organize BadRequest import * Update docs/src/openapi-v1.yaml Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> * Update docs/src/openapi-v1.yaml Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> * Update docs/src/openapi-v1.yaml Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> * fix: extrinsicsIndex -> extriniscIndex (singular) fix: typos, naming, add parseNumberOrThrow fix: revert to parseInt * fix: typos, IAt type, docs, error messages * fix: lint * fix: fix error messaging, and docs fix: cleanup block extrinsics controller fix: omitFinalized -> true fix: add test to check parseNumberOrThrow will throw an error if a negative is passed in. Yarn fix * fix: remove async * fix: remove async fix: update extrinsic index test to query extrinsic 2 fix: lint * Update docs/src/openapi-v1.yaml Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> * fix: getExtrinsicByExtrinsicIndex => getExtrinsicByIndex * fix: getExtrinsicByIndex => getExtrinsicByTimepoint Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
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,66 @@ | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { RequestHandler } from 'express'; | ||
|
||
import { BlocksService } from '../../services'; | ||
import { INumberParam } from '../../types/requests'; | ||
import AbstractController from '../AbstractController'; | ||
|
||
export default class BlocksExtrinsicsController extends AbstractController<BlocksService> { | ||
constructor(api: ApiPromise) { | ||
super(api, '/blocks/:blockId/extrinsics', new BlocksService(api)); | ||
this.initRoutes(); | ||
} | ||
|
||
protected initRoutes(): void { | ||
this.safeMountAsyncGetHandlers([ | ||
['/:extrinsicIndex', this.getExtrinsicByTimepoint], | ||
]); | ||
} | ||
|
||
/** | ||
* | ||
* @param _req Express Request | ||
* @param res Express Response | ||
*/ | ||
private getExtrinsicByTimepoint: RequestHandler<INumberParam> = async ( | ||
{ | ||
params: { blockId, extrinsicIndex }, | ||
query: { eventDocs, extrinsicDocs }, | ||
}, | ||
res | ||
): Promise<void> => { | ||
const hash = await this.getHashForBlock(blockId); | ||
|
||
const eventDocsArg = eventDocs === 'true'; | ||
const extrinsicDocsArg = extrinsicDocs === 'true'; | ||
|
||
const options = { | ||
eventDocs: eventDocsArg, | ||
extrinsicDocs: extrinsicDocsArg, | ||
checkFinalized: true, | ||
queryFinalizedHead: false, | ||
omitFinalizedTag: true, | ||
}; | ||
|
||
const block = await this.service.fetchBlock(hash, options); | ||
|
||
/** | ||
* Verify our param `extrinsicIndex` is an integer represented as a string | ||
*/ | ||
this.parseNumberOrThrow( | ||
extrinsicIndex, | ||
'`exstrinsicIndex` path param is not a number' | ||
); | ||
|
||
/** | ||
* Change extrinsicIndex from a type string to a number before passing it | ||
* into any service. | ||
*/ | ||
const index = parseInt(extrinsicIndex, 10); | ||
|
||
BlocksExtrinsicsController.sanitizedSend( | ||
res, | ||
this.service.fetchExtrinsicByIndex(block, index) | ||
); | ||
}; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as Blocks } from './BlocksController'; | ||
export { default as BlocksExtrinsics } from './BlocksExtrinsicsController'; |
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,11 @@ | ||
import { BadRequest } from 'http-errors'; | ||
|
||
export function parseNumberOrThrow(n: string, errorMessage: string): number { | ||
const num = Number(n); | ||
|
||
if (!Number.isInteger(num) || num < 0) { | ||
throw new BadRequest(errorMessage); | ||
} | ||
|
||
return num; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/services/test-helpers/responses/blocks/block789629Extrinsic.json
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,37 @@ | ||
{ | ||
"at": { | ||
"height": "789629", | ||
"hash": "0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578" | ||
}, | ||
"extrinsics": { | ||
"method": { | ||
"pallet": "parachains", | ||
"method": "setHeads" | ||
}, | ||
"signature": null, | ||
"nonce": null, | ||
"args": { | ||
"heads": [] | ||
}, | ||
"tip": null, | ||
"hash": "0xcf52705d1ade64fc0b05859ac28358c0770a217dd76b75e586ae848c56ae810d", | ||
"info": {}, | ||
"events": [ | ||
{ | ||
"method": { | ||
"pallet": "system", | ||
"method": "ExtrinsicSuccess" | ||
}, | ||
"data": [ | ||
{ | ||
"weight": "1000000000", | ||
"class": "Mandatory", | ||
"paysFee": "Yes" | ||
} | ||
] | ||
} | ||
], | ||
"success": true, | ||
"paysFee": false | ||
} | ||
} |
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