-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
147 lines (124 loc) · 3.63 KB
/
app.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
const h = require('h')
var state = {
isLoading: true,
faucetAddress: '0x18a3462427bcc9133bb46e88bcbe39cd7ef0e761',
faucetBalance: null,
userAddress: null,
fromBalance: null,
txSendResult: null,
}
// check environment
if (typeof web3 === 'undefined') {
return render(h('span', 'No web3 detected.'))
}
startApp()
function startApp(){
renderApp()
setInterval(updateState, 4000)
}
function updateState(){
getAccounts()
getBalances()
}
function getAccounts(){
web3.eth.getAccounts(function(err, accounts){
if (err) return console.error(err)
var address = accounts[0]
if (state.userAddress === address) return
state.userAddress = address
state.fromBalance = null
getBalances()
renderApp()
})
}
function getBalances(){
if (state.faucetAddress) web3.eth.getBalance(state.faucetAddress, function(err, result){
if (err) return console.error(err)
state.faucetBalance = result.toNumber()/1e18
renderApp()
})
if (state.userAddress) web3.eth.getBalance(state.userAddress, function(err, result){
if (err) return console.error(err)
state.fromBalance = result.toNumber()/1e18
renderApp()
})
}
function renderApp(){
// if (state.isLoading) {
// return render(h('span', 'web3 detected - Loading...'))
// }
render([
h('nav.navbar.navbar-default', [
h('h1.container-fluid', 'MetaMask Ether Faucet')
]),
h('section.container', [
h('div.panel.panel-default', [
h('div.panel-heading', [
h('h3', 'faucet'),
]),
h('div.panel-body', [
h('div', 'address: '+state.faucetAddress),
h('div', 'balance: '+formatBalance(state.faucetBalance)),
h('button.btn.btn-default', 'request 1 ether from faucet', {
// disabled: state.userAddress ? null : true,
click: getEther,
}),
]),
]),
h('div.panel.panel-default', [
h('div.panel-heading', [
h('h3', 'user'),
]),
h('div.panel-body', [
h('div', 'address: '+state.userAddress),
h('div', 'balance: '+formatBalance(state.fromBalance)),
h('div', 'donate to faucet:'),
h('button.btn.btn-default', '1 ether', {
// disabled: state.userAddress ? null : true,
click: sendTx.bind(null, 1),
}),
h('button.btn.btn-default', '10 ether', {
// disabled: state.userAddress ? null : true,
click: sendTx.bind(null, 10),
}),
h('button.btn.btn-default', '100 ether', {
// disabled: state.userAddress ? null : true,
click: sendTx.bind(null, 100),
}),
state.txSendResult ? h('div', state.txSendResult) : null,
]),
]),
]),
])
}
function getEther(){
if (!state.userAddress) return alert('no user accounts found')
var uri = window.location.href
var http = new XMLHttpRequest()
var data = state.userAddress
http.open('POST', uri, true)
http.setRequestHeader('Content-type', 'application/rawdata')
http.send(data)
}
function sendTx(value){
if (!state.userAddress) return alert('no user accounts found')
web3.eth.sendTransaction({
from: state.userAddress,
to: state.faucetAddress,
value: '0x'+(value*1e18).toString(16),
}, function(err, txHash){
state.txSendResult = (err && err.stack) || txHash
})
}
function render(elements){
if (!Array.isArray(elements)) elements = [elements]
// clear
document.body.innerHTML = ''
// insert
elements.forEach(function(element){
document.body.appendChild(element)
})
}
function formatBalance(balance){
return balance ? balance+' ether' : '...'
}