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.6-alpha.0

9

dist/index.d.ts

@@ -1,13 +0,14 @@

import { Connector, Actions, Provider } from '@web3-react/types';
import { Connector, Actions } from '@web3-react/types';
import type { ConnectionInfo } from '@ethersproject/web';
declare type url = string | ConnectionInfo;
export declare class Network extends Connector {
provider: Provider | undefined;
private urlMap;
private chainId;
private providerCache;
private readonly instantiateProvider;
constructor(actions: Actions, urlMap: {
[chainId: number]: url | url[];
});
}, connectEagerly?: boolean);
private initialize;
activate(desiredChainId?: number): Promise<void>;
}
export {};

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

export class Network extends Connector {
constructor(actions, urlMap) {
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) {
this.initialize();
}
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
// 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'),
]);
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]) {
this.provider = 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) {
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);
});
}
// 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) {
activate(desiredChainId = Number(Object.keys(this.urlMap)[0])) {
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;
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: [] });
return this.initialize();
});
}
}

@@ -6,3 +6,3 @@ {

},
"version": "8.0.5-alpha.0",
"version": "8.0.6-alpha.0",
"type": "module",

@@ -19,5 +19,5 @@ "exports": "./dist/index.js",

"@ethersproject/providers": "^5.4.5",
"@web3-react/types": "^8.0.5-alpha.0"
"@web3-react/types": "^8.0.6-alpha.0"
},
"gitHead": "b66d6733ff21e2ede6f6d5f05f9be4909d3c9ef0"
"gitHead": "b32f036ca775ecf65861b2c4a4bae09e7cb26718"
}

@@ -7,20 +7,27 @@ import { Connector, Actions, Provider } from '@web3-react/types'

export class Network extends Connector {
public provider: Provider | undefined
private urlMap: { [chainId: number]: url[] }
private chainId: number
private providerCache: { [chainId: number]: Provider } = {}
private readonly instantiateProvider: (chainId?: number) => Promise<void>
constructor(actions: Actions, urlMap: { [chainId: number]: url | url[] }) {
constructor(actions: Actions, urlMap: { [chainId: number]: url | url[] }, connectEagerly = true) {
super(actions)
this.urlMap = Object.keys(urlMap).reduce<{ [chainId: number]: url[] }>((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])
this.instantiateProvider = async (chainId) => {
if (typeof chainId === 'undefined') {
chainId = Number(Object.keys(urlMap)[0])
}
if (connectEagerly) {
this.initialize()
}
}
// load provider from cache if possible
if (this.providerCache[chainId]) {
this.provider = this.providerCache[chainId]
}
private async initialize(): Promise<void> {
// cache the desired chainId before async logic
const chainId = this.chainId
// populate the provider cache if necessary
if (!this.providerCache[chainId]) {
// instantiate new provider

@@ -32,13 +39,6 @@ const [{ JsonRpcProvider, FallbackProvider }, { Eip1193Bridge }] = await Promise.all([

let urls = urlMap[chainId]
if (typeof urls === 'undefined') {
throw new Error(`no urls provided for chainId ${chainId}`)
}
if (!Array.isArray(urls)) {
urls = [urls]
}
const urls = this.urlMap[chainId]
const providers = urls.map((url) => new JsonRpcProvider(url, chainId))
const provider = new Eip1193Bridge(
// TODO: use VoidSigner here?
providers[0].getSigner(),

@@ -49,16 +49,39 @@ providers.length === 1 ? providers[0] : new FallbackProvider(providers)

this.providerCache[chainId] = provider
this.provider = 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)
})
}
}
public async activate(desiredChainId?: number): Promise<void> {
public async activate(desiredChainId: number = Number(Object.keys(this.urlMap)[0])): Promise<void> {
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
this.actions.startActivation()
await this.instantiateProvider(desiredChainId)
// this.provider guaranteed to be defined now, and for the correct chainId
const chainId = (await this.provider!.request({ method: 'eth_chainId' })) as number
this.actions.update({ chainId, accounts: [] })
return this.initialize()
}
}
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