@web3-react/metamask
Advanced tools
Comparing version 8.0.25-beta.0 to 8.0.26-beta.0
@@ -12,18 +12,17 @@ import type detectEthereumProvider from '@metamask/detect-provider'; | ||
} | ||
/** | ||
* @param options - Options to pass to `@metamask/detect-provider` | ||
* @param onError - Handler to report errors thrown from eventListeners. | ||
*/ | ||
export interface MetaMaskConstructorArgs { | ||
actions: Actions; | ||
options?: Parameters<typeof detectEthereumProvider>[0]; | ||
onError?: (error: Error) => void; | ||
} | ||
export declare class MetaMask extends Connector { | ||
/** {@inheritdoc Connector.provider} */ | ||
provider: MetaMaskProvider | undefined; | ||
provider?: MetaMaskProvider; | ||
private readonly options?; | ||
private eagerConnection?; | ||
/** | ||
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed. | ||
* @param options - Options to pass to `@metamask/detect-provider` | ||
* @param onError - Handler to report errors thrown from eventListeners. | ||
*/ | ||
constructor({ actions, connectEagerly, options, onError, }: { | ||
actions: Actions; | ||
connectEagerly?: boolean; | ||
options?: Parameters<typeof detectEthereumProvider>[0]; | ||
onError?: (error: Error) => void; | ||
}); | ||
constructor({ actions, options, onError }: MetaMaskConstructorArgs); | ||
private isomorphicInitialize; | ||
@@ -30,0 +29,0 @@ /** {@inheritdoc Connector.connectEagerly} */ |
@@ -49,15 +49,5 @@ "use strict"; | ||
class MetaMask extends types_1.Connector { | ||
/** | ||
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed. | ||
* @param options - Options to pass to `@metamask/detect-provider` | ||
* @param onError - Handler to report errors thrown from eventListeners. | ||
*/ | ||
constructor({ actions, connectEagerly, options, onError, }) { | ||
constructor({ actions, options, onError }) { | ||
super(actions, onError); | ||
if (connectEagerly && this.serverSide) { | ||
throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect'); | ||
} | ||
this.options = options; | ||
if (connectEagerly) | ||
void this.connectEagerly(); | ||
} | ||
@@ -67,9 +57,9 @@ isomorphicInitialize() { | ||
if (this.eagerConnection) | ||
return this.eagerConnection; | ||
yield (this.eagerConnection = Promise.resolve().then(() => __importStar(require('@metamask/detect-provider'))).then((m) => m.default(this.options)) | ||
.then((provider) => { | ||
return; | ||
return (this.eagerConnection = Promise.resolve().then(() => __importStar(require('@metamask/detect-provider'))).then((m) => __awaiter(this, void 0, void 0, function* () { | ||
var _a, _b; | ||
const provider = yield m.default(this.options); | ||
if (provider) { | ||
this.provider = provider; | ||
// edge case if e.g. metamask and coinbase wallet are both installed | ||
// handle the case when e.g. metamask and coinbase wallet are both installed | ||
if ((_a = this.provider.providers) === null || _a === void 0 ? void 0 : _a.length) { | ||
@@ -99,3 +89,3 @@ this.provider = (_b = this.provider.providers.find((p) => p.isMetaMask)) !== null && _b !== void 0 ? _b : this.provider.providers[0]; | ||
} | ||
})); | ||
}))); | ||
}); | ||
@@ -140,45 +130,44 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let cancelActivation; | ||
if (!((_b = (_a = this.provider) === null || _a === void 0 ? void 0 : _a.isConnected) === null || _b === void 0 ? void 0 : _b.call(_a))) | ||
this.actions.startActivation(); | ||
yield this.isomorphicInitialize(); | ||
if (!this.provider) { | ||
throw new NoMetaMaskError(); | ||
} | ||
return Promise.all([ | ||
this.provider.request({ method: 'eth_chainId' }), | ||
this.provider.request({ method: 'eth_requestAccounts' }), | ||
]).then(([chainId, accounts]) => { | ||
const receivedChainId = parseChainId(chainId); | ||
const desiredChainId = typeof desiredChainIdOrChainParameters === 'number' | ||
? desiredChainIdOrChainParameters | ||
: desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId; | ||
// if there's no desired chain, or it's equal to the received, update | ||
if (!desiredChainId || receivedChainId === desiredChainId) | ||
return this.actions.update({ chainId: receivedChainId, accounts }); | ||
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`; | ||
// if we're here, we can try to switch networks | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.provider.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: desiredChainIdHex }], | ||
}) | ||
.catch((error) => { | ||
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') { | ||
// if we're here, we can try to add a new network | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.provider.request({ | ||
method: 'wallet_addEthereumChain', | ||
params: [Object.assign(Object.assign({}, desiredChainIdOrChainParameters), { chainId: desiredChainIdHex })], | ||
}); | ||
} | ||
else { | ||
this.actions.resetState(); | ||
cancelActivation = this.actions.startActivation(); | ||
return this.isomorphicInitialize() | ||
.then(() => __awaiter(this, void 0, void 0, function* () { | ||
if (!this.provider) | ||
throw new NoMetaMaskError(); | ||
return Promise.all([ | ||
this.provider.request({ method: 'eth_chainId' }), | ||
this.provider.request({ method: 'eth_requestAccounts' }), | ||
]).then(([chainId, accounts]) => { | ||
const receivedChainId = parseChainId(chainId); | ||
const desiredChainId = typeof desiredChainIdOrChainParameters === 'number' | ||
? desiredChainIdOrChainParameters | ||
: desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId; | ||
// if there's no desired chain, or it's equal to the received, update | ||
if (!desiredChainId || receivedChainId === desiredChainId) | ||
return this.actions.update({ chainId: receivedChainId, accounts }); | ||
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`; | ||
// if we're here, we can try to switch networks | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.provider.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: desiredChainIdHex }], | ||
}) | ||
.catch((error) => { | ||
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') { | ||
// if we're here, we can try to add a new network | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.provider.request({ | ||
method: 'wallet_addEthereumChain', | ||
params: [Object.assign(Object.assign({}, desiredChainIdOrChainParameters), { chainId: desiredChainIdHex })], | ||
}); | ||
} | ||
throw error; | ||
} | ||
}) | ||
.then(() => this.activate(desiredChainId)) | ||
.catch((error) => { | ||
this.actions.resetState(); | ||
throw error; | ||
}) | ||
.then(() => this.activate(desiredChainId)); | ||
}); | ||
})) | ||
.catch((error) => { | ||
cancelActivation === null || cancelActivation === void 0 ? void 0 : cancelActivation(); | ||
throw error; | ||
}); | ||
@@ -185,0 +174,0 @@ }); |
@@ -13,3 +13,3 @@ { | ||
}, | ||
"version": "8.0.25-beta.0", | ||
"version": "8.0.26-beta.0", | ||
"files": [ | ||
@@ -29,8 +29,8 @@ "dist/*" | ||
"@metamask/detect-provider": "^1.2.0", | ||
"@web3-react/types": "^8.0.18-beta.0" | ||
"@web3-react/types": "^8.0.19-beta.0" | ||
}, | ||
"devDependencies": { | ||
"@web3-react/store": "^8.0.23-beta.0" | ||
"@web3-react/store": "^8.0.24-beta.0" | ||
}, | ||
"gitHead": "b497295f7f4aeecf86845551e8fd0ee53d27d202" | ||
"gitHead": "9a5d6207c54457de7081755d5d69e5bd7c9fda31" | ||
} |
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
47595
239