Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@web3-react/metamask

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@web3-react/metamask - npm Package Compare versions

Comparing version 8.0.6-alpha.0 to 8.0.6-beta.0

dist/cjs/index.js

18

dist/index.d.ts

@@ -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"
}
src/index.ts
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