@daisypayments/react-ethereum
Advanced tools
Comparing version 0.1.2 to 0.1.3
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
@@ -174,5 +210,10 @@ if (!(instance instanceof Constructor)) { | ||
_defineProperty(_assertThisInitialized(_this), "handleConnect", function _callee() { | ||
var response; | ||
return regeneratorRuntime.async(function _callee$(_context) { | ||
_defineProperty(_assertThisInitialized(_this), "handleConnect", | ||
/*#__PURE__*/ | ||
_asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee() { | ||
var response, nextState, _nextState; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
@@ -183,2 +224,10 @@ switch (_context.prev = _context.next) { | ||
if (window.ethereum) { | ||
_context.next = 3; | ||
break; | ||
} | ||
throw new Error("Current browser is not web3 enabled"); | ||
case 3: | ||
_this.setState({ | ||
@@ -188,36 +237,33 @@ awaiting: true | ||
_context.next = 4; | ||
return regeneratorRuntime.awrap(window.ethereum.send("eth_requestAccounts")); | ||
_context.next = 6; | ||
return window.ethereum.send("eth_requestAccounts"); | ||
case 4: | ||
case 6: | ||
response = _context.sent; | ||
nextState = { | ||
ethereum: window.ethereum, | ||
accounts: response ? response["result"] : [], | ||
error: null, | ||
awaiting: false | ||
}; | ||
_this.setState({ | ||
accounts: response["result"], | ||
awaiting: false, | ||
error: null | ||
}); | ||
_this.setState(nextState); | ||
return _context.abrupt("return", { | ||
ethereum: window.ethereum, | ||
accounts: response["result"], | ||
error: null | ||
}); | ||
return _context.abrupt("return", nextState); | ||
case 9: | ||
_context.prev = 9; | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context["catch"](0); | ||
_this.setState({ | ||
_nextState = { | ||
ethereum: window.ethereum, | ||
accounts: [], | ||
error: _context.t0, | ||
awaiting: false | ||
}); | ||
}; | ||
return _context.abrupt("return", { | ||
ethereum: window.ethereum, | ||
accounts: [], | ||
error: _context.t0 | ||
}); | ||
_this.setState(_nextState); | ||
case 13: | ||
return _context.abrupt("return", _nextState); | ||
case 17: | ||
case "end": | ||
@@ -227,4 +273,4 @@ return _context.stop(); | ||
} | ||
}, null, null, [[0, 9]]); | ||
}); | ||
}, _callee, null, [[0, 12]]); | ||
}))); | ||
@@ -250,19 +296,29 @@ _defineProperty(_assertThisInitialized(_this), "handleChainChange", function (chainId) { | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChange", function _callee2(accounts) { | ||
return regeneratorRuntime.async(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_this.setState({ | ||
accounts: accounts | ||
}); | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChange", | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee2(accounts) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_this.setState({ | ||
accounts: accounts | ||
}); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}, _callee2); | ||
})); | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChangeError", function (error) { | ||
@@ -303,43 +359,61 @@ // Metamask says this should never happen | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
var _ref, _ref2, chainId, accounts; | ||
value: function () { | ||
var _componentDidMount = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee3() { | ||
var _ref3, _ref4, a, b; | ||
return regeneratorRuntime.async(function componentDidMount$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (isBrowser()) { | ||
_context3.next = 2; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (!(!isBrowser() || !window.ethereum)) { | ||
_context3.next = 2; | ||
break; | ||
} | ||
return _context3.abrupt("return", void 0); | ||
case 2: | ||
_context3.prev = 2; | ||
this.listenerClose = window.ethereum.on("close", this.handleClose); | ||
this.listenerNetwork = window.ethereum.on("networkChanged", this.handleChainChange); | ||
this.listenerChain = window.ethereum.on("chainChanged", this.handleChainChange); | ||
this.listenerAccount = window.ethereum.on("accountsChanged", this.handleAccountChange); | ||
_context3.next = 9; | ||
return Promise.all([window.ethereum.send("eth_chainId"), window.ethereum.send("eth_accounts")]); | ||
case 9: | ||
_ref3 = _context3.sent; | ||
_ref4 = _slicedToArray(_ref3, 2); | ||
a = _ref4[0]; | ||
b = _ref4[1]; | ||
// const { result: chainId } = await window.ethereum.send("eth_chainId"); | ||
// const { result: accounts } = await window.ethereum.send("eth_accounts"); | ||
this.setState({ | ||
chainId: a ? Number(a.result) || null : null, | ||
accounts: b ? b.result : [] | ||
}); | ||
_context3.next = 19; | ||
break; | ||
} | ||
return _context3.abrupt("return", void 0); | ||
case 16: | ||
_context3.prev = 16; | ||
_context3.t0 = _context3["catch"](2); | ||
console.error("react-metamask:", "Error getting current network and accounts", _context3.t0); | ||
case 2: | ||
this.listenerClose = window.ethereum.on("close", this.handleClose); | ||
this.listenerNetwork = window.ethereum.on("networkChanged", this.handleChainChange); | ||
this.listenerChain = window.ethereum.on("chainChanged", this.handleChainChange); | ||
this.listenerAccount = window.ethereum.on("accountsChanged", this.handleAccountChange); | ||
_context3.next = 8; | ||
return regeneratorRuntime.awrap(Promise.all([window.ethereum.send("eth_chainId"), window.ethereum.send("eth_accounts")])); | ||
case 19: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 16]]); | ||
})); | ||
case 8: | ||
_ref = _context3.sent; | ||
_ref2 = _slicedToArray(_ref, 2); | ||
chainId = _ref2[0].result; | ||
accounts = _ref2[1].result; | ||
// const { result: chainId } = await window.ethereum.send("eth_chainId"); | ||
// const { result: accounts } = await window.ethereum.send("eth_accounts"); | ||
this.setState({ | ||
chainId: Number(chainId) || null, | ||
accounts: accounts | ||
}); | ||
function componentDidMount() { | ||
return _componentDidMount.apply(this, arguments); | ||
} | ||
case 13: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, null, this); | ||
} | ||
return componentDidMount; | ||
}() | ||
}, { | ||
@@ -406,3 +480,3 @@ key: "shouldComponentUpdate", | ||
// `Error` type | ||
chainId: PropTypes.number.isRequired, | ||
chainId: PropTypes.number, | ||
awaiting: PropTypes.bool.isRequired, | ||
@@ -409,0 +483,0 @@ requestConnection: PropTypes.func.isRequired |
@@ -11,2 +11,38 @@ 'use strict'; | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { | ||
try { | ||
var info = gen[key](arg); | ||
var value = info.value; | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (info.done) { | ||
resolve(value); | ||
} else { | ||
Promise.resolve(value).then(_next, _throw); | ||
} | ||
} | ||
function _asyncToGenerator(fn) { | ||
return function () { | ||
var self = this, | ||
args = arguments; | ||
return new Promise(function (resolve, reject) { | ||
var gen = fn.apply(self, args); | ||
function _next(value) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); | ||
} | ||
function _throw(err) { | ||
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); | ||
} | ||
_next(undefined); | ||
}); | ||
}; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
@@ -182,5 +218,10 @@ if (!(instance instanceof Constructor)) { | ||
_defineProperty(_assertThisInitialized(_this), "handleConnect", function _callee() { | ||
var response; | ||
return regeneratorRuntime.async(function _callee$(_context) { | ||
_defineProperty(_assertThisInitialized(_this), "handleConnect", | ||
/*#__PURE__*/ | ||
_asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee() { | ||
var response, nextState, _nextState; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
@@ -191,2 +232,10 @@ switch (_context.prev = _context.next) { | ||
if (window.ethereum) { | ||
_context.next = 3; | ||
break; | ||
} | ||
throw new Error("Current browser is not web3 enabled"); | ||
case 3: | ||
_this.setState({ | ||
@@ -196,36 +245,33 @@ awaiting: true | ||
_context.next = 4; | ||
return regeneratorRuntime.awrap(window.ethereum.send("eth_requestAccounts")); | ||
_context.next = 6; | ||
return window.ethereum.send("eth_requestAccounts"); | ||
case 4: | ||
case 6: | ||
response = _context.sent; | ||
nextState = { | ||
ethereum: window.ethereum, | ||
accounts: response ? response["result"] : [], | ||
error: null, | ||
awaiting: false | ||
}; | ||
_this.setState({ | ||
accounts: response["result"], | ||
awaiting: false, | ||
error: null | ||
}); | ||
_this.setState(nextState); | ||
return _context.abrupt("return", { | ||
ethereum: window.ethereum, | ||
accounts: response["result"], | ||
error: null | ||
}); | ||
return _context.abrupt("return", nextState); | ||
case 9: | ||
_context.prev = 9; | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context["catch"](0); | ||
_this.setState({ | ||
_nextState = { | ||
ethereum: window.ethereum, | ||
accounts: [], | ||
error: _context.t0, | ||
awaiting: false | ||
}); | ||
}; | ||
return _context.abrupt("return", { | ||
ethereum: window.ethereum, | ||
accounts: [], | ||
error: _context.t0 | ||
}); | ||
_this.setState(_nextState); | ||
case 13: | ||
return _context.abrupt("return", _nextState); | ||
case 17: | ||
case "end": | ||
@@ -235,4 +281,4 @@ return _context.stop(); | ||
} | ||
}, null, null, [[0, 9]]); | ||
}); | ||
}, _callee, null, [[0, 12]]); | ||
}))); | ||
@@ -258,19 +304,29 @@ _defineProperty(_assertThisInitialized(_this), "handleChainChange", function (chainId) { | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChange", function _callee2(accounts) { | ||
return regeneratorRuntime.async(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_this.setState({ | ||
accounts: accounts | ||
}); | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChange", | ||
/*#__PURE__*/ | ||
function () { | ||
var _ref2 = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee2(accounts) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_this.setState({ | ||
accounts: accounts | ||
}); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
case 1: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}, _callee2); | ||
})); | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
_defineProperty(_assertThisInitialized(_this), "handleAccountChangeError", function (error) { | ||
@@ -311,43 +367,61 @@ // Metamask says this should never happen | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
var _ref, _ref2, chainId, accounts; | ||
value: function () { | ||
var _componentDidMount = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee3() { | ||
var _ref3, _ref4, a, b; | ||
return regeneratorRuntime.async(function componentDidMount$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (isBrowser()) { | ||
_context3.next = 2; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (!(!isBrowser() || !window.ethereum)) { | ||
_context3.next = 2; | ||
break; | ||
} | ||
return _context3.abrupt("return", void 0); | ||
case 2: | ||
_context3.prev = 2; | ||
this.listenerClose = window.ethereum.on("close", this.handleClose); | ||
this.listenerNetwork = window.ethereum.on("networkChanged", this.handleChainChange); | ||
this.listenerChain = window.ethereum.on("chainChanged", this.handleChainChange); | ||
this.listenerAccount = window.ethereum.on("accountsChanged", this.handleAccountChange); | ||
_context3.next = 9; | ||
return Promise.all([window.ethereum.send("eth_chainId"), window.ethereum.send("eth_accounts")]); | ||
case 9: | ||
_ref3 = _context3.sent; | ||
_ref4 = _slicedToArray(_ref3, 2); | ||
a = _ref4[0]; | ||
b = _ref4[1]; | ||
// const { result: chainId } = await window.ethereum.send("eth_chainId"); | ||
// const { result: accounts } = await window.ethereum.send("eth_accounts"); | ||
this.setState({ | ||
chainId: a ? Number(a.result) || null : null, | ||
accounts: b ? b.result : [] | ||
}); | ||
_context3.next = 19; | ||
break; | ||
} | ||
return _context3.abrupt("return", void 0); | ||
case 16: | ||
_context3.prev = 16; | ||
_context3.t0 = _context3["catch"](2); | ||
console.error("react-metamask:", "Error getting current network and accounts", _context3.t0); | ||
case 2: | ||
this.listenerClose = window.ethereum.on("close", this.handleClose); | ||
this.listenerNetwork = window.ethereum.on("networkChanged", this.handleChainChange); | ||
this.listenerChain = window.ethereum.on("chainChanged", this.handleChainChange); | ||
this.listenerAccount = window.ethereum.on("accountsChanged", this.handleAccountChange); | ||
_context3.next = 8; | ||
return regeneratorRuntime.awrap(Promise.all([window.ethereum.send("eth_chainId"), window.ethereum.send("eth_accounts")])); | ||
case 19: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[2, 16]]); | ||
})); | ||
case 8: | ||
_ref = _context3.sent; | ||
_ref2 = _slicedToArray(_ref, 2); | ||
chainId = _ref2[0].result; | ||
accounts = _ref2[1].result; | ||
// const { result: chainId } = await window.ethereum.send("eth_chainId"); | ||
// const { result: accounts } = await window.ethereum.send("eth_accounts"); | ||
this.setState({ | ||
chainId: Number(chainId) || null, | ||
accounts: accounts | ||
}); | ||
function componentDidMount() { | ||
return _componentDidMount.apply(this, arguments); | ||
} | ||
case 13: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, null, this); | ||
} | ||
return componentDidMount; | ||
}() | ||
}, { | ||
@@ -414,3 +488,3 @@ key: "shouldComponentUpdate", | ||
// `Error` type | ||
chainId: PropTypes.number.isRequired, | ||
chainId: PropTypes.number, | ||
awaiting: PropTypes.bool.isRequired, | ||
@@ -417,0 +491,0 @@ requestConnection: PropTypes.func.isRequired |
{ | ||
"name": "@daisypayments/react-ethereum", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Ethereum context for React", | ||
@@ -35,21 +35,21 @@ "author": "Patricio López Juri <patricio.juri@consensys.net> (https://lopezjuri.com)", | ||
"devDependencies": { | ||
"@babel/core": "^7.7.5", | ||
"@babel/helpers": "^7.7.4", | ||
"@babel/plugin-external-helpers": "^7.7.4", | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", | ||
"@babel/plugin-transform-runtime": "^7.7.6", | ||
"@babel/preset-env": "^7.7.6", | ||
"@babel/preset-react": "^7.7.4", | ||
"@daisypayments/eslint-config": "^1.7.0", | ||
"@babel/core": "^7.8.3", | ||
"@babel/helpers": "^7.8.3", | ||
"@babel/plugin-external-helpers": "^7.8.3", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.8.3", | ||
"@babel/plugin-transform-runtime": "^7.8.3", | ||
"@babel/preset-env": "^7.8.3", | ||
"@babel/preset-react": "^7.8.3", | ||
"@daisypayments/eslint-config": "^2.0.3", | ||
"babel-jest": "^24.9.0", | ||
"cross-env": "^6.0.3", | ||
"eslint": "^6.7.2", | ||
"gh-pages": "^2.1.1", | ||
"eslint": "^6.8.0", | ||
"gh-pages": "^2.2.0", | ||
"react": "^16.12.0", | ||
"react-dom": "^16.12.0", | ||
"react-scripts": "^3.3.0", | ||
"rollup": "^1.27.11", | ||
"rollup": "^1.29.0", | ||
"rollup-plugin-babel": "^4.3.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
58605
821