Skip to content

Commit

Permalink
feat: add metadata versions endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 committed Apr 2, 2024
1 parent c73c801 commit 53a2a0d
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 69 deletions.
114 changes: 57 additions & 57 deletions docs/dist/app.bundle.js

Large diffs are not rendered by default.

163 changes: 163 additions & 0 deletions docs/src/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,86 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/transaction/material/v14:
get:
tags:
- transaction
summary: Get all the network information needed to construct a transaction offline and
the v14 of metadata.
description: Returns the material that is universal to constructing any
signed transaction offline and the v14 of metadata. Replaces `/tx/artifacts`
from versions < v1.0.0.
operationId: getTransactionMaterial
parameters:
- name: at
in: query
description: Block at which to retrieve the transaction construction
material.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
- name: metadata
in: query
description: Specifies the format of the metadata to be returned. Accepted values are
'json', and 'scale'. 'json' being the decoded metadata, and 'scale' being the SCALE encoded metadata.
When `metadata` is not inputted, the `metadata` field will be absent.
schema:
type: string
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/TransactionMaterial'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/transaction/material/v15:
get:
tags:
- transaction
summary: Get all the network information needed to construct a transaction offline and
the v15 of metadata.
description: Returns the material that is universal to constructing any
signed transaction offline and the v15 of metadata. Replaces `/tx/artifacts`
from versions < v1.0.0.
operationId: getTransactionMaterial
parameters:
- name: at
in: query
description: Block at which to retrieve the transaction construction
material.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
- name: metadata
in: query
description: Specifies the format of the metadata to be returned. Accepted values are
'json', and 'scale'. 'json' being the decoded metadata, and 'scale' being the SCALE encoded metadata.
When `metadata` is not inputted, the `metadata` field will be absent.
schema:
type: string
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/TransactionMaterial'
"400":
description: invalid blockId supplied for at query param
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pallets/assets/{assetId}/asset-info:
get:
tags:
Expand Down Expand Up @@ -1753,6 +1833,89 @@ paths:
schema:
type: object
description: Response is dependent on the runtime metadata contents.
/runtime/metadata/v14:
get:
tags:
- runtime
summary: Get the version 14 of runtime metadata in decoded, JSON form.
description: >-
Returns the version 14 of 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
description: Block at which to retrieve the metadata at.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: object
description: Response is dependent on the runtime metadata contents.
/runtime/metadata/v15:
get:
tags:
- runtime
summary: Get the version 15 of runtime metadata in decoded, JSON form.
description: >-
Returns the version 15 of 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
description: Block at which to retrieve the metadata at.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: object
description: Response is dependent on the runtime metadata contents.
/runtime/metadata/versions:
get:
tags:
- runtime
summary: Get the available versions of runtime metadata.
description: >-
Returns the available versions of runtime metadata.
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
description: Block at which to retrieve the metadata versions at.
required: false
schema:
type: string
description: Block identifier, as the block height or block hash.
format: unsignedInteger or $hex
responses:
"200":
description: successful operation
content:
application/json:
schema:
type: array
items:
type: string
description: An array with the available metadata versions.
/runtime/code:
get:
tags:
Expand Down
72 changes: 70 additions & 2 deletions src/controllers/runtime/RuntimeMetadataController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 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
Expand All @@ -23,6 +23,10 @@ import AbstractController from '../AbstractController';
/**
* GET the chain's metadata.
*
* Path params:
* - (Optional) `metadataVersion`: The specific version of the Metadata to query.
* The input must conform to the `vX` format, where `X` represents the version number (examples: 'v14', 'v15').
*
* Query:
* - (Optional) `at`: Block hash or height at which to query. If not provided, queries
* finalized head.
Expand All @@ -41,7 +45,11 @@ export default class RuntimeMetadataController extends AbstractController<Runtim
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([['', this.getMetadata]]);
this.safeMountAsyncGetHandlers([
['/', this.getMetadata],
['/versions', this.getMetadataAvailableVersions],
['/:metadataVersion', this.getMetadataVersioned],
]);
}

/**
Expand All @@ -65,4 +73,64 @@ export default class RuntimeMetadataController extends AbstractController<Runtim
metadataOpts: { registry, version: metadata.version },
});
};

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

let api;
if (at) {
api = await this.api.at(hash);
} else {
api = this.api;
}

// Validation of the `metadataVersion` path parameter.
const versionM = metadataVersion.slice(1);
const version = this.parseNumberOrThrow(
versionM,
`Version ${metadataVersion.slice(1).toString()} of metadata provided is not a number.`,
);

const regExPattern = new RegExp('^[vV][0-9]+$');
if (regExPattern.test(metadataVersion) === false) {
throw new Error(
`${metadataVersion} input is not of the expected 'vX' format, where 'X' represents the version number (examples: 'v14', 'v15').`,
);
}

const availableVersions = (await api.call.metadata.metadataVersions()).toHuman() as string[];
if (version && availableVersions?.includes(version.toString()) === false) {
throw new Error(`Version ${version} of Metadata is not available.`);
}

const registry = api ? api.registry : this.api.registry;
const metadata = await this.service.fetchMetadataVersioned(hash, version);

RuntimeMetadataController.sanitizedSend(res, metadata, {
metadataOpts: { registry, version },
});
};

/**
* Get the available versions of chain's metadata.
*
* @param _req Express Request
* @param res Express Response
*/
private getMetadataAvailableVersions: RequestHandler = async ({ query: { at } }, res): Promise<void> => {
const hash = await this.getHashFromAt(at);

const metadataVersions = await this.service.fetchMetadataVersions(hash);

RuntimeMetadataController.sanitizedSend(res, metadataVersions, {});
};
}
58 changes: 56 additions & 2 deletions src/controllers/transaction/TransactionMaterialController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 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
Expand All @@ -25,6 +25,10 @@ export type MetadataOpts = 'json' | 'scale';
/**
* GET all the network information needed to construct a transaction offline.
*
* Path params:
* - (Optional) `metadataVersion`: The specific version of the Metadata to query.
* The input must conform to the `vX` format, where `X` represents the version number (examples: 'v14', 'v15').
*
* Query
* - (Optional) `metadata`: It accepts `json`, or `scale` values. If it is not present,
* the metadata field will not be included.
Expand Down Expand Up @@ -59,7 +63,10 @@ export default class TransactionMaterialController extends AbstractController<Tr
}

protected initRoutes(): void {
this.safeMountAsyncGetHandlers([['', this.getTransactionMaterial]]);
this.safeMountAsyncGetHandlers([
['/', this.getTransactionMaterial],
['/:metadataVersion', this.getTransactionMaterialwithVersionedMetadata],
]);
}

/**
Expand Down Expand Up @@ -98,4 +105,51 @@ export default class TransactionMaterialController extends AbstractController<Tr

return false;
}

/**
* Get the chain's metadata at the reuqested version in JSON or scale format
* depending on the `metadata` query param.
*
* @param _req Express Request
* @param res Express Response
*/
private getTransactionMaterialwithVersionedMetadata: RequestHandler = async (
{ params: { metadataVersion }, query: { at, metadata } },
res,
): Promise<void> => {
const hash = await this.getHashFromAt(at);

let api;
if (at) {
api = await this.api.at(hash);
} else {
api = this.api;
}

// Validation of the `metadataVersion` path parameter.
const metadataV = metadataVersion.slice(1);
const version = this.parseNumberOrThrow(
metadataV,
`Version ${metadataV.toString()} of metadata provided is not a number.`,
);

const regExPattern = new RegExp('^[vV][0-9]+$');
if (regExPattern.test(metadataVersion) === false) {
throw new Error(
`${metadataVersion} input is not of the expected 'vX' format, where 'X' represents the version number (examples: 'v14', 'v15').`,
);
}

const availableVersions = (await api.call.metadata.metadataVersions()).toHuman() as string[];
if (version && availableVersions?.includes(version.toString()) === false) {
throw new Error(`Version ${version} of Metadata is not available.`);
}

const metadataArg = this.parseMetadataArgs(metadata);

TransactionMaterialController.sanitizedSend(
res,
await this.service.fetchTransactionMaterialwithVersionedMetadata(hash, metadataArg, version),
);
};
}
Loading

0 comments on commit 53a2a0d

Please sign in to comment.