aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/pending-balance-test.js
blob: 7f20270cb6f7f0c8ca72813f05708eab37f3fcf2 (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
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
const assert = require('assert')
const PendingBalanceCalculator = require('../../app/scripts/lib/pending-balance-calculator')
const MockTxGen = require('../lib/mock-tx-gen')
const BN = require('ethereumjs-util').BN
let providerResultStub = {}

const etherBn = new BN(String(1e18))
const ether = '0x' + etherBn.toString(16)

describe('PendingBalanceCalculator', function () {
  let balanceCalculator

  describe('#valueFor(tx)', function () {
    it('returns a BN for a given tx value', function () {
      const txGen = new MockTxGen()
      pendingTxs = txGen.generate({
        status: 'submitted',
        txParams: {
          value: ether,
          gasPrice: '0x0',
          gas: '0x0',
        }
      }, { count: 1 })

      const balanceCalculator = generateBalaneCalcWith([], '0x0')
      const result = balanceCalculator.valueFor(pendingTxs[0])
      assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
    })
  })

  describe('if you have no pending txs and one ether', function () {

    beforeEach(function () {
      balanceCalculator = generateBalaneCalcWith([], ether)
    })

    it('returns the network balance', async function () {
      const result = await balanceCalculator.getBalance()
      assert.equal(result, ether, `gave ${result} needed ${ether}`)
    })
  })

  describe('if you have a one ether pending tx and one ether', function () {
    beforeEach(function () {
      const txGen = new MockTxGen()
      pendingTxs = txGen.generate({
        status: 'submitted',
        txParams: {
          value: ether,
          gasPrice: '0x0',
          gas: '0x0',
        }
      }, { count: 1 })

      balanceCalculator = generateBalaneCalcWith(pendingTxs, ether)
    })

    it('returns the network balance', async function () {
      console.log('one')
      console.dir(balanceCalculator)
      console.dir(balanceCalculator.getBalance.toString())
      const result = await balanceCalculator.getBalance()
      console.log('two')
      console.dir(result)
      assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
      return true
    })

  })
})

function generateBalaneCalcWith (transactions, providerStub = '0x0') {
  const getPendingTransactions = () => Promise.resolve(transactions)
  const getBalance = () => Promise.resolve(providerStub)
  providerResultStub.result = providerStub
  const provider = {
    sendAsync: (_, cb) => { cb(undefined, providerResultStub) },
    _blockTracker: {
      getCurrentBlock: () => '0x11b568',
    },
  }
  return new PendingBalanceCalculator({
    getBalance,
    getPendingTransactions,
  })
}