Created
April 7, 2018 22:40
-
-
Save bentooth/1649668a8a577fb25e132ff2f22dc826 to your computer and use it in GitHub Desktop.
web3 stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import styled from 'styled-components'; | |
//import web3 from 'web3'; | |
const StyledHome = styled.div` | |
color: blue; | |
`; | |
class Home extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
response: '', | |
provider: {}, | |
address: undefined | |
}; | |
this.getWeb3 = this.getWeb3.bind(this) | |
} | |
callApi = async () => { | |
try { | |
let response = await fetch('/api/hello'); | |
let body = await response.json(); | |
return body; | |
} catch (error) { | |
return error; | |
} | |
}; | |
getWeb3Object = async () => { | |
try { | |
let web3 = await this.getWeb3(); | |
return web3; | |
} catch (error) { | |
return error; | |
} | |
} | |
componentWillMount() { | |
Promise.all([this.callApi(), this.getWeb3Object()]) | |
.then(res => this.setState({ | |
response: res[0].express, | |
provider: res[1].currentProvider, | |
address: res[1].currentProvider.publicConfigStore._state.selectedAddress | |
})) | |
.catch(err => console.log(err)); | |
} | |
getWeb3() { | |
return new Promise( | |
function (resolve, reject) { | |
try { | |
let WEB3 = window.web3; | |
resolve(WEB3); | |
} catch (error) { | |
let err = { error: error, detail: 'No web3 object found' } | |
reject(err); | |
} | |
} | |
) | |
} | |
render() { | |
if (this.state.address) | |
return ( | |
<StyledHome> | |
<div>Hi from {this.state.response}</div> | |
<div>Welcome!: {this.state.address}</div> | |
</StyledHome> | |
) | |
return ( | |
<div>loading...</div> | |
) | |
} | |
} | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment