UNPKG

@colligence/web3-react-klip

Version:
106 lines (105 loc) 5.02 kB
"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KLIP = void 0; const providers_1 = require("@ethersproject/providers"); const klip_web3_provider_1 = require("@klaytn/klip-web3-provider"); const web3modal_1 = __importDefault(require("@klaytn/web3modal")); const types_1 = require("@web3-react/types"); function parseChainId(chainId) { return typeof chainId === 'string' ? Number.parseInt(chainId, 16) : chainId; } class KLIP extends types_1.Connector { constructor({ actions, options, onError }) { var _a; super(actions, onError); this.restoreProvider = () => __awaiter(this, void 0, void 0, function* () { var _b; const web3Modal = new web3modal_1.default({ providerOptions: this.options }); if (web3Modal.cachedProvider) { try { console.log('---------------- restoreProvider -----------------------'); const externalProvider = yield web3Modal.connect(); this.provider = new providers_1.Web3Provider(externalProvider); } catch (err) { console.error('이전 provider 복원 실패:', err); } } return (yield ((_b = this.provider) === null || _b === void 0 ? void 0 : _b.request({ method: 'eth_accounts' }))); }); const providerOptions = { klip: { package: klip_web3_provider_1.KlipWeb3Provider, options: { bappName: options.bappName, rpcUrl: (_a = options.rpcUrl) !== null && _a !== void 0 ? _a : 'https://public-en.node.kaia.io', disableInjectedProvider: true, }, }, }; this.options = providerOptions; const web3Modal = new web3modal_1.default({ providerOptions, cacheProvider: true }); web3Modal.connect().then((externalProvider) => { const ethersProvider = new providers_1.Web3Provider(externalProvider); this.provider = ethersProvider; }); if (this.provider) { this.provider.on('connect', ({ chainId }) => { this.actions.update({ chainId: parseChainId(chainId) }); }); this.provider.on('disconnect', (error) => { var _a; this.actions.resetState(); (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error); }); this.provider.on('chainChanged', (chainId) => { this.actions.update({ chainId: parseChainId(chainId) }); }); this.provider.on('accountsChanged', (accounts) => { this.actions.update({ accounts }); }); } } activateAccounts(requestAccounts) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const cancelActivation = this.actions.startActivation(); try { // Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing // chains; they should be requested serially, with accounts first, so that the chainId can settle. const accounts = yield requestAccounts(); const chainId = ((_b = (yield ((_a = this.provider) === null || _a === void 0 ? void 0 : _a.request({ method: 'eth_chainId' })))) !== null && _b !== void 0 ? _b : '0x2019'); this.actions.update({ chainId: parseChainId(chainId), accounts }); } catch (error) { cancelActivation(); throw error; } }); } /** {@inheritdoc Connector.connectEagerly} */ connectEagerly() { return __awaiter(this, void 0, void 0, function* () { return this.activateAccounts(this.restoreProvider); }); } /** {@inheritdoc Connector.activate} */ activate() { return __awaiter(this, void 0, void 0, function* () { return this.activateAccounts(this.restoreProvider); }); } } exports.KLIP = KLIP;