@web3-react/metamask
Advanced tools
Comparing version 8.0.6-alpha.0 to 8.0.6-beta.0
@@ -1,3 +0,4 @@ | ||
import { Connector, Actions } from '@web3-react/types'; | ||
import type detectEthereumProvider from '@metamask/detect-provider'; | ||
import type { Actions, AddEthereumChainParameter } from '@web3-react/types'; | ||
import { Connector } from '@web3-react/types'; | ||
export declare class NoMetaMaskError extends Error { | ||
@@ -9,5 +10,18 @@ constructor(); | ||
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` | ||
*/ | ||
constructor(actions: Actions, connectEagerly?: boolean, options?: Parameters<typeof detectEthereumProvider>[0]); | ||
private initialize; | ||
activate(): Promise<void>; | ||
/** | ||
* Initiates a connection. | ||
* | ||
* @param desiredChainIdOrChainParameters - If defined, indicates the desired chain to connect to. If the user is | ||
* already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch | ||
* to the chain, if one of two conditions is met: either they already have it added in their extension, or the | ||
* argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the | ||
* specified parameters first, before being prompted to switch. | ||
*/ | ||
activate(desiredChainIdOrChainParameters?: number | AddEthereumChainParameter): Promise<void>; | ||
} |
@@ -22,2 +22,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class MetaMask extends 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` | ||
*/ | ||
constructor(actions, connectEagerly = true, options) { | ||
@@ -32,2 +36,6 @@ super(actions); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let cancelActivation; | ||
if (connectEagerly) { | ||
cancelActivation = this.actions.startActivation(); | ||
} | ||
return import('@metamask/detect-provider') | ||
@@ -49,3 +57,9 @@ .then((m) => m.default(this.options)) | ||
this.provider.on('accountsChanged', (accounts) => { | ||
this.actions.update({ accounts }); | ||
if (accounts.length === 0) { | ||
// handle this edge case by disconnecting | ||
this.actions.reportError(undefined); | ||
} | ||
else { | ||
this.actions.update({ accounts }); | ||
} | ||
}); | ||
@@ -61,13 +75,32 @@ if (connectEagerly) { | ||
} | ||
else { | ||
throw new Error('No accounts returned'); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.debug('Could not connect eagerly', error); | ||
cancelActivation(); | ||
}); | ||
} | ||
} | ||
else if (connectEagerly) { | ||
cancelActivation(); | ||
} | ||
}); | ||
}); | ||
} | ||
activate() { | ||
/** | ||
* Initiates a connection. | ||
* | ||
* @param desiredChainIdOrChainParameters - If defined, indicates the desired chain to connect to. If the user is | ||
* already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch | ||
* to the chain, if one of two conditions is met: either they already have it added in their extension, or the | ||
* argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the | ||
* specified parameters first, before being prompted to switch. | ||
*/ | ||
activate(desiredChainIdOrChainParameters) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const desiredChainId = typeof desiredChainIdOrChainParameters === 'number' | ||
? desiredChainIdOrChainParameters | ||
: desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId; | ||
this.actions.startActivation(); | ||
@@ -78,19 +111,42 @@ if (!this.eagerConnection) { | ||
yield this.eagerConnection; | ||
if (this.provider) { | ||
return Promise.all([ | ||
this.provider.request({ method: 'eth_chainId' }), | ||
this.provider.request({ method: 'eth_requestAccounts' }), | ||
]) | ||
.then(([chainId, accounts]) => { | ||
this.actions.update({ chainId: parseChainId(chainId), accounts }); | ||
if (!this.provider) { | ||
return this.actions.reportError(new NoMetaMaskError()); | ||
} | ||
return Promise.all([ | ||
this.provider.request({ method: 'eth_chainId' }), | ||
this.provider.request({ method: 'eth_requestAccounts' }), | ||
]) | ||
.then(([chainId, accounts]) => { | ||
const receivedChainId = parseChainId(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 }); | ||
} | ||
// if we're here, we can try to switch networks | ||
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return this.provider.request({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: desiredChainIdHex }], | ||
}) | ||
.catch((error) => { | ||
this.actions.reportError(error); | ||
}); | ||
} | ||
else { | ||
this.actions.reportError(new NoMetaMaskError()); | ||
} | ||
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 { | ||
throw error; | ||
} | ||
}) | ||
.then(() => this.activate(desiredChainId)); | ||
}) | ||
.catch((error) => { | ||
this.actions.reportError(error); | ||
}); | ||
}); | ||
} | ||
} |
{ | ||
"name": "@web3-react/metamask", | ||
"keywords": [ | ||
"web3-react", | ||
"metamask" | ||
], | ||
"author": "Noah Zinsmeister <noahwz@gmail.com>", | ||
"license": "GPL-3.0-or-later", | ||
"repository": "github:NoahZinsmeister/web3-react", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"version": "8.0.6-alpha.0", | ||
"version": "8.0.6-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" | ||
@@ -17,5 +33,8 @@ }, | ||
"@metamask/detect-provider": "^1.2.0", | ||
"@web3-react/types": "^8.0.6-alpha.0" | ||
"@web3-react/types": "^8.0.3-beta.0" | ||
}, | ||
"gitHead": "b32f036ca775ecf65861b2c4a4bae09e7cb26718" | ||
"devDependencies": { | ||
"@web3-react/store": "^8.0.5-beta.0" | ||
}, | ||
"gitHead": "151d10002e2dd8793626cf7f3da6261d8c9b5435" | ||
} |
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
53089
343
1
0
2
1
3
1