Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ a 32-byte hex string (`0x` followed by 64 hexadecimal digits) that denotes the b

- [`/block` fetch latest finalized block details.](/src/controllers/blocks/BlocksController.ts)

- [`/block` fetch latest finalized block details.](/src/controllers/blocks/BlocksController.ts)

- [`/block/NUMBER` fetch block details at the block identified by 'NUMBER`.](/src/controllers/blocks/BlocksController.ts)

- [`/balance/ADDRESS` fetch balances for `ADDRESS` at latest finalized block.](src/controllers/accounts/AccountsBalanceInfoController.ts)
Expand All @@ -104,16 +102,14 @@ a 32-byte hex string (`0x` followed by 64 hexadecimal digits) that denotes the b

- [`/vesting/ADDRESS/NUMBER` fetch the vesting info for `ADDRESS` at the block identified by 'NUMBER`.](src/controllers/accounts/AccountsVestingInfoController.ts)

- [`/metadata` fetch chain metadata at latest finalized block.](src/controllers/runtime/RuntimeMetadataController.ts)

- [`/metadata/NUMBER` fetch chain metadata at the block identified by 'NUMBER`.](src/controllers/runtime/RuntimeMetadataController.ts)

- [`/node/network` fetch information about the Substrate node's activity in the peer-to-peer network.](src/controllers/node/NodeNetworkController.ts)

- [`/node/transaction-pool` fetch pending extrinsics from the Substrate node.](src/controllers/node/NodeTransactionPoolController.ts)

- [`/node/version` fetch information about the Substrates node's implementation and versioning.](src/controllers/node/NodeVersionController.ts)

- [`/runtime/metadata` fetch the runtime metadata in decoded, JSON form.](src/controllers/runtime/RuntimeMetadataController.ts) (replaces `/metadata`)

- [`/runtime/code` fetch the Wasm code blob of the Substrate runtime.](src/controllers/runtime/RuntimeCodeController.ts)

- [`/runtime/spec` version information of the Substrate runtime.](src/controllers/runtime/RuntimeSpecController.ts)
Expand Down
6 changes: 5 additions & 1 deletion openapi/openapi-proposal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,11 @@ paths:
tags:
- runtime
summary: Get the runtime metadata in decoded, JSON form.
description: Returns the runtime metadata as a JSON object.
description: >-
Returns the runtime metadata as a JSON object.
Substrate Reference:
- FRAME Support: https://crates.parity.io/frame_support/metadata/index.html
- Knowledge Base: https://substrate.dev/docs/en/knowledgebase/runtime/metadata
parameters:
- name: at
in: query
Expand Down
50 changes: 50 additions & 0 deletions src/controllers/runtime/RuntimeMetadataController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ApiPromise } from '@polkadot/api';
import { RequestHandler } from 'express';

import { RuntimeMetadataService } from '../../services';
import AbstractController from '../AbstractController';

/**
* GET the chain's metadata.
*
* Query:
* - (Optional) `at`: Block hash or height at which to query. If not provided, queries
* finalized head.
*
* Returns:
* - Metadata object.
*
* Substrate Reference:
* - FRAME Support: https://crates.parity.io/frame_support/metadata/index.html
* - Knowledge Base: https://substrate.dev/docs/en/knowledgebase/runtime/metadata
*/
export default class RuntimeMetadataController extends AbstractController<
RuntimeMetadataService
> {
constructor(api: ApiPromise) {
super(api, '/runtime/metadata', new RuntimeMetadataService(api));
this.initRoutes();
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([['', this.getMetadata]]);
}

/**
* Get the chain's latest metadata in a decoded, JSON format.
*
* @param _req Express Request
* @param res Express Response
*/
private getMetadata: RequestHandler = async (
{ query: { at } },
res
): Promise<void> => {
const hash = await this.getHashFromAt(at);

RuntimeMetadataController.sanitizedSend(
res,
await this.service.fetchMetadata(hash)
);
};
}
1 change: 1 addition & 0 deletions src/controllers/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as RuntimeCode } from './RuntimeCodeController';
export { default as RuntimeSpec } from './RuntimeSpecController';
export { default as RuntimeMetadata } from './RuntimeMetadataController';
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async function main() {
);
const runtimeCodeController = new controllers.RuntimeCode(api);
const runtimeSpecController = new controllers.RuntimeSpec(api);
const runtimeMetadataController = new controllers.RuntimeMetadata(api);
const transactionDryRunController = new controllers.TransactionDryRun(api);

// Create our App
Expand All @@ -109,6 +110,7 @@ async function main() {
nodeTransactionPoolController,
runtimeCodeController,
runtimeSpecController,
runtimeMetadataController,
transactionDryRunController,
...v0Controllers,
],
Expand Down
18 changes: 18 additions & 0 deletions src/services/runtime/RuntimeMetadataService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { blockHash789629, mockApi } from '../test-helpers/mock';
import * as response789629 from '../test-helpers/responses/runtime/metadata789629.json';
import { RuntimeMetadataService } from './RuntimeMetadataService';

const runtimeMetadataService = new RuntimeMetadataService(mockApi);

describe('RuntimeMetadataService', () => {
describe('fetchMetadata', () => {
it('works when ApiPromise works (block 789629)', async () => {
expect(
sanitizeNumbers(
await runtimeMetadataService.fetchMetadata(blockHash789629)
)
).toStrictEqual(response789629);
});
});
});
19 changes: 19 additions & 0 deletions src/services/runtime/RuntimeMetadataService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Metadata } from '@polkadot/types';
import { BlockHash } from '@polkadot/types/interfaces';

import { AbstractService } from '../AbstractService';

export class RuntimeMetadataService extends AbstractService {
/**
* Fetch `Metadata` in decoded JSON form.
*
* @param hash `BlockHash` to make call at
*/
async fetchMetadata(hash: BlockHash): Promise<Metadata> {
const api = await this.ensureMeta(hash);

const metadata = await api.rpc.state.getMetadata(hash);

return metadata;
}
}
1 change: 1 addition & 0 deletions src/services/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './RuntimeCodeService';
export * from './RuntimeSpecService';
export * from './RuntimeMetadataService';