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.5-alpha.0 to 8.0.5-beta.0

dist/cjs/index.js

22

dist/index.d.ts

@@ -1,13 +0,27 @@

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

96

dist/index.js

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

export class Network extends Connector {
constructor(actions, urlMap) {
/**
* @param urlMap - A mapping from chainIds to RPC urls.
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed.
*/
constructor(actions, urlMap, connectEagerly = true) {
super(actions);
this.providerCache = {};
this.instantiateProvider = (chainId) => __awaiter(this, void 0, void 0, function* () {
if (typeof chainId === 'undefined') {
chainId = Number(Object.keys(urlMap)[0]);
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;
}
// load provider from cache if possible
if (this.providerCache[chainId]) {
// 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);
});
}
// instantiate new provider
const [{ JsonRpcProvider, FallbackProvider }, { Eip1193Bridge }] = yield Promise.all([
import('@ethersproject/providers'),
import('@ethersproject/experimental'),
]);
let urls = urlMap[chainId];
if (typeof urls === 'undefined') {
throw new Error(`no urls provided for chainId ${chainId}`);
}
if (!Array.isArray(urls)) {
urls = [urls];
}
const providers = urls.map((url) => new JsonRpcProvider(url, chainId));
const provider = new Eip1193Bridge(
// TODO: use VoidSigner here?
providers[0].getSigner(), providers.length === 1 ? providers[0] : new FallbackProvider(providers));
this.providerCache[chainId] = provider;
this.provider = provider;
});
}
activate(desiredChainId) {
/**
* Initiates a connection.
*
* @param desiredChainId - The desired chain to connect to.
*/
activate(desiredChainId = Number(Object.keys(this.urlMap)[0])) {
return __awaiter(this, void 0, void 0, function* () {
this.actions.startActivation();
yield this.instantiateProvider(desiredChainId);
// this.provider guaranteed to be defined now, and for the correct chainId
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.5-alpha.0",
"version": "8.0.5-beta.0",
"type": "module",
"exports": "./dist/index.js",
"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.5-alpha.0"
"@ethersproject/experimental": "^5.5.0",
"@ethersproject/providers": "^5.5.1",
"@web3-react/types": "^8.0.3-beta.0"
},
"gitHead": "b66d6733ff21e2ede6f6d5f05f9be4909d3c9ef0"
"devDependencies": {
"@web3-react/store": "^8.0.5-beta.0"
},
"gitHead": "151d10002e2dd8793626cf7f3da6261d8c9b5435"
}
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