@web3-react/network
Advanced tools
Comparing version 8.0.4-alpha.0 to 8.0.5-alpha.0
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 { ConnectionInfo } from '@ethersproject/web'; | ||
@@ -8,6 +6,9 @@ declare type url = string | ConnectionInfo; | ||
provider: Provider | undefined; | ||
private providerCache; | ||
private readonly instantiateProvider; | ||
constructor(actions: Actions, urls: url | url[], network?: Networkish, fallbackProviderConfigs?: Omit<FallbackProviderConfig, 'provider'>[], quorum?: number); | ||
activate(): Promise<void>; | ||
constructor(actions: Actions, urlMap: { | ||
[chainId: number]: url | url[]; | ||
}); | ||
activate(desiredChainId?: number): Promise<void>; | ||
} | ||
export {}; |
@@ -12,19 +12,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class Network extends Connector { | ||
constructor(actions, urls, network, fallbackProviderConfigs, quorum) { | ||
constructor(actions, urlMap) { | ||
super(actions); | ||
if (!Array.isArray(urls)) { | ||
if (fallbackProviderConfigs) { | ||
throw new Error('fallbackProviderConfigs defined with a single url'); | ||
this.providerCache = {}; | ||
this.instantiateProvider = (chainId) => __awaiter(this, void 0, void 0, function* () { | ||
if (typeof chainId === 'undefined') { | ||
chainId = Number(Object.keys(urlMap)[0]); | ||
} | ||
if (quorum) { | ||
throw new Error('quorum defined with a single url'); | ||
// load provider from cache if possible | ||
if (this.providerCache[chainId]) { | ||
this.provider = this.providerCache[chainId]; | ||
} | ||
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* () { | ||
// instantiate new provider | ||
const [{ JsonRpcProvider, FallbackProvider }, { Eip1193Bridge }] = yield Promise.all([ | ||
@@ -34,18 +29,23 @@ import('@ethersproject/providers'), | ||
]); | ||
const providers = urls.map((url) => new JsonRpcProvider(url, network)); | ||
this.provider = new Eip1193Bridge( | ||
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(fallbackProviderConfigs | ||
? fallbackProviderConfigs.map((config, i) => (Object.assign(Object.assign({}, config), { provider: providers[i] }))) | ||
: providers, quorum)); | ||
providers[0].getSigner(), providers.length === 1 ? providers[0] : new FallbackProvider(providers)); | ||
this.providerCache[chainId] = provider; | ||
this.provider = provider; | ||
}); | ||
} | ||
activate() { | ||
activate(desiredChainId) { | ||
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' }); | ||
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: [] }); | ||
@@ -52,0 +52,0 @@ }); |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "8.0.4-alpha.0", | ||
"version": "8.0.5-alpha.0", | ||
"type": "module", | ||
@@ -19,5 +19,5 @@ "exports": "./dist/index.js", | ||
"@ethersproject/providers": "^5.4.5", | ||
"@web3-react/types": "^8.0.4-alpha.0" | ||
"@web3-react/types": "^8.0.5-alpha.0" | ||
}, | ||
"gitHead": "002652f883ab79f86c47c4336e7ef165cd39838d" | ||
"gitHead": "b66d6733ff21e2ede6f6d5f05f9be4909d3c9ef0" | ||
} |
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 { ConnectionInfo } from '@ethersproject/web' | ||
@@ -11,28 +9,19 @@ | ||
private readonly instantiateProvider: () => Promise<void> | ||
private providerCache: { [chainId: number]: Provider } = {} | ||
private readonly instantiateProvider: (chainId?: number) => Promise<void> | ||
constructor( | ||
actions: Actions, | ||
urls: url | url[], | ||
network?: Networkish, | ||
fallbackProviderConfigs?: Omit<FallbackProviderConfig, 'provider'>[], | ||
quorum?: number | ||
) { | ||
constructor(actions: Actions, urlMap: { [chainId: number]: url | url[] }) { | ||
super(actions) | ||
if (!Array.isArray(urls)) { | ||
if (fallbackProviderConfigs) { | ||
throw new Error('fallbackProviderConfigs defined with a single url') | ||
this.instantiateProvider = async (chainId) => { | ||
if (typeof chainId === 'undefined') { | ||
chainId = Number(Object.keys(urlMap)[0]) | ||
} | ||
if (quorum) { | ||
throw new Error('quorum defined with a single url') | ||
// load provider from cache if possible | ||
if (this.providerCache[chainId]) { | ||
this.provider = this.providerCache[chainId] | ||
} | ||
urls = [urls] | ||
} else { | ||
if (fallbackProviderConfigs && fallbackProviderConfigs.length !== urls.length) { | ||
throw new Error('fallbackProviderConfigs length does not match urls length') | ||
} | ||
} | ||
this.instantiateProvider = async () => { | ||
// instantiate new provider | ||
const [{ JsonRpcProvider, FallbackProvider }, { Eip1193Bridge }] = await Promise.all([ | ||
@@ -43,26 +32,29 @@ import('@ethersproject/providers'), | ||
const providers = (urls as url[]).map((url) => new JsonRpcProvider(url, network)) | ||
let urls = urlMap[chainId] | ||
if (typeof urls === 'undefined') { | ||
throw new Error(`no urls provided for chainId ${chainId}`) | ||
} | ||
if (!Array.isArray(urls)) { | ||
urls = [urls] | ||
} | ||
this.provider = new Eip1193Bridge( | ||
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( | ||
fallbackProviderConfigs | ||
? fallbackProviderConfigs.map((config, i) => ({ ...config, provider: providers[i] })) | ||
: providers, | ||
quorum | ||
) | ||
providers.length === 1 ? providers[0] : new FallbackProvider(providers) | ||
) | ||
this.providerCache[chainId] = provider | ||
this.provider = provider | ||
} | ||
} | ||
public async activate(): Promise<void> { | ||
public async activate(desiredChainId?: number): Promise<void> { | ||
this.actions.startActivation() | ||
await this.instantiateProvider() | ||
// this.provider guaranteed to be defined now | ||
await this.instantiateProvider(desiredChainId) | ||
// this.provider guaranteed to be defined now, and for the correct chainId | ||
const chainId = await ((this.provider as Provider).request({ method: 'eth_chainId' }) as Promise<number>) | ||
const chainId = (await this.provider!.request({ method: 'eth_chainId' })) as number | ||
@@ -69,0 +61,0 @@ this.actions.update({ chainId, accounts: [] }) |
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
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
40839
119