Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metadata script for types-support #6021

Merged
merged 1 commit into from
Nov 7, 2024
Merged
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
105 changes: 64 additions & 41 deletions scripts/metadata-get.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,70 @@ const CMD = {
const META_VERSION_HEX = '0x0f000000';
const META_VERSION = 15;

let requestId = 0;

/**
* Small CLI parser.
*
* This script accepts `--url` to specify the endpoint to query the metadata from.
*
* @param {'rpc_methods' | 'state_getMetadata' | 'state_getRuntimeVersion' | 'state_call'} method
* @param {string[]} params
* @returns {Promise<any>}
* @returns {string}
*/
async function get (method, params = []) {
const res = await fetch('http://127.0.0.1:9944', {
body: JSON.stringify({
id: ++requestId,
jsonrpc: '2.0',
method,
params
}),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
method: 'POST'
});
const body = await res.json();

return body.result;
}

/** @type {[string[], string, { specName: 'polkadot' | 'kusama' | 'node'; specVersion: string; }]} */
const [methods, metadata, version] = await Promise.all([
get('rpc_methods'),
get('state_call', ['Metadata_metadata_at_version', META_VERSION_HEX]),
get('state_getRuntimeVersion')
]);
const chain = version.specName === 'node'
? 'substrate'
: version.specName;
const metaVer = parseInt(metadata.substring(10, 12), 16);
const path = `packages/types-support/src/metadata/v${META_VERSION}/${chain}`;

fs.writeFileSync(`${path}-hex.ts`, `${CMD[chain]} '${metadata}';\n`);
fs.writeFileSync(`${path}-rpc.ts`, `${CMD[chain]} ${JSON.stringify(methods, null, 2)};\n`);
fs.writeFileSync(`${path}-ver.ts`, `${CMD[chain]} ${JSON.stringify(version, null, 2)};\n`);

console.log(`Retrieved ${chain}/${version.specVersion}, metadata v${metaVer}`);
const getUrl = () => {
const [,, command, ...args] = process.argv;

if (command === '--url') {
return args.join(' ');
}

return 'http://127.0.0.1:9944';
};

const main = async () => {
let requestId = 0;

const chainUrl = getUrl();

/**
*
* @param {'rpc_methods' | 'state_getMetadata' | 'state_getRuntimeVersion' | 'state_call'} method
* @param {string[]} params
* @returns {Promise<any>}
*/
async function get (method, params = []) {
const res = await fetch(chainUrl, {
body: JSON.stringify({
id: ++requestId,
jsonrpc: '2.0',
method,
params
}),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
method: 'POST'
});
const body = await res.json();

return body.result;
}

/** @type {[string[], string, { specName: 'polkadot' | 'kusama' | 'node'; specVersion: string; }]} */
const [methods, metadata, version] = await Promise.all([
get('rpc_methods'),
get('state_call', ['Metadata_metadata_at_version', META_VERSION_HEX]),
get('state_getRuntimeVersion')
]);
const chain = version.specName === 'node'
? 'substrate'
: version.specName;
const metaVer = parseInt(metadata.substring(10, 12), 16);
const path = `packages/types-support/src/metadata/v${META_VERSION}/${chain}`;

fs.writeFileSync(`${path}-hex.ts`, `${CMD[chain]} '${metadata}';\n`);
fs.writeFileSync(`${path}-rpc.ts`, `${CMD[chain]} ${JSON.stringify(methods, null, 2)};\n`);
fs.writeFileSync(`${path}-ver.ts`, `${CMD[chain]} ${JSON.stringify(version, null, 2)};\n`);

console.log(`Retrieved ${chain}/${version.specVersion}, metadata v${metaVer}`);
};

main().catch((err) => console.error(err)).finally(() => process.exit());
Loading