From bde652533e59ae5db71198290025852899687dbe Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 8 Oct 2018 22:19:52 +0200 Subject: [PATCH] basic transaction interaction --- package.json | 3 +- src/components/molecules/ModalThanks.jsx | 158 +++++++++++++----- .../molecules/ModalThanks.module.scss | 5 + 3 files changed, 126 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 531d3431..8c0448e6 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,8 @@ "react-qr-svg": "^2.1.0", "react-time": "^4.3.0", "react-transition-group": "^2.5.0", - "slugify": "^1.3.1" + "slugify": "^1.3.1", + "web3": "^1.0.0-beta.36" }, "devDependencies": { "@babel/node": "^7.0.0", diff --git a/src/components/molecules/ModalThanks.jsx b/src/components/molecules/ModalThanks.jsx index 6aa723bd..757411f3 100644 --- a/src/components/molecules/ModalThanks.jsx +++ b/src/components/molecules/ModalThanks.jsx @@ -1,7 +1,8 @@ -import React from 'react' +import React, { PureComponent } from 'react' import { StaticQuery, graphql } from 'gatsby' import { QRCode } from 'react-qr-svg' import Clipboard from 'react-clipboard.js' +import Web3 from 'web3' import Modal from '../atoms/Modal' import { ReactComponent as IconClipboard } from '../../images/clipboard.svg' @@ -20,45 +21,124 @@ const query = graphql` } ` -const ModalThanks = ({ ...props }) => ( - { - const { author } = data.site.siteMetadata +class ModalThanks extends PureComponent { + state = { + minimal: false, + web3Connected: false, + balance: '', + network: '', + accounts: [], + receipt: '' + } - return ( - -
- {Object.keys(author).map((address, i) => ( -
-

{address}

- -
-                  {author[address]}
-                  
-                    
-                  
-                
+ web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546') + + componentDidMount() { + this.getWeb3Account() + } + + getWeb3Account() { + if (this.web3 && this.web3.eth.net.isListening()) { + this.setState({ web3Connected: true }) + + this.web3.eth.net.getId((err, netId) => { + switch (netId) { + case '1': + this.setState({ network: 'Main' }) + break + case '2': + this.setState({ network: 'Morden' }) + break + case '3': + this.setState({ network: 'Ropsten' }) + break + case '4': + this.setState({ network: 'Rinkeby' }) + break + case '42': + this.setState({ network: 'Kovan' }) + break + default: + this.setState({ network: 'unknown' }) + } + }) + + this.web3.eth.getAccounts((error, accounts) => { + this.setState({ accounts }) + this.web3.eth.getBalance(accounts[0]).then(balance => { + this.setState({ balance }) + }) + }) + } + } + + handleWeb3Button = () => { + this.web3.eth + .sendTransaction({ + from: this.state.accounts[0], + to: '0x339dbC44d39bf1961E385ed0Ae88FC6069b87Ea1', + value: '1000000000000000' + }) + .then(receipt => this.setState({ receipt })) + .catch(err => console.error(err)) + } + + render() { + return ( + { + const { author } = data.site.siteMetadata + + return ( + +
+ {this.state.web3Connected && ( +
+ + {this.state.receipt.status && ( +
{this.state.receipt.transactionHash}
+ )} +
+ )} + + {Object.keys(author).map((address, i) => ( +
+

{address}

+ +
+                      {author[address]}
+                      
+                        
+                      
+                    
+
+ ))}
- ))} -
- - ) - }} - /> -) + + ) + }} + /> + ) + } +} export default ModalThanks diff --git a/src/components/molecules/ModalThanks.module.scss b/src/components/molecules/ModalThanks.module.scss index 02155fd3..61d62f95 100644 --- a/src/components/molecules/ModalThanks.module.scss +++ b/src/components/molecules/ModalThanks.module.scss @@ -4,6 +4,11 @@ @media (min-width: $screen-sm) { display: flex; justify-content: space-between; + flex-wrap: wrap; + + > div:first-child { + width: 100%; + } } }