@soundsright/connector
Version:
soundsright wallet connector
156 lines (155 loc) • 6.54 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetaMaskConnection = void 0;
const ethers_1 = require("ethers");
const config_1 = require("@soundsright/config");
const types_1 = require("../types");
// 单例
class MetaMaskConnection extends types_1.Connection {
constructor() {
super();
this.eagerConnection = null;
this.ethereum = null;
}
static getInstance() {
if (!MetaMaskConnection._instance) {
MetaMaskConnection._instance = new this();
}
return MetaMaskConnection._instance;
}
_initialize() {
return __awaiter(this, void 0, void 0, function* () {
// const ethereum = (await import('@metamask/detect-provider')).default() as any;
const { ethereum } = window;
if (!ethereum) {
throw new types_1.NoEthereumProviderError();
}
this.state.provider = new ethers_1.ethers.providers.Web3Provider(ethereum);
ethereum.on('connect', () => {
if (!ethereum.selectedAddress) {
return;
}
this.state.provider = new ethers_1.ethers.providers.Web3Provider(ethereum);
this.state.signer = this.state.provider.getSigner();
this.state.chainId = Number.parseInt(ethereum.chainId);
this.state.account = ethereum.selectedAddress;
this.state.connected = true;
this.emitChange('connect');
});
ethereum.on('disconnect', (error) => {
this.disconnect(error);
});
ethereum.on('accountsChanged', (accounts) => {
if (!accounts[0]) {
this.disconnect();
return;
}
this.state.signer = this.state.provider.getSigner();
this.state.account = accounts[0];
this.emitChange('accountsChanged');
});
ethereum.on('chainChanged', (chainId) => {
this.state.provider = new ethers_1.ethers.providers.Web3Provider(ethereum);
this.state.signer = this.state.provider.getSigner();
this.state.chainId = Number.parseInt(chainId);
this.emitChange('chainChanged');
});
this.ethereum = ethereum;
});
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.eagerConnection) {
this.eagerConnection = this._initialize();
}
return this.eagerConnection;
});
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
yield this.initialize();
const { ethereum } = this;
const accounts = yield ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
this.state.account = account;
this.state.signer = this.state.provider.getSigner();
this.state.chainId = Number.parseInt(ethereum.chainId);
this.state.connected = true;
this.emitChange('connect');
return this.state;
});
}
switchChain(chainId) {
return __awaiter(this, void 0, void 0, function* () {
if (chainId === this.state.chainId)
return;
const { ethereum } = this;
const chainIdStr = `0x${Number(chainId).toString(16)}`;
try {
yield ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainIdStr }],
});
}
catch (e) {
if (e.code !== 4902)
throw e;
const network = config_1.NETWORKS[chainId];
yield ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: chainIdStr,
chainName: network.chainName,
rpcUrls: network.rpcUrls,
nativeCurrency: network.nativeCurrency,
blockExplorerUrls: network.blockExplorerUrls,
}],
});
}
yield new Promise((resolve) => {
const timer = setTimeout(() => {
this.state.provider = new ethers_1.ethers.providers.Web3Provider(ethereum);
this.state.signer = this.state.provider.getSigner();
this.state.chainId = chainId;
resolve(undefined);
}, 2000);
this.once('chainChanged', () => {
if (timer)
clearTimeout(timer);
resolve(undefined);
});
});
});
}
addToken(options) {
return __awaiter(this, void 0, void 0, function* () {
const { ethereum } = this;
yield ethereum.request({
method: 'wallet_watchAsset',
params: {
type: 'ERC20',
options,
},
});
});
}
disconnect(error) {
return __awaiter(this, void 0, void 0, function* () {
this.state.account = undefined;
this.state.chainId = undefined;
this.state.signer = undefined;
this.state.connected = undefined;
this.emitChange('disconnect', error);
});
}
}
exports.MetaMaskConnection = MetaMaskConnection;