Created
March 5, 2018 22:54
-
-
Save kyriediculous/54ceb4a54b2da170b0553ca3af7d8fbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Web3 from 'web3' | |
/* | |
* 1. Check for injected web3 (mist/metamask) | |
* 2. If metamask/mist create a new web3 instance and pass on result | |
* 3. Get networkId - Now we can check the user is connected to the right network to use our dApp | |
* 4. Get user account from metamask | |
* 5. Get user balance | |
*/ | |
let getWeb3 = new Promise(function (resolve, reject) { | |
// Check for injected web3 (mist/metamask) | |
var web3js = window.web3 | |
if (typeof web3js !== 'undefined') { | |
var web3 = new Web3(web3js.currentProvider) | |
resolve({ | |
injectedWeb3: web3.isConnected(), | |
web3 () { | |
return web3 | |
} | |
}) | |
} else { | |
// web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545')) GANACHE FALLBACK | |
reject(new Error('Unable to connect to Metamask')) | |
} | |
}) | |
.then(result => { | |
return new Promise(function (resolve, reject) { | |
// Retrieve network ID | |
result.web3().version.getNetwork((err, networkId) => { | |
if (err) { | |
// If we can't find a networkId keep result the same and reject the promise | |
reject(new Error('Unable to retrieve network ID')) | |
} else { | |
// Assign the networkId property to our result and resolve promise | |
result = Object.assign({}, result, {networkId}) | |
resolve(result) | |
} | |
}) | |
}) | |
}) | |
.then(result => { | |
return new Promise(function (resolve, reject) { | |
// Retrieve coinbase | |
result.web3().eth.getCoinbase((err, coinbase) => { | |
if (err) { | |
reject(new Error('Unable to retrieve coinbase')) | |
} else { | |
result = Object.assign({}, result, { coinbase }) | |
resolve(result) | |
} | |
}) | |
}) | |
}) | |
.then(result => { | |
return new Promise(function (resolve, reject) { | |
// Retrieve balance for coinbase | |
result.web3().eth.getBalance(result.coinbase, (err, balance) => { | |
if (err) { | |
reject(new Error('Unable to retrieve balance for address: ' + result.coinbase)) | |
} else { | |
result = Object.assign({}, result, { balance }) | |
resolve(result) | |
} | |
}) | |
}) | |
}) | |
export default getWeb3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am using this box for truffle and react.
But I´m getting an error that says "metamask no longer injects web3." Can I get help solving this?