UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

42 lines (41 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTvmConnection = getTvmConnection; /* eslint-disable @stylistic/max-len */ const everscale_inpage_provider_1 = require("everscale-inpage-provider"); const adapters_1 = require("../adapters"); const connections = new Map(); function makeConnection(network) { return new everscale_inpage_provider_1.ProviderRpcClient({ provider: new adapters_1.StandaloneClientAdapter({ connection: network.connectionProperties || { data: { endpoint: network.rpcUrl }, id: Number(network.chainId), type: process.env.NODE_ENV === 'production' ? 'proto' : 'jrpc', }, }), }); } function getTvmConnection(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) { connection = makeConnection(_network); } } 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 = makeConnection(_network); connections.set(_network.chainId, connection); return connection; } throw new Error(`The ${_network?.shortName} connection was not established`); }