-
Notifications
You must be signed in to change notification settings - Fork 2
/
home.js
181 lines (140 loc) · 4.53 KB
/
home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import '../App.css'
import '../css/home.css'
import Web3 from 'web3'
import { SYNAPSE_ABI, SYNAPSE_ADDRESS } from '../config'
import { PROFILE_ABI, PROFILE_ADDRESS } from '../config'
import { BIO_ABI, BIO_ADDRESS } from '../config'
import { USER_ABI, USER_ADDRESS } from '../config'
import Thought from '../createThought.js'
import Feed from '../feed.js'
function isInstalled() {
if (typeof Web3 !== 'undefined'){
console.log('MetaMask is installed')
}
else{
console.log('MetaMask is not installed')
}
}
function isLocked(web3) {
web3.eth.getAccounts(function(err, accounts){
if (err != null) {
console.log(err)
}
else if (accounts.length === 0) {
console.log('MetaMask is locked')
}
else {
console.log('MetaMask is unlocked')
}
});
}
class Home extends Component {
async componentWillMount() {
await this.loadWeb3()
await this.loadBlockchainData()
await this.getBioText()
await this.getHandle()
this.forceUpdate()
}
componentDidMount(){
document.title = "My Synapse Feed"
}
async loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum)
await window.ethereum.enable()
}
else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider)
}
else {
window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!')
}
}
async componentWillUnmount() {
clearInterval(this.interval);
}
async loadBlockchainData() {
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545")
isInstalled();
isLocked(web3);
const synapse = new web3.eth.Contract(SYNAPSE_ABI, SYNAPSE_ADDRESS)
const profile = new web3.eth.Contract(PROFILE_ABI, PROFILE_ADDRESS)
const bio = new web3.eth.Contract(BIO_ABI, BIO_ADDRESS)
this.setState({ profile })
this.setState({ bio })
const accounts = await web3.eth.getAccounts()
const balanceWei = await web3.eth.getBalance(accounts[0])
var balance = balanceWei/1000000000000000000
this.setState({ account: accounts[0], balance: balance })
this.setState({ loading: false })
}
constructor(props) {
super(props);
this.state = {
account: '',
balance: '',
myString: '',
thoughts: [],
handle: 0,
loading: true,
currentBio: "Loading..."
}
this.createThought = this.createThought.bind(this)
this.getBioText = this.getBioText.bind(this)
this.getHandle = this.getHandle.bind(this)
}
async refresh() {
this.forceUpdate()
}
createThought(string) {
//const profile = new web3.eth.Contract(PROFILE_ABI, PROFILE_ADDRESS)
this.setState({ loading: true })
this.state.profile.methods.createThought(string).send({ from: this.state.account })
.once('receipt', (receipt) => {
this.setState({ loading: false })
})
}
async getBioText() {
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545")
const bio = new web3.eth.Contract(BIO_ABI, BIO_ADDRESS)
const accounts = await web3.eth.getAccounts()
this.setState({ account: accounts[0]})
bio.methods.getBio(accounts[0]).call({ from: accounts[0]}).then(val => this.setState({ currentBio: val }));
}
async getHandle() {
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545")
const handle = new web3.eth.Contract(PROFILE_ABI, PROFILE_ADDRESS)
const accounts = await web3.eth.getAccounts()
this.setState({ account: accounts[0]})
handle.methods.getOwnHandle().call({ from: accounts[0]}).then(val => this.setState({ currentHandle: val }))
}
render() {
return (
<div className="App">
<section>
<div class="left">
<h5> Account: {this.state.account} </h5>
<h5> @ { this.state.currentHandle} </h5>
<h5>My Balance: <p> {this.state.balance}</p></h5>
<h5>My Bio: <p> {this.state.currentBio} </p> </h5>
</div>
<div class="right">
{ this.state.loading
? <div id="loader" className=""><p className="">Communicating with blockchain...</p></div>
: <Thought
thoughts={this.state.thoughts}
createThought={this.createThought}
/>
}
<span></span>
<Feed/>
</div>
</section>
</div>
);
}
}
export default Home;