1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-25 18:56:25 +02:00
blog/src/components/molecules/Web3Donation/index.tsx

211 lines
4.5 KiB
TypeScript
Raw Normal View History

2018-10-09 23:48:25 +02:00
import React, { PureComponent } from 'react'
2019-10-11 23:50:03 +02:00
import Web3 from 'web3'
2018-10-13 20:37:13 +02:00
import InputGroup from './InputGroup'
2018-10-30 18:08:07 +01:00
import Alerts, { alertMessages } from './Alerts'
2018-10-13 20:37:13 +02:00
import styles from './index.module.scss'
2018-10-26 23:47:41 +02:00
import { getWeb3, getAccounts, getNetwork } from './utils'
2018-10-09 23:48:25 +02:00
const ONE_SECOND = 1000
const ONE_MINUTE = ONE_SECOND * 60
2018-10-30 18:08:07 +01:00
const correctNetwork = 1
2018-10-09 23:48:25 +02:00
2019-10-11 23:50:03 +02:00
interface Web3DonationState {
netId: number
networkName: string
accounts: string[]
selectedAccount: string
amount: number
transactionHash: string
receipt: string
message: {
status?: string
text?: string
2018-10-09 23:48:25 +02:00
}
2019-10-11 23:50:03 +02:00
inTransaction: boolean
}
2018-10-09 23:48:25 +02:00
2019-10-11 23:50:03 +02:00
export default class Web3Donation extends PureComponent<
{ address: string },
Web3DonationState
> {
state = {
netId: 0,
networkName: '',
accounts: [''],
selectedAccount: '',
amount: 0.01,
transactionHash: '',
receipt: '',
message: {},
inTransaction: false
2018-10-09 23:48:25 +02:00
}
2019-10-11 23:50:03 +02:00
web3: Web3 = null
interval: any = null
networkInterval: any = null
2018-10-09 23:48:25 +02:00
componentDidMount() {
2018-10-22 19:24:53 +02:00
this.initWeb3()
2018-10-13 20:13:50 +02:00
}
componentWillUnmount() {
this.resetAllTheThings()
}
initWeb3 = async () => {
2018-10-30 18:08:07 +01:00
this.setState({ message: { text: 'Checking' } })
2018-10-26 23:47:41 +02:00
try {
this.web3 = await getWeb3()
2018-10-09 23:48:25 +02:00
2018-10-30 18:08:07 +01:00
this.web3
? this.initAllTheTings()
: this.setState({
2019-10-11 23:50:03 +02:00
message: {
status: 'error',
text: alertMessages().noWeb3
}
2018-10-30 18:08:07 +01:00
})
2018-10-26 23:47:41 +02:00
} catch (error) {
2019-10-11 23:50:03 +02:00
this.setState({
message: { status: 'error', text: error }
})
2018-10-13 20:13:50 +02:00
}
2018-10-09 23:48:25 +02:00
}
async initAllTheTings() {
2018-10-30 18:08:07 +01:00
this.fetchAccounts()
this.fetchNetwork()
2018-10-22 19:24:53 +02:00
this.initAccountsPoll()
this.initNetworkPoll()
}
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 20:13:50 +02:00
initAccountsPoll() {
2018-10-09 23:48:25 +02:00
if (!this.interval) {
this.interval = setInterval(this.fetchAccounts, ONE_SECOND * 10)
2018-10-09 23:48:25 +02:00
}
}
initNetworkPoll() {
if (!this.networkInterval) {
this.networkInterval = setInterval(this.fetchNetwork, ONE_MINUTE)
}
}
2018-10-26 23:47:41 +02:00
fetchNetwork = async () => {
2018-10-09 23:48:25 +02:00
const { web3 } = this
2018-10-30 18:08:07 +01:00
const { netId, networkName } = await getNetwork(web3)
2018-10-09 23:48:25 +02:00
2018-10-30 18:08:07 +01:00
if (netId === correctNetwork) {
this.setState({ netId, networkName })
} else {
2018-10-26 23:47:41 +02:00
this.setState({
2018-10-30 18:08:07 +01:00
message: {
status: 'error',
text: alertMessages(networkName).noCorrectNetwork
}
2018-10-09 23:48:25 +02:00
})
2018-10-26 23:47:41 +02:00
}
2018-10-09 23:48:25 +02:00
}
2018-10-26 23:47:41 +02:00
fetchAccounts = async () => {
2018-10-09 23:48:25 +02:00
const { web3 } = this
2018-10-30 18:08:07 +01:00
const accounts = await getAccounts(web3)
2018-10-09 23:48:25 +02:00
2018-10-30 18:08:07 +01:00
if (accounts[0]) {
2018-10-26 23:47:41 +02:00
this.setState({
accounts,
2018-10-30 18:08:07 +01:00
selectedAccount: accounts[0].toLowerCase()
})
} else {
this.setState({
2019-10-11 23:50:03 +02:00
message: {
status: 'error',
text: alertMessages().noAccount
}
2018-10-09 23:48:25 +02:00
})
2018-10-26 23:47:41 +02:00
}
2018-10-09 23:48:25 +02:00
}
2018-10-30 18:08:07 +01:00
sendTransaction = () => {
2018-10-09 23:48:25 +02:00
const { web3 } = this
2018-10-30 18:08:07 +01:00
this.setState({
inTransaction: true,
message: { text: alertMessages().waitingForUser }
})
2018-10-22 21:07:53 +02:00
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-22 21:07:53 +02:00
})
.once('transactionHash', transactionHash => {
this.setState({
transactionHash,
2018-10-30 18:08:07 +01:00
message: { text: alertMessages().waitingConfirmation }
2018-10-22 21:07:53 +02:00
})
})
2018-10-30 18:08:07 +01:00
.on('error', error =>
2019-10-11 23:50:03 +02:00
this.setState({
message: { status: 'error', text: error.message }
})
2018-10-30 18:08:07 +01:00
)
2018-10-22 21:07:53 +02:00
.then(() => {
2018-10-28 11:16:57 +01:00
this.setState({
2019-10-11 23:50:03 +02:00
message: {
status: 'success',
text: alertMessages().success
}
2018-10-28 11:16:57 +01:00
})
2018-10-22 21:07:53 +02:00
})
2018-10-09 23:48:25 +02:00
}
2019-10-11 23:50:03 +02:00
onAmountChange = ({ target }: { target: any }) => {
2018-10-13 15:40:05 +02:00
this.setState({ amount: target.value })
}
2018-10-09 23:48:25 +02:00
render() {
2018-10-14 21:48:38 +02:00
const {
selectedAccount,
amount,
2018-10-22 21:07:53 +02:00
transactionHash,
2018-10-28 11:16:57 +01:00
message,
2018-10-30 18:08:07 +01:00
inTransaction
2018-10-14 21:48:38 +02:00
} = this.state
2018-10-22 19:24:53 +02:00
2018-10-11 20:06:02 +02:00
return (
<div className={styles.web3}>
2018-10-13 18:13:36 +02:00
<header>
<h4>Web3 Wallet</h4>
<p>Send Ether with MetaMask or Brave.</p>
2018-10-13 18:13:36 +02:00
</header>
2018-10-11 20:06:02 +02:00
<div className={styles.web3Row}>
2018-10-30 18:08:07 +01:00
{selectedAccount &&
this.state.netId === correctNetwork &&
!inTransaction ? (
<InputGroup
selectedAccount={selectedAccount}
amount={amount}
onAmountChange={this.onAmountChange}
sendTransaction={this.sendTransaction}
/>
) : (
2018-10-30 18:08:07 +01:00
message && (
<Alerts message={message} transactionHash={transactionHash} />
)
)}
</div>
2018-10-11 20:06:02 +02:00
</div>
)
2018-10-09 23:48:25 +02:00
}
}