@web3-react/network
Advanced tools
Comparing version 8.0.10-alpha.0 to 8.0.10-beta.0
@@ -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 {}; |
@@ -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" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
45187
6
187
1
0
2
1
3
1
No