Skip to content

Instantly share code, notes, and snippets.

@bentooth
Created April 7, 2018 22:40
Show Gist options
  • Save bentooth/1649668a8a577fb25e132ff2f22dc826 to your computer and use it in GitHub Desktop.
Save bentooth/1649668a8a577fb25e132ff2f22dc826 to your computer and use it in GitHub Desktop.
web3 stuff
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