1
0
mirror of https://github.com/kremalicious/blog.git synced 2025-01-05 03:15:07 +01:00

switch to Web3 1.0 beta

This commit is contained in:
Matthias Kretschmann 2018-10-22 21:07:53 +02:00
parent 9ebf183567
commit 03c1a39f93
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 41 additions and 43 deletions

View File

@ -73,7 +73,7 @@
"react-time": "^4.3.0", "react-time": "^4.3.0",
"react-transition-group": "^2.5.0", "react-transition-group": "^2.5.0",
"slugify": "^1.3.1", "slugify": "^1.3.1",
"web3": "^0.20.7" "web3": "^1.0.0-beta.36"
}, },
"devDependencies": { "devDependencies": {
"@babel/node": "^7.0.0", "@babel/node": "^7.0.0",

View File

@ -13,6 +13,8 @@ export default class Alerts extends PureComponent {
networkName: PropTypes.string, networkName: PropTypes.string,
error: PropTypes.object, error: PropTypes.object,
transactionHash: PropTypes.string, transactionHash: PropTypes.string,
confirmationNumber: PropTypes.number,
receipt: PropTypes.object,
web3Connected: PropTypes.bool.isRequired web3Connected: PropTypes.bool.isRequired
} }
@ -22,10 +24,7 @@ export default class Alerts extends PureComponent {
noCorrectNetwork: `Please connect to <strong>Main</strong> network. You are on <strong>${networkName}</strong> right now.`, noCorrectNetwork: `Please connect to <strong>Main</strong> network. You are on <strong>${networkName}</strong> right now.`,
noWeb3: noWeb3:
'No Web3 detected. Install <a href="https://metamask.io">MetaMask</a>, <a href="https://brave.com">Brave</a>, or <a href="https://github.com/ethereum/mist">Mist</a>.', 'No Web3 detected. Install <a href="https://metamask.io">MetaMask</a>, <a href="https://brave.com">Brave</a>, or <a href="https://github.com/ethereum/mist">Mist</a>.',
success: `You are awesome, thanks!<br /> transaction: `<a href="https://etherscan.io/tx/${transactionHash}" target="_blank">See your transaction on etherscan.io.</a>`
<a href="https://etherscan.io/tx/${transactionHash}">
See your transaction on etherscan.io.
</a>`
}) })
render() { render() {
@ -56,8 +55,7 @@ export default class Alerts extends PureComponent {
{transactionHash && ( {transactionHash && (
<Message <Message
className={styles.success} message={this.alertMessages(null, transactionHash).transaction}
message={this.alertMessages(transactionHash).success}
/> />
)} )}
</Fragment> </Fragment>

View File

@ -18,8 +18,10 @@ export default class Web3Donation extends PureComponent {
selectedAccount: null, selectedAccount: null,
amount: '0.01', amount: '0.01',
transactionHash: null, transactionHash: null,
receipt: null,
loading: false, loading: false,
error: null error: null,
message: 'Hang on...'
} }
static propTypes = { static propTypes = {
@ -57,7 +59,6 @@ export default class Web3Donation extends PureComponent {
} }
// Legacy dapp browsers... // Legacy dapp browsers...
else if (window.web3) { else if (window.web3) {
// 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 })
@ -99,11 +100,10 @@ export default class Web3Donation extends PureComponent {
web3 && web3 &&
web3.eth && web3.eth &&
//web3.eth.net.getId((err, netId) => { web3.eth.net.getId((err, netId) => {
web3.version.getNetwork((err, netId) => {
if (err) this.setState({ error: err }) if (err) this.setState({ error: err })
if (netId != this.state.networkId) { if (netId !== this.state.networkId) {
this.setState({ this.setState({
error: null, error: null,
networkId: netId networkId: netId
@ -135,33 +135,27 @@ export default class Web3Donation extends PureComponent {
handleButton = () => { handleButton = () => {
const { web3 } = this const { web3 } = this
this.setState({ loading: true }) this.setState({
loading: true,
message: 'Waiting for your confirmation...'
})
// web3.eth web3.eth
// .sendTransaction({ .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(
{
from: this.state.selectedAccount, from: this.state.selectedAccount,
to: this.props.address, to: this.props.address,
value: this.state.amount * 1e18 // ETH -> Wei value: this.state.amount * 1e18 // ETH -> Wei
}, })
(error, transactionHash) => { .once('transactionHash', transactionHash => {
if (error) this.setState({ error, loading: false }) this.setState({
if (!transactionHash) this.setState({ loading: true }) transactionHash,
this.setState({ transactionHash, loading: false }) message: 'Waiting for network confirmation, hang on...'
} })
) })
.on('error', error => this.setState({ error, loading: false }))
.then(() => {
this.setState({ message: 'Confirmed. You are awesome, thanks!' })
})
} }
onAmountChange = ({ target }) => { onAmountChange = ({ target }) => {
@ -178,10 +172,12 @@ export default class Web3Donation extends PureComponent {
amount, amount,
networkName, networkName,
error, error,
transactionHash transactionHash,
confirmationNumber,
message
} = this.state } = this.state
const hasCorrectNetwork = networkId === '1' const hasCorrectNetwork = networkId === 42
const hasAccount = accounts.length !== 0 const hasAccount = accounts.length !== 0
return ( return (
@ -194,7 +190,7 @@ export default class Web3Donation extends PureComponent {
{web3Connected && ( {web3Connected && (
<div className={styles.web3Row}> <div className={styles.web3Row}>
{loading ? ( {loading ? (
'Hang on...' message
) : ( ) : (
<InputGroup <InputGroup
hasCorrectNetwork={hasCorrectNetwork} hasCorrectNetwork={hasCorrectNetwork}
@ -215,6 +211,7 @@ export default class Web3Donation extends PureComponent {
error={error} error={error}
transactionHash={transactionHash} transactionHash={transactionHash}
web3Connected={web3Connected} web3Connected={web3Connected}
confirmationNumber={confirmationNumber}
/> />
</div> </div>
) )

View File

@ -19,4 +19,7 @@
.web3Row { .web3Row {
min-height: 58px; min-height: 58px;
display: flex;
align-items: center;
justify-content: center;
} }

View File

@ -2,19 +2,19 @@ export const getNetworkName = async netId => {
let networkName let networkName
switch (netId) { switch (netId) {
case '1': case 1:
networkName = 'Main' networkName = 'Main'
break break
case '2': case 2:
networkName = 'Morden' networkName = 'Morden'
break break
case '3': case 3:
networkName = 'Ropsten' networkName = 'Ropsten'
break break
case '4': case 4:
networkName = 'Rinkeby' networkName = 'Rinkeby'
break break
case '42': case 42:
networkName = 'Kovan' networkName = 'Kovan'
break break
default: default: