aboutsummaryrefslogtreecommitdiff
path: root/slides/pws/script.js
blob: 3f95d001584dffd98c8fe63a075c6e1e3101b012 (plain)
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
const web3 = new Web3(new Web3.providers.HttpProvider('http://95.85.1.235:8545'));

Reveal.initialize({
  history: true,
  backgroundTransition: 'slide'
})

function addToSlide(data, isHeader) {
  const pre = document.createElement('pre');
  if (isHeader) {
    pre.classList.add('header');
  }
  pre.textContent = data;

  document.querySelector('#blockchain').appendChild(pre);
}

function compactStr(str) {
  return str.substr(0, 10) + '...' + str.substr(str.length - 10);
}

// addToSlide('012345678901234567890123456789012345678901234567890123456789')

// Listen for incoming blocks.
const filter = web3.eth.filter('latest');
filter.watch((err, blockHash) => {
  // New block created
  if (err) {
    throw err;
  }

  const block = web3.eth.getBlock(blockHash, true);
  console.log(block);
  addToSlide(`Block ${block.number}`, true);

  block.transactions.forEach((transaction) => {
    if (transaction.from == null || transaction.to == null) {
      return;
    }

    const humanReadableValue = web3.fromWei(transaction.value, 'ether').toString();
    addToSlide(`${compactStr(transaction.from)} -> ${humanReadableValue} ETHER -> ${compactStr(transaction.to)}`);
  });
});