-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onChangeAccount callback and selectedAccount context variable
- Loading branch information
Showing
1 changed file
with
68 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,88 @@ | ||
import React, { Component } from 'react'; | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import Web3 from 'web3' | ||
|
||
const ONE_SECOND = 1000; | ||
|
||
class Web3Provider extends Component { | ||
constructor(props, context) { | ||
super(props, context) | ||
|
||
this.state = { | ||
selectedAccount: null | ||
} | ||
|
||
this.web3 = null | ||
this.interval = null | ||
|
||
} | ||
render() { | ||
const { web3UnavailableScreen: Web3UnavailableScreen } = this.props | ||
|
||
if (window.web3) { | ||
if (!this.web3) { | ||
this.web3 = new Web3(window.web3.currentProvider); | ||
this.fetchAccounts() | ||
} | ||
|
||
return this.props.children | ||
} | ||
|
||
return <this.props.web3UnavailableScreen /> | ||
return <Web3UnavailableScreen /> | ||
} | ||
|
||
componentDidMount() { | ||
this.initPoll(); | ||
} | ||
|
||
initPoll = () => { | ||
if (!this.interval) { | ||
this.interval = setInterval(this.fetchAccounts, ONE_SECOND) | ||
} | ||
} | ||
|
||
fetchAccounts = () => { | ||
const { web3 } = this | ||
const { onChangeAccount } = this.props | ||
|
||
if (!web3 || !web3.eth) { | ||
return | ||
} | ||
|
||
return web3.eth.getAccounts() | ||
.then(accounts => { | ||
if (!accounts || !accounts.length) { | ||
return | ||
} | ||
|
||
let curr = this.state.selectedAccount | ||
let next = accounts[0] | ||
curr = curr && curr.toLowerCase() | ||
next = next && next.toLowerCase() | ||
|
||
const didChange = curr && next && (curr !== next) | ||
|
||
if (didChange && typeof onChangeAccount === 'function') { | ||
onChangeAccount(next) | ||
} | ||
|
||
this.setState({ | ||
selectedAccount: next || null | ||
}) | ||
}) | ||
} | ||
|
||
getChildContext() { | ||
return { | ||
web3: window.web3 | ||
web3: this.web3, | ||
selectedAccount: this.state.selectedAccount | ||
} | ||
} | ||
} | ||
|
||
Web3Provider.childContextTypes = { | ||
web3: PropTypes.object | ||
web3: PropTypes.object, | ||
selectedAccount: PropTypes.string | ||
} | ||
|
||
export default Web3Provider; |