UNPKG

@broxus/js-core

Version:

MobX-based JavaScript Core library

42 lines (41 loc) 1.6 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) { networks.forEach(value => { if (!connections.has(value.chainId.toString())) { connections.set(value.chainId, makeConnection(value)); } }); if (connections.has(chainId)) { const connection = connections.get(chainId); if (!connection) { throw new Error('Cannot get TVM 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('Cannot get TVM connection'); }