1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-17 18:13:14 +02:00
blog/src/components/Web3Donation/index.jsx

207 lines
4.9 KiB
React
Raw Normal View History

2018-10-09 23:48:25 +02:00
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import Web3 from 'web3'
2018-10-13 20:37:13 +02:00
import InputGroup from './InputGroup'
import Alerts from './Alerts'
import styles from './index.module.scss'
2018-10-13 23:35:21 +02:00
import { getNetworkName } from './utils'
2018-10-09 23:48:25 +02:00
const ONE_SECOND = 1000
const ONE_MINUTE = ONE_SECOND * 60
export default class Web3Donation extends PureComponent {
state = {
web3Connected: false,
networkId: null,
2018-10-13 20:13:50 +02:00
networkName: null,
2018-10-09 23:48:25 +02:00
accounts: [],
selectedAccount: null,
2018-10-13 15:40:05 +02:00
amount: 0.01,
2018-10-11 23:18:03 +02:00
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-13 20:13:50 +02:00
this.initAllTheTings()
}
componentWillUnmount() {
this.resetAllTheThings()
}
// getPermissions = async ethereum => {
// try {
// // Request account access if needed
// await ethereum.enable()
// } catch (error) {
// // User denied account access...
// console.log(error)
// }
// }
initAllTheTings() {
// Modern dapp browsers...
// if (window.ethereum) {
// this.web3 = new Web3(window.ethereum)
// this.setState({ web3Connected: true })
// this.getPermissions(this.web3.eth)
// }
// Legacy dapp browsers...
if (window.web3) {
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()
2018-10-13 20:13:50 +02:00
this.initAccountsPoll()
2018-10-09 23:48:25 +02:00
this.initNetworkPoll()
}
2018-10-13 20:13:50 +02:00
// Non-dapp browsers...
else {
this.setState({ web3Connected: false })
}
2018-10-09 23:48:25 +02:00
}
2018-10-13 20:13:50 +02:00
resetAllTheThings() {
2018-10-09 23:48:25 +02:00
clearInterval(this.interval)
clearInterval(this.networkInterval)
2018-10-13 23:35:21 +02:00
this.setState({ web3Connected: false })
2018-10-09 23:48:25 +02:00
}
2018-10-13 20:13:50 +02:00
initAccountsPoll() {
2018-10-09 23:48:25 +02:00
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-13 23:35:21 +02:00
if (err) this.setState({ error: err })
2018-10-11 20:06:02 +02:00
if (netId != this.state.networkId) {
2018-10-09 23:48:25 +02:00
this.setState({
2018-10-13 23:35:21 +02:00
error: null,
networkId: netId
})
getNetworkName(netId).then(networkName => {
this.setState({ networkName: networkName })
2018-10-09 23:48:25 +02:00
})
}
})
}
fetchAccounts = () => {
const { web3 } = this
web3 &&
web3.eth &&
web3.eth.getAccounts((err, accounts) => {
2018-10-13 23:35:21 +02:00
if (err) this.setState({ error: err })
2018-10-11 20:06:02 +02:00
this.setState({
2018-10-13 23:35:21 +02:00
error: null,
2018-10-11 20:06:02 +02:00
accounts,
selectedAccount: accounts[0]
})
2018-10-09 23:48:25 +02:00
})
}
2018-10-13 23:35:21 +02:00
handleButton = () => {
2018-10-09 23:48:25 +02:00
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,
2018-10-13 15:40:05 +02:00
value: this.state.amount * 1e18 // ETH -> Wei
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
}
2018-10-13 15:40:05 +02:00
onAmountChange = ({ target }) => {
this.setState({ amount: target.value })
}
2018-10-09 23:48:25 +02:00
render() {
2018-10-13 23:35:21 +02:00
const hasCorrectNetwork = this.state.networkId === '1'
const hasAccount = this.state.accounts.length !== 0
2018-10-11 20:06:02 +02:00
return (
<div className={styles.web3}>
2018-10-13 18:13:36 +02:00
<header>
<h4>web3</h4>
<p>Send Ether with MetaMask, Brave, or Mist.</p>
</header>
2018-10-11 20:06:02 +02:00
2018-10-13 23:35:21 +02:00
{this.state.web3Connected && (
2018-10-13 18:13:36 +02:00
<div className={styles.web3Row}>
2018-10-11 20:06:02 +02:00
{this.state.loading ? (
'Hang on...'
) : (
2018-10-13 18:13:36 +02:00
<InputGroup
2018-10-13 23:35:21 +02:00
hasCorrectNetwork={hasCorrectNetwork}
hasAccount={hasAccount}
2018-10-13 18:13:36 +02:00
amount={this.state.amount}
onAmountChange={this.onAmountChange}
2018-10-13 23:35:21 +02:00
handleButton={this.handleButton}
2018-10-13 18:13:36 +02:00
/>
2018-10-11 20:06:02 +02:00
)}
</div>
)}
2018-10-13 23:35:21 +02:00
<Alerts
hasCorrectNetwork={hasCorrectNetwork}
hasAccount={hasAccount}
networkName={this.state.networkName}
error={this.state.error}
transactionHash={this.state.transactionHash}
web3Connected={this.state.web3Connected}
/>
2018-10-11 20:06:02 +02:00
</div>
)
2018-10-09 23:48:25 +02:00
}
}