aboutsummaryrefslogtreecommitdiffstats
path: root/library/example/index.js
blob: 329302a4da41b91074ef653e161b2e2fba118e46 (plain) (blame)
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
window.addEventListener('load', web3Detect)

function web3Detect() {
  if (global.web3) {
    logToDom('web3 detected!')
    startApp()
  } else {
    logToDom('no web3 detected!')
  }
}

function startApp(){
  console.log('app started')

  var primaryAccount = null
  console.log('getting main account...')
  web3.eth.getAccounts(function(err, addresses){
    if (err) throw err
    console.log('set address', addresses[0])
    debugger
    primaryAccount = addresses[0]
  })

  document.querySelector('.action-button-1').addEventListener('click', function(){
    console.log('saw click')
    console.log('sending tx')
    web3.eth.sendTransaction({
      from: primaryAccount,
      to: primaryAccount,
      value: 0,
    }, function(err, txHash){
      if (err) throw err
      console.log('sendTransaction result:', err || txHash)
    })
  })
  document.querySelector('.action-button-2').addEventListener('click', function(){
    console.log('saw click')
    setTimeout(function(){
      console.log('sending tx')
      web3.eth.sendTransaction({
        from: primaryAccount,
        to: primaryAccount,
        value: 0,
      }, function(err, txHash){
        if (err) throw err
        console.log('sendTransaction result:', err || txHash)
      })
    })
  })

}

function logToDom(message){
  document.body.appendChild(document.createTextNode(message))
  console.log(message)
}