@1hive/connect-core
Version:
Access and interact with Aragon Organizations and their apps.
62 lines • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toNetwork = exports.networkFromName = exports.networkFromChainId = void 0;
const errors_1 = require("../errors");
const params_1 = require("../params");
function networkFromChainId(chainId) {
return params_1.NETWORKS.find((network) => network.chainId === chainId) || null;
}
exports.networkFromChainId = networkFromChainId;
function networkFromName(name) {
return params_1.NETWORKS.find((network) => network.name === name) || null;
}
exports.networkFromName = networkFromName;
function networkFromObject({ chainId, ensAddress, name, }) {
var _a;
if (name === undefined && chainId === undefined) {
throw new errors_1.ErrorInvalidNetwork(`Network: no name or chainId passed. ` +
`Please provide at least one of these.`);
}
// Handle the case of having a name but no chainId.
if (name !== undefined && chainId === undefined) {
chainId = (_a = networkFromName(name)) === null || _a === void 0 ? void 0 : _a.chainId;
if (chainId === undefined) {
throw new errors_1.ErrorInvalidNetwork(`Network: invalid name provided: ${name}. ` +
`Please use provide a chainId or use one of the following names: ` +
params_1.NETWORKS.map((network) => network.chainId).join(', ') +
`.`);
}
}
// Just a little help for TypeScript, at this
// point we know that chainId cannot be undefined.
chainId = chainId;
const chainIdNetwork = networkFromChainId(chainId);
if (!chainIdNetwork) {
throw new errors_1.ErrorInvalidNetwork(`Network: invalid chainId provided: ${chainId}. ` +
`Please use one of the following: ` +
params_1.NETWORKS.map((network) => network.chainId).join(', ') +
`.`);
}
// We compare with undefined to accept empty strings.
if (name === undefined) {
name = chainIdNetwork.name;
}
if (!ensAddress) {
ensAddress = chainIdNetwork.ensAddress;
}
return { chainId, ensAddress, name };
}
function toNetwork(network) {
if (!network) {
throw new errors_1.ErrorInvalidNetwork(`Network: incorrect value provided.`);
}
if (typeof network === 'string') {
return networkFromObject({ name: network });
}
if (typeof network === 'number') {
return networkFromObject({ chainId: network });
}
return networkFromObject(network);
}
exports.toNetwork = toNetwork;
//# sourceMappingURL=network.js.map