1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-14 09:05:17 +01:00

basic transaction interaction

This commit is contained in:
Matthias Kretschmann 2018-10-08 22:19:52 +02:00
parent a5b38cdadc
commit bde652533e
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 126 additions and 40 deletions

View File

@ -71,7 +71,8 @@
"react-qr-svg": "^2.1.0", "react-qr-svg": "^2.1.0",
"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": "^1.0.0-beta.36"
}, },
"devDependencies": { "devDependencies": {
"@babel/node": "^7.0.0", "@babel/node": "^7.0.0",

View File

@ -1,7 +1,8 @@
import React from 'react' import React, { PureComponent } from 'react'
import { StaticQuery, graphql } from 'gatsby' import { StaticQuery, graphql } from 'gatsby'
import { QRCode } from 'react-qr-svg' import { QRCode } from 'react-qr-svg'
import Clipboard from 'react-clipboard.js' import Clipboard from 'react-clipboard.js'
import Web3 from 'web3'
import Modal from '../atoms/Modal' import Modal from '../atoms/Modal'
import { ReactComponent as IconClipboard } from '../../images/clipboard.svg' import { ReactComponent as IconClipboard } from '../../images/clipboard.svg'
@ -20,45 +21,124 @@ const query = graphql`
} }
` `
const ModalThanks = ({ ...props }) => ( class ModalThanks extends PureComponent {
<StaticQuery state = {
query={query} minimal: false,
render={data => { web3Connected: false,
const { author } = data.site.siteMetadata balance: '',
network: '',
accounts: [],
receipt: ''
}
return ( web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546')
<Modal
{...props} componentDidMount() {
contentLabel="Say thanks with Bitcoin or Ether" this.getWeb3Account()
title="Say thanks" }
>
<div className={styles.modalThanks}> getWeb3Account() {
{Object.keys(author).map((address, i) => ( if (this.web3 && this.web3.eth.net.isListening()) {
<div key={i} className={styles.coin}> this.setState({ web3Connected: true })
<h4>{address}</h4>
<QRCode this.web3.eth.net.getId((err, netId) => {
bgColor="transparent" switch (netId) {
fgColor="#6b7f88" case '1':
level="Q" this.setState({ network: 'Main' })
style={{ width: 150 }} break
value={author[address]} case '2':
/> this.setState({ network: 'Morden' })
<pre> break
<code>{author[address]}</code> case '3':
<Clipboard this.setState({ network: 'Ropsten' })
data-clipboard-text={author[address]} break
button-title="Copy to clipboard" case '4':
> this.setState({ network: 'Rinkeby' })
<IconClipboard /> break
</Clipboard> case '42':
</pre> 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 (
<StaticQuery
query={query}
render={data => {
const { author } = data.site.siteMetadata
return (
<Modal
{...this.props}
contentLabel="Say thanks with Bitcoin or Ether"
title="Say thanks"
>
<div className={styles.modalThanks}>
{this.state.web3Connected && (
<div>
<button
className="btn btn-primary"
onClick={this.handleWeb3Button}
>
Make it rain
</button>
{this.state.receipt.status && (
<div>{this.state.receipt.transactionHash}</div>
)}
</div>
)}
{Object.keys(author).map((address, i) => (
<div key={i} className={styles.coin}>
<h4>{address}</h4>
<QRCode
bgColor="transparent"
fgColor="#6b7f88"
level="Q"
style={{ width: 150 }}
value={author[address]}
/>
<pre>
<code>{author[address]}</code>
<Clipboard
data-clipboard-text={author[address]}
button-title="Copy to clipboard"
>
<IconClipboard />
</Clipboard>
</pre>
</div>
))}
</div> </div>
))} </Modal>
</div> )
</Modal> }}
) />
}} )
/> }
) }
export default ModalThanks export default ModalThanks

View File

@ -4,6 +4,11 @@
@media (min-width: $screen-sm) { @media (min-width: $screen-sm) {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
flex-wrap: wrap;
> div:first-child {
width: 100%;
}
} }
} }