Do not rely on web3 injection with new ethereum provider standard

when window.ethereum is available web3 is likely not available as the
browser uses the new provider standard.
So we either user the window.ethereum as provider or the
web3.currentProvider as legacy option.
This commit is contained in:
2019-04-09 22:37:48 +02:00
parent 0af78e3dbd
commit da608a0012
+4 -5
View File
@@ -50,9 +50,9 @@ export default Service.extend({
}); });
} }
function instantiateWithAccount (web3, context) { function instantiateWithAccount (web3Provider, context) {
console.debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask'); console.debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
ethProvider = new ethers.providers.Web3Provider(web3.currentProvider); ethProvider = new ethers.providers.Web3Provider(web3Provider);
ethProvider.listAccounts().then(accounts => { ethProvider.listAccounts().then(accounts => {
context.set('currentUserAccounts', accounts); context.set('currentUserAccounts', accounts);
const ethSigner = accounts.length === 0 ? null : ethProvider.getSigner(); const ethSigner = accounts.length === 0 ? null : ethProvider.getSigner();
@@ -64,19 +64,18 @@ export default Service.extend({
} }
if (window.ethereum) { if (window.ethereum) {
window.web3 = new window.Web3(window.ethereum);
try { try {
// Request account access if needed // Request account access if needed
await window.ethereum.enable(); await window.ethereum.enable();
// Acccounts now exposed // Acccounts now exposed
instantiateWithAccount(window.web3, this); instantiateWithAccount(window.ethereum, this);
} catch (error) { } catch (error) {
instantiateWithoutAccount(); instantiateWithoutAccount();
} }
} }
// Legacy dapp browsers... // Legacy dapp browsers...
else if (window.web3) { else if (window.web3) {
instantiateWithAccount(window.web3, this); instantiateWithAccount(window.web3.currentProvider, this);
} }
// Non-dapp browsers... // Non-dapp browsers...
else { else {