Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@web3-react/network

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@web3-react/network - npm Package Compare versions

Comparing version 8.0.1-alpha.0 to 8.0.1-beta.0

dist/cjs/index.js

19

dist/index.d.ts

@@ -1,12 +0,17 @@

import { Connector, Actions, Provider } from '@web3-react/types';
import type { Networkish } from '@ethersproject/networks';
import type { FallbackProviderConfig } from '@ethersproject/providers/lib.esm/fallback-provider';
import type { Eip1193Bridge } from '@ethersproject/experimental';
import type { ConnectionInfo } from '@ethersproject/web';
import type { Actions } from '@web3-react/types';
import { Connector } from '@web3-react/types';
declare type url = string | ConnectionInfo;
export declare class Network extends Connector {
provider: Provider | undefined;
private readonly instantiateProvider;
constructor(actions: Actions, urls: url | url[], network?: Networkish, fallbackProviderConfigs?: Omit<FallbackProviderConfig, 'provider'>[], quorum?: number);
activate(): Promise<void>;
provider: Eip1193Bridge | undefined;
private urlMap;
private chainId;
private providerCache;
constructor(actions: Actions, urlMap: {
[chainId: number]: url | url[];
}, connectEagerly?: boolean);
private initialize;
activate(desiredChainId?: number): Promise<void>;
}
export {};

@@ -12,42 +12,69 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

export class Network extends Connector {
constructor(actions, urls, network, fallbackProviderConfigs, quorum) {
constructor(actions, urlMap, connectEagerly = true) {
super(actions);
if (!Array.isArray(urls)) {
if (fallbackProviderConfigs) {
throw new Error('fallbackProviderConfigs defined with a single url');
this.providerCache = {};
this.urlMap = Object.keys(urlMap).reduce((accumulator, chainId) => {
const urls = urlMap[Number(chainId)];
accumulator[Number(chainId)] = Array.isArray(urls) ? urls : [urls];
return accumulator;
}, {});
// use the first chainId in urlMap as the default
this.chainId = Number(Object.keys(this.urlMap)[0]);
if (connectEagerly) {
void this.initialize();
}
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
this.provider = undefined;
this.actions.startActivation();
// cache the desired chainId before async logic
const chainId = this.chainId;
// populate the provider cache if necessary
if (!this.providerCache[chainId]) {
// instantiate new provider
const [{ JsonRpcProvider, FallbackProvider }, Eip1193Bridge] = yield Promise.all([
import('@ethersproject/providers').then(({ JsonRpcProvider, FallbackProvider }) => ({
JsonRpcProvider,
FallbackProvider,
})),
import('@ethersproject/experimental').then(({ Eip1193Bridge }) => Eip1193Bridge),
]);
const urls = this.urlMap[chainId];
const providers = urls.map((url) => new JsonRpcProvider(url, chainId));
const provider = new Eip1193Bridge(providers[0].getSigner(), providers.length === 1 ? providers[0] : new FallbackProvider(providers));
this.providerCache[chainId] = provider;
}
if (quorum) {
throw new Error('quorum defined with a single url');
// once we're here, the cache is guaranteed to be initialized
// so, if the current chainId still matches the one at the beginning of the call, update
if (chainId === this.chainId) {
this.provider = this.providerCache[chainId];
return this.provider
.request({ method: 'eth_chainId' })
.then((returnedChainId) => {
if (returnedChainId !== chainId) {
// this means the returned chainId was unexpected, i.e. the provided url(s) were wrong
throw new Error(`expected chainId ${chainId}, received ${returnedChainId}`);
}
// again we have to make sure the chainIds match, to prevent race conditions
if (chainId === this.chainId) {
this.actions.update({ chainId, accounts: [] });
}
})
.catch((error) => {
this.actions.reportError(error);
});
}
urls = [urls];
}
else {
if (fallbackProviderConfigs && fallbackProviderConfigs.length !== urls.length) {
throw new Error('fallbackProviderConfigs length does not match urls length');
}
}
this.instantiateProvider = () => __awaiter(this, void 0, void 0, function* () {
const [{ JsonRpcProvider, FallbackProvider }, { Eip1193Bridge }] = yield Promise.all([
import('@ethersproject/providers').then((m) => { var _a; return (_a = m === null || m === void 0 ? void 0 : m.default) !== null && _a !== void 0 ? _a : m; }),
import('@ethersproject/experimental').then((m) => { var _a; return (_a = m === null || m === void 0 ? void 0 : m.default) !== null && _a !== void 0 ? _a : m; }),
]);
const providers = urls.map((url) => new JsonRpcProvider(url, network));
this.provider = new Eip1193Bridge(
// TODO: use VoidSigner here?
providers[0].getSigner(), providers.length === 1
? providers[0]
: new FallbackProvider(fallbackProviderConfigs
? fallbackProviderConfigs.map((config, i) => (Object.assign(Object.assign({}, config), { provider: providers[i] })))
: providers, quorum));
});
}
activate() {
activate(desiredChainId = Number(Object.keys(this.urlMap)[0])) {
return __awaiter(this, void 0, void 0, function* () {
this.actions.startActivation();
yield this.instantiateProvider();
// this.provider guaranteed to be defined now
const chainId = yield this.provider.request({ method: 'eth_chainId' });
this.actions.update({ chainId, accounts: [] });
if (this.urlMap[desiredChainId] === undefined) {
throw new Error(`no url(s) provided for desiredChainId ${desiredChainId}`);
}
// set the connector's chainId to the target, to prevent race conditions
this.chainId = desiredChainId;
return this.initialize();
});
}
}
{
"name": "@web3-react/network",
"keywords": [
"web3-react"
],
"author": "Noah Zinsmeister <noahwz@gmail.com>",
"license": "GPL-3.0-or-later",
"repository": "github:NoahZinsmeister/web3-react",
"publishConfig": {
"access": "public"
},
"version": "8.0.1-alpha.0",
"version": "8.0.1-beta.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*"
],
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/cjs/index.js"
},
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"main": "./dist/cjs/index.js",
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"build": "tsc && tsc --project tsconfig.cjs.json",
"start": "tsc --watch"
},
"dependencies": {
"@ethersproject/experimental": "^5.4.0",
"@ethersproject/providers": "^5.4.5",
"@web3-react/types": "^8.0.1-alpha.0"
"@ethersproject/experimental": "^5.5.0",
"@ethersproject/providers": "^5.5.1",
"@web3-react/types": "^8.0.1-beta.0"
},
"gitHead": "f642ebf18fd798537b081d945bdc6e8806d9a507"
"devDependencies": {
"@web3-react/store": "^8.0.1-beta.0"
},
"gitHead": "cbe15d77b9b9a237562733d7d217ec7ef0ef2e39"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc