aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/pending-balance-calculator.js
blob: 9df87e34b81943c83fe520bf9532bee757ad6dae (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
const BN = require('ethereumjs-util').BN
const EthQuery = require('ethjs-query')
const normalize = require('eth-sig-util').normalize

class PendingBalanceCalculator {

  constructor ({ getBalance, getPendingTransactions }) {
    this.getPendingTransactions = getPendingTransactions
    this.getBalance = getBalance
  }

  async getBalance() {
    console.log('getting balance')
    const results = await Promise.all([
      this.getBalance(),
      this.getPendingTransactions(),
    ])
    console.dir(results)

    const balance = results[0]
    const pending = results[1]

    console.dir({ balance, pending })

    const pendingValue = pending.reduce(function (total, tx) {
      return total.sub(this.valueFor(tx))
    }, new BN(0))

    const balanceBn = new BN(normalize(balance))

    return `0x${ balanceBn.sub(pendingValue).toString(16) }`
  }

  valueFor (tx) {
    const value = new BN(normalize(tx.txParams.value))
    return value
  }

}

module.exports = PendingBalanceCalculator