Skip to content

Commit b736cb8

Browse files
authored
Add noMeta query param to tx/artifacts (#222)
* Add noMeta query param to `tx/artifacts` * Fix comment * Change to boolean arg for service
1 parent 41dac62 commit b736cb8

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

src/controllers/transaction/TransactionMaterialController.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import AbstractController from '../AbstractController';
1313
* - (Optional) `number`: Block hash or number at which to query. If not provided, queries
1414
* finalized head.
1515
*
16+
* Query
17+
* - (Optional) `noMeta`: If true, does not return metadata hex. This is useful when metadata is not
18+
* needed and response time is a concern. Defaults to false.
19+
*
1620
* Returns:
1721
* - `at`: Block number and hash at which the call was made.
1822
* - `genesisHash`: The hash of the chain's genesis block.
@@ -56,14 +60,16 @@ export default class TransactionMaterialController extends AbstractController<
5660
* @param res Express Response
5761
*/
5862
private getTxArtifacts: RequestHandler = async (
59-
_req,
63+
{ query: { noMeta } },
6064
res
6165
): Promise<void> => {
6266
const hash = await this.api.rpc.chain.getFinalizedHead();
6367

68+
const noMetaArg = noMeta === 'true' ? true : false;
69+
6470
TransactionMaterialController.sanitizedSend(
6571
res,
66-
await this.service.fetchTransactionMaterial(hash)
72+
await this.service.fetchTransactionMaterial(hash, noMetaArg)
6773
);
6874
};
6975

@@ -75,14 +81,16 @@ export default class TransactionMaterialController extends AbstractController<
7581
* @param res Express Response
7682
*/
7783
private getTxArtifactsAtBlock: RequestHandler<INumberParam> = async (
78-
{ params: { number } },
84+
{ params: { number }, query: { noMeta } },
7985
res
8086
): Promise<void> => {
8187
const hash = await this.getHashForBlock(number);
8288

89+
const noMetaArg = noMeta === 'true' ? true : false;
90+
8391
TransactionMaterialController.sanitizedSend(
8492
res,
85-
await this.service.fetchTransactionMaterial(hash)
93+
await this.service.fetchTransactionMaterial(hash, noMetaArg)
8694
);
8795
};
8896
}

src/services/transaction/TransactionMaterialService.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('TransactionMaterialService', () => {
1111
expect(
1212
sanitizeNumbers(
1313
await transactionMaterialService.fetchTransactionMaterial(
14-
blockHash789629
14+
blockHash789629,
15+
false
1516
)
1617
)
1718
).toStrictEqual(response789629);

src/services/transaction/TransactionMaterialService.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,34 @@ export class TransactionMaterialService extends AbstractService {
1111
* @param hash `BlockHash` to make call at
1212
*/
1313
async fetchTransactionMaterial(
14-
hash: BlockHash
14+
hash: BlockHash,
15+
noMeta: boolean
1516
): Promise<ITransactionMaterial> {
1617
const api = await this.ensureMeta(hash);
1718

19+
if (noMeta) {
20+
const [header, genesisHash, name, version] = await Promise.all([
21+
api.rpc.chain.getHeader(hash),
22+
api.rpc.chain.getBlockHash(0),
23+
api.rpc.system.chain(),
24+
api.rpc.state.getRuntimeVersion(hash),
25+
]);
26+
27+
const at = {
28+
hash,
29+
height: header.number.toNumber().toString(10),
30+
};
31+
32+
return {
33+
at,
34+
genesisHash,
35+
chainName: name.toString(),
36+
specName: version.specName.toString(),
37+
specVersion: version.specVersion,
38+
txVersion: version.transactionVersion,
39+
};
40+
}
41+
1842
const [
1943
header,
2044
metadata,

src/types/responses/TransactionMaterial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export interface ITransactionMaterial {
1010
specName: string;
1111
specVersion: U32;
1212
txVersion: U32;
13-
metadata: string;
13+
metadata?: string;
1414
}

0 commit comments

Comments
 (0)