2018-10-09 23:48:25 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import Web3 from 'web3'
|
|
|
|
import styles from './Web3Donation.module.scss'
|
|
|
|
|
|
|
|
const ONE_SECOND = 1000
|
|
|
|
const ONE_MINUTE = ONE_SECOND * 60
|
|
|
|
|
|
|
|
export default class Web3Donation extends PureComponent {
|
|
|
|
state = {
|
|
|
|
web3Connected: false,
|
|
|
|
networkError: null,
|
|
|
|
networkId: null,
|
|
|
|
accounts: [],
|
|
|
|
selectedAccount: null,
|
2018-10-11 23:18:03 +02:00
|
|
|
receipt: null,
|
|
|
|
transactionHash: null,
|
2018-10-09 23:48:25 +02:00
|
|
|
loading: false,
|
|
|
|
error: null
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
address: PropTypes.string
|
|
|
|
}
|
|
|
|
|
2018-10-11 20:06:02 +02:00
|
|
|
web3 = null
|
2018-10-09 23:48:25 +02:00
|
|
|
interval = null
|
|
|
|
networkInterval = null
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-10-11 20:06:02 +02:00
|
|
|
if (typeof window.web3 === 'undefined') {
|
|
|
|
// no web3
|
|
|
|
this.setState({ web3Connected: false })
|
|
|
|
} else {
|
2018-10-11 23:18:03 +02:00
|
|
|
// this.web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546')
|
|
|
|
this.web3 = new Web3(window.web3.currentProvider)
|
2018-10-09 23:48:25 +02:00
|
|
|
this.setState({ web3Connected: true })
|
|
|
|
|
|
|
|
this.fetchAccounts()
|
|
|
|
this.fetchNetwork()
|
|
|
|
this.initPoll()
|
|
|
|
this.initNetworkPoll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearInterval(this.interval)
|
|
|
|
clearInterval(this.networkInterval)
|
|
|
|
this.setState({ web3Connected: false })
|
|
|
|
}
|
|
|
|
|
|
|
|
initPoll() {
|
|
|
|
if (!this.interval) {
|
|
|
|
this.interval = setInterval(this.fetchAccounts, ONE_SECOND)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initNetworkPoll() {
|
|
|
|
if (!this.networkInterval) {
|
|
|
|
this.networkInterval = setInterval(this.fetchNetwork, ONE_MINUTE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchNetwork = () => {
|
|
|
|
const { web3 } = this
|
|
|
|
|
|
|
|
web3 &&
|
|
|
|
web3.eth &&
|
2018-10-11 23:18:03 +02:00
|
|
|
//web3.eth.net.getId((err, netId) => {
|
|
|
|
web3.version.getNetwork((err, netId) => {
|
2018-10-09 23:48:25 +02:00
|
|
|
if (err) {
|
2018-10-11 20:06:02 +02:00
|
|
|
this.setState({ networkError: err })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (netId != this.state.networkId) {
|
2018-10-09 23:48:25 +02:00
|
|
|
this.setState({
|
2018-10-11 20:06:02 +02:00
|
|
|
networkError: null,
|
|
|
|
networkId: netId
|
2018-10-09 23:48:25 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchAccounts = () => {
|
|
|
|
const { web3 } = this
|
|
|
|
|
|
|
|
web3 &&
|
|
|
|
web3.eth &&
|
|
|
|
web3.eth.getAccounts((err, accounts) => {
|
|
|
|
if (err) {
|
2018-10-11 20:06:02 +02:00
|
|
|
this.setState({ accountsError: err })
|
2018-10-09 23:48:25 +02:00
|
|
|
}
|
2018-10-11 20:06:02 +02:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
accounts,
|
|
|
|
selectedAccount: accounts[0]
|
|
|
|
})
|
2018-10-09 23:48:25 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWeb3Button = () => {
|
|
|
|
const { web3 } = this
|
|
|
|
|
|
|
|
this.setState({ loading: true })
|
|
|
|
|
2018-10-11 23:18:03 +02:00
|
|
|
// web3.eth
|
|
|
|
// .sendTransaction({
|
|
|
|
// from: this.state.selectedAccount,
|
|
|
|
// to: this.props.address,
|
|
|
|
// value: '10000000000000000'
|
|
|
|
// })
|
|
|
|
// .then(receipt => {
|
|
|
|
// this.setState({ receipt, loading: false })
|
|
|
|
// })
|
|
|
|
// .catch(error => {
|
|
|
|
// this.setState({ error, loading: false })
|
|
|
|
// })
|
|
|
|
|
|
|
|
web3.eth.sendTransaction(
|
|
|
|
{
|
2018-10-09 23:48:25 +02:00
|
|
|
from: this.state.selectedAccount,
|
|
|
|
to: this.props.address,
|
|
|
|
value: '10000000000000000'
|
2018-10-11 23:18:03 +02:00
|
|
|
},
|
|
|
|
(error, transactionHash) => {
|
|
|
|
if (error) this.setState({ error, loading: false })
|
|
|
|
if (!transactionHash) this.setState({ loading: true })
|
|
|
|
this.setState({ transactionHash, loading: false })
|
|
|
|
}
|
|
|
|
)
|
2018-10-09 23:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-11 20:06:02 +02:00
|
|
|
return (
|
|
|
|
<div className={styles.web3}>
|
|
|
|
<h4>web3</h4>
|
|
|
|
<p>Send a donation with MetaMask or Mist.</p>
|
|
|
|
|
|
|
|
{this.state.web3Connected ? (
|
|
|
|
<div>
|
|
|
|
{this.state.loading ? (
|
|
|
|
'Hang on...'
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className="btn btn-primary"
|
|
|
|
onClick={this.handleWeb3Button}
|
|
|
|
disabled={
|
2018-10-11 23:18:03 +02:00
|
|
|
!(this.state.networkId === '1') || !this.state.selectedAccount
|
2018-10-11 20:06:02 +02:00
|
|
|
}
|
|
|
|
>
|
|
|
|
Make it rain 0.01 Ξ
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{this.state.accounts.length === 0 && (
|
|
|
|
<div className={styles.alert}>
|
|
|
|
Web3 detected, but no account. Are you logged into your MetaMask
|
|
|
|
account?
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2018-10-11 23:18:03 +02:00
|
|
|
{this.state.networkId !== '1' && (
|
2018-10-11 20:06:02 +02:00
|
|
|
<div className={styles.alert}>Please connect to Main network</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{this.state.error && (
|
|
|
|
<div className={styles.alert}>{this.state.error.message}</div>
|
|
|
|
)}
|
|
|
|
|
2018-10-11 23:18:03 +02:00
|
|
|
{this.state.transactionHash && (
|
2018-10-11 20:06:02 +02:00
|
|
|
<div className={styles.success}>
|
|
|
|
You are awesome, thanks!
|
|
|
|
<br />
|
|
|
|
<a
|
2018-10-11 23:18:03 +02:00
|
|
|
href={`https://etherscan.io/tx/${this.state.transactionHash}`}
|
2018-10-09 23:48:25 +02:00
|
|
|
>
|
2018-10-11 20:06:02 +02:00
|
|
|
See your transaction on etherscan.io.
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className={styles.alert}>No Web3 capable browser detected.</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
2018-10-09 23:48:25 +02:00
|
|
|
}
|
|
|
|
}
|