1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-26 11:49:04 +01:00

adapt for upcoming MetaMask changes

This commit is contained in:
Matthias Kretschmann 2018-10-22 19:24:53 +02:00
parent 0c472a3d91
commit 9ebf183567
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -4,7 +4,7 @@ import Web3 from 'web3'
import InputGroup from './InputGroup' import InputGroup from './InputGroup'
import Alerts from './Alerts' import Alerts from './Alerts'
import styles from './index.module.scss' import styles from './index.module.scss'
import { getNetworkName } from './utils' import { getNetworkName, Logger } from './utils'
const ONE_SECOND = 1000 const ONE_SECOND = 1000
const ONE_MINUTE = ONE_SECOND * 60 const ONE_MINUTE = ONE_SECOND * 60
@ -31,41 +31,37 @@ export default class Web3Donation extends PureComponent {
networkInterval = null networkInterval = null
componentDidMount() { componentDidMount() {
this.initAllTheTings() this.initWeb3()
} }
componentWillUnmount() { componentWillUnmount() {
this.resetAllTheThings() this.resetAllTheThings()
} }
// getPermissions = async ethereum => { async initWeb3() {
// try {
// // Request account access if needed
// await ethereum.enable()
// } catch (error) {
// // User denied account access...
// Logger.error(error)
// }
// }
initAllTheTings() {
// Modern dapp browsers... // Modern dapp browsers...
// if (window.ethereum) { if (window.ethereum) {
// this.web3 = new Web3(window.ethereum) this.web3 = new Web3(window.ethereum)
// this.setState({ web3Connected: true })
// this.getPermissions(this.web3.eth)
// }
try {
// Request account access
await window.ethereum.enable()
this.setState({ web3Connected: true })
this.initAllTheTings()
} catch (error) {
// User denied account access...
Logger.error(error)
this.setState({ error })
}
}
// Legacy dapp browsers... // Legacy dapp browsers...
if (window.web3) { else if (window.web3) {
// this.web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546') // this.web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546')
this.web3 = new Web3(window.web3.currentProvider) this.web3 = new Web3(window.web3.currentProvider)
this.setState({ web3Connected: true }) this.setState({ web3Connected: true })
this.fetchAccounts() this.initAllTheTings()
this.fetchNetwork()
this.initAccountsPoll()
this.initNetworkPoll()
} }
// Non-dapp browsers... // Non-dapp browsers...
else { else {
@ -73,6 +69,13 @@ export default class Web3Donation extends PureComponent {
} }
} }
initAllTheTings() {
this.fetchAccounts()
this.fetchNetwork()
this.initAccountsPoll()
this.initNetworkPoll()
}
resetAllTheThings() { resetAllTheThings() {
clearInterval(this.interval) clearInterval(this.interval)
clearInterval(this.networkInterval) clearInterval(this.networkInterval)
@ -177,6 +180,7 @@ export default class Web3Donation extends PureComponent {
error, error,
transactionHash transactionHash
} = this.state } = this.state
const hasCorrectNetwork = networkId === '1' const hasCorrectNetwork = networkId === '1'
const hasAccount = accounts.length !== 0 const hasAccount = accounts.length !== 0