@darwinia/types-known
Version:
A JavaScript wrapper for the Polkadot JsonRPC interface
153 lines (115 loc) • 7.06 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getMetadataTypes = getMetadataTypes;
exports.getModuleTypes = getModuleTypes;
exports.getSpecAlias = getSpecAlias;
exports.getSpecRpc = getSpecRpc;
exports.getSpecTypes = getSpecTypes;
exports.getUpgradeVersion = getUpgradeVersion;
var _util = require("@polkadot/util");
var _chain = _interopRequireDefault(require("./chain"));
var _metadata = _interopRequireDefault(require("./metadata"));
var _modules = _interopRequireDefault(require("./modules"));
var _spec = _interopRequireDefault(require("./spec"));
var _upgrades = _interopRequireDefault(require("./upgrades"));
var _warnings = _interopRequireDefault(require("./warnings"));
// Copyright 2017-2022 @darwinia/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
// flatten a VersionedType[] into a Record<string, string>
/** @internal */
function filterVersions() {
let versions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let specVersion = arguments.length > 1 ? arguments[1] : undefined;
return versions.filter(_ref => {
let {
minmax: [min, max]
} = _ref;
return ((0, _util.isUndefined)(min) || min === null || specVersion >= min) && ((0, _util.isUndefined)(max) || max === null || specVersion <= max);
}).reduce((result, _ref2) => {
let {
types
} = _ref2;
return { ...result,
...types
};
}, {});
}
/**
* @description Based on the metadata version, return the registry types
*/
function getMetadataTypes(_registry, specVersion) {
return filterVersions(_metadata.default, specVersion);
}
/**
* @description Get types for specific modules (metadata override)
*/
function getModuleTypes(_ref3, section) {
var _knownTypes$typesAlia;
let {
knownTypes
} = _ref3;
return { ...(_modules.default[section] || {}),
...(((_knownTypes$typesAlia = knownTypes.typesAlias) === null || _knownTypes$typesAlia === void 0 ? void 0 : _knownTypes$typesAlia[section]) || {})
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable types (ready for registration)
*/
function getSpecTypes(_ref4, chainName, specName, specVersion) {
var _knownTypes$typesBund, _knownTypes$typesBund2, _knownTypes$typesBund3, _knownTypes$typesBund4, _knownTypes$typesBund5, _knownTypes$typesBund6, _knownTypes$typesSpec, _knownTypes$typesChai;
let {
knownTypes
} = _ref4;
const _chainName = chainName.toString();
const _specName = specName.toString();
const _specVersion = (0, _util.bnToBn)(specVersion).toNumber();
(_warnings.default[_specName] || []).forEach(warning => console.warn(`*** ${warning}`));
return { ...filterVersions(_spec.default[_specName], _specVersion),
...filterVersions(_chain.default[_chainName], _specVersion),
...filterVersions((_knownTypes$typesBund = knownTypes.typesBundle) === null || _knownTypes$typesBund === void 0 ? void 0 : (_knownTypes$typesBund2 = _knownTypes$typesBund.spec) === null || _knownTypes$typesBund2 === void 0 ? void 0 : (_knownTypes$typesBund3 = _knownTypes$typesBund2[_specName]) === null || _knownTypes$typesBund3 === void 0 ? void 0 : _knownTypes$typesBund3.types, _specVersion),
...filterVersions((_knownTypes$typesBund4 = knownTypes.typesBundle) === null || _knownTypes$typesBund4 === void 0 ? void 0 : (_knownTypes$typesBund5 = _knownTypes$typesBund4.chain) === null || _knownTypes$typesBund5 === void 0 ? void 0 : (_knownTypes$typesBund6 = _knownTypes$typesBund5[_chainName]) === null || _knownTypes$typesBund6 === void 0 ? void 0 : _knownTypes$typesBund6.types, _specVersion),
...(((_knownTypes$typesSpec = knownTypes.typesSpec) === null || _knownTypes$typesSpec === void 0 ? void 0 : _knownTypes$typesSpec[_specName]) || {}),
...(((_knownTypes$typesChai = knownTypes.typesChain) === null || _knownTypes$typesChai === void 0 ? void 0 : _knownTypes$typesChai[_chainName]) || {})
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable rpc definitions (ready for registration)
*/
function getSpecRpc(_ref5, chainName, specName) {
var _knownTypes$typesBund7, _knownTypes$typesBund8, _knownTypes$typesBund9, _knownTypes$typesBund10, _knownTypes$typesBund11, _knownTypes$typesBund12;
let {
knownTypes
} = _ref5;
const _chainName = chainName.toString();
const _specName = specName.toString();
return { ...(((_knownTypes$typesBund7 = knownTypes.typesBundle) === null || _knownTypes$typesBund7 === void 0 ? void 0 : (_knownTypes$typesBund8 = _knownTypes$typesBund7.spec) === null || _knownTypes$typesBund8 === void 0 ? void 0 : (_knownTypes$typesBund9 = _knownTypes$typesBund8[_specName]) === null || _knownTypes$typesBund9 === void 0 ? void 0 : _knownTypes$typesBund9.rpc) || {}),
...(((_knownTypes$typesBund10 = knownTypes.typesBundle) === null || _knownTypes$typesBund10 === void 0 ? void 0 : (_knownTypes$typesBund11 = _knownTypes$typesBund10.chain) === null || _knownTypes$typesBund11 === void 0 ? void 0 : (_knownTypes$typesBund12 = _knownTypes$typesBund11[_chainName]) === null || _knownTypes$typesBund12 === void 0 ? void 0 : _knownTypes$typesBund12.rpc) || {})
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable alias definitions (ready for registration)
*/
function getSpecAlias(_ref6, chainName, specName) {
var _knownTypes$typesBund13, _knownTypes$typesBund14, _knownTypes$typesBund15, _knownTypes$typesBund16, _knownTypes$typesBund17, _knownTypes$typesBund18;
let {
knownTypes
} = _ref6;
const _chainName = chainName.toString();
const _specName = specName.toString();
return { ...(knownTypes.typesAlias || {}),
...(((_knownTypes$typesBund13 = knownTypes.typesBundle) === null || _knownTypes$typesBund13 === void 0 ? void 0 : (_knownTypes$typesBund14 = _knownTypes$typesBund13.spec) === null || _knownTypes$typesBund14 === void 0 ? void 0 : (_knownTypes$typesBund15 = _knownTypes$typesBund14[_specName]) === null || _knownTypes$typesBund15 === void 0 ? void 0 : _knownTypes$typesBund15.alias) || {}),
...(((_knownTypes$typesBund16 = knownTypes.typesBundle) === null || _knownTypes$typesBund16 === void 0 ? void 0 : (_knownTypes$typesBund17 = _knownTypes$typesBund16.chain) === null || _knownTypes$typesBund17 === void 0 ? void 0 : (_knownTypes$typesBund18 = _knownTypes$typesBund17[_chainName]) === null || _knownTypes$typesBund18 === void 0 ? void 0 : _knownTypes$typesBund18.alias) || {})
};
}
/**
* @description Returns a version record for known chains where upgrades are being tracked
*/
function getUpgradeVersion(genesisHash, blockNumber) {
const known = _upgrades.default.find(u => genesisHash.eq(u.genesisHash));
return known ? [known.versions.reduce((last, version) => {
return blockNumber.gt(version.blockNumber) ? version : last;
}, undefined), known.versions.find(version => blockNumber.lte(version.blockNumber))] : [undefined, undefined];
}