@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
32 lines (31 loc) • 1.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEvmConnection = getEvmConnection;
const web3_1 = __importDefault(require("web3"));
const connections = new Map();
function getEvmConnection(networks, chainId, network) {
if (Array.isArray(networks) && chainId) {
let connection = connections.get(chainId);
if (!connection) {
const _network = networks.find(value => value.chainId.toString() === chainId.toString());
if (_network?.rpcUrl) {
connection = new web3_1.default(_network.rpcUrl);
}
}
if (!connection) {
throw new Error(`The ${network?.shortName} connection was not established`);
}
connections.set(chainId, connection);
return connection;
}
const _network = 'rpcUrl' in networks && 'chainId' in networks ? networks : network;
if (_network?.rpcUrl) {
const connection = new web3_1.default(_network.rpcUrl);
connections.set(_network.chainId, connection);
return connection;
}
throw new Error(`The ${_network?.shortName} connection was not established`);
}