UNPKG

@1hive/connect-core

Version:

Access and interact with Aragon Organizations and their apps.

56 lines 2.23 kB
import { ErrorInvalidNetwork } from '../errors'; import { NETWORKS } from '../params'; export function networkFromChainId(chainId) { return NETWORKS.find((network) => network.chainId === chainId) || null; } export function networkFromName(name) { return NETWORKS.find((network) => network.name === name) || null; } function networkFromObject({ chainId, ensAddress, name, }) { var _a; if (name === undefined && chainId === undefined) { throw new 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 ErrorInvalidNetwork(`Network: invalid name provided: ${name}. ` + `Please use provide a chainId or use one of the following names: ` + 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 ErrorInvalidNetwork(`Network: invalid chainId provided: ${chainId}. ` + `Please use one of the following: ` + 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 }; } export function toNetwork(network) { if (!network) { throw new ErrorInvalidNetwork(`Network: incorrect value provided.`); } if (typeof network === 'string') { return networkFromObject({ name: network }); } if (typeof network === 'number') { return networkFromObject({ chainId: network }); } return networkFromObject(network); } //# sourceMappingURL=network.js.map