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.10-alpha.0 to 8.0.10-beta.0

dist/cjs/index.cjs

22

dist/index.d.ts

@@ -1,14 +0,28 @@

import { Connector, Actions } from '@web3-react/types';
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 {
/** {@inheritdoc Connector.provider} */
provider: Eip1193Bridge | undefined;
private urlMap;
private chainId;
private defaultChainId;
private providerCache;
/**
* @param urlMap - A mapping from chainIds to RPC urls.
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed.
* @param defaultChainId - The chainId to connect to if connectEagerly is true.
*/
constructor(actions: Actions, urlMap: {
[chainId: number]: url | url[];
}, connectEagerly?: boolean);
private initialize;
}, connectEagerly?: boolean, defaultChainId?: number);
private isomorphicInitialize;
/**
* Initiates a connection.
*
* @param desiredChainId - The desired chain to connect to.
*/
activate(desiredChainId?: number): Promise<void>;
}
export {};

87

dist/index.js

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

export class Network extends Connector {
constructor(actions, urlMap, connectEagerly = true) {
/**
* @param urlMap - A mapping from chainIds to RPC urls.
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed.
* @param defaultChainId - The chainId to connect to if connectEagerly is true.
*/
constructor(actions, urlMap, connectEagerly = false, defaultChainId = Number(Object.keys(urlMap)[0])) {
super(actions);
this.providerCache = {};
if (connectEagerly && typeof window === 'undefined') {
throw new Error('connectEagerly = true is invalid for SSR, instead use the activate method in a useEffect');
}
this.urlMap = Object.keys(urlMap).reduce((accumulator, chainId) => {

@@ -21,57 +29,42 @@ const urls = urlMap[Number(chainId)];

}, {});
// use the first chainId in urlMap as the default
this.chainId = Number(Object.keys(this.urlMap)[0]);
if (connectEagerly) {
this.initialize();
}
this.defaultChainId = defaultChainId;
if (connectEagerly)
void this.activate();
}
initialize() {
isomorphicInitialize(chainId) {
return __awaiter(this, void 0, void 0, function* () {
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'),
import('@ethersproject/experimental'),
]);
if (this.providerCache[chainId])
return this.providerCache[chainId];
return (this.providerCache[chainId] = Promise.all([
import('@ethersproject/providers').then(({ JsonRpcProvider, FallbackProvider }) => ({
JsonRpcProvider,
FallbackProvider,
})),
import('@ethersproject/experimental').then(({ Eip1193Bridge }) => Eip1193Bridge),
]).then(([{ JsonRpcProvider, FallbackProvider }, 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;
}
// 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) {
const provider = this.providerCache[chainId];
return 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);
});
}
return new Eip1193Bridge(providers[0].getSigner(), providers.length === 1 ? providers[0] : new FallbackProvider(providers));
}));
});
}
activate(desiredChainId = Number(Object.keys(this.urlMap)[0])) {
/**
* Initiates a connection.
*
* @param desiredChainId - The desired chain to connect to.
*/
activate(desiredChainId = this.defaultChainId) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof 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();
this.actions.startActivation();
this.provider = yield this.isomorphicInitialize(desiredChainId);
return this.provider
.request({ method: 'eth_chainId' })
.then((chainId) => {
this.actions.update({ chainId, accounts: [] });
})
.catch((error) => {
this.actions.reportError(error);
});
});
}
}
{
"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.10-alpha.0",
"type": "module",
"version": "8.0.10-beta.0",
"files": [

@@ -12,17 +17,24 @@ "dist/*"

"exports": {
"import": "./dist/index.js"
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/cjs/index.cjs"
},
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/cjs/index.cjs",
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"build": "tsc && tsc --project tsconfig.cjs.json",
"postbuild": "mv dist/cjs/index.js dist/cjs/index.cjs",
"start": "tsc --watch"
},
"dependencies": {
"@ethersproject/experimental": "^5.4.0",
"@ethersproject/providers": "^5.4.5",
"@web3-react/types": "^8.0.10-alpha.0"
"@ethersproject/experimental": "^5.5.0",
"@ethersproject/providers": "^5.5.1",
"@web3-react/types": "^8.0.7-beta.0"
},
"gitHead": "f043db8e2c9d4e64b77fe76b41a82ad254770a98"
"devDependencies": {
"@web3-react/store": "^8.0.9-beta.0"
},
"gitHead": "72cf341cd9b7f52bccdad19d880388303b69c61f"
}
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