aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/util_test.js
blob: 5f28dbb25a313bd344c26a0a265b00cbd36e8c4e (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var assert = require('assert')
var sinon = require('sinon')
const ethUtil = require('ethereumjs-util')

var path = require('path')
var util = require(path.join(__dirname, '..', '..', 'ui', 'app', 'util.js'))

describe('util', function() {
  var ethInWei = '1'
  for (var i = 0; i < 18; i++ ) { ethInWei += '0' }

  beforeEach(function() {
    this.sinon = sinon.sandbox.create()
  })

  afterEach(function() {
    this.sinon.restore()
  })

  describe('addressSummary', function() {
    it('should add case-sensitive checksum', function() {
      var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
      var result = util.addressSummary(address)
      assert.equal(result, '0xFDEa65C8...b825')
    })
  })

  describe('isValidAddress', function() {
    it('should allow 40-char non-prefixed hex', function() {
      var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825'
      var result = util.isValidAddress(address)
      assert.ok(result)
    })

    it('should allow 42-char non-prefixed hex', function() {
      var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
      var result = util.isValidAddress(address)
      assert.ok(result)
    })

    it('should not allow less non hex-prefixed', function() {
      var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85'
      var result = util.isValidAddress(address)
      assert.ok(!result)
    })

    it('should not allow less hex-prefixed', function() {
      var address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85'
      var result = util.isValidAddress(address)
      assert.ok(!result)
    })

    it('should recognize correct capitalized checksum', function() {
      var address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825'
      var result = util.isValidAddress(address)
      assert.ok(result)
    })

    it('should recognize incorrect capitalized checksum', function() {
      var address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825'
      var result = util.isValidAddress(address)
      assert.ok(!result)
    })

  })

  describe('numericBalance', function() {

    it('should return a BN 0 if given nothing', function() {
      var result = util.numericBalance()
      assert.equal(result.toString(10), 0)
    })

    it('should work with hex prefix', function() {
      var result = util.numericBalance('0x012')
      assert.equal(result.toString(10), '18')
    })

    it('should work with no hex prefix', function() {
      var result = util.numericBalance('012')
      assert.equal(result.toString(10), '18')
    })

  })

  describe('#ethToWei', function() {

    it('should take an eth BN, returns wei BN', function() {
      var input = new ethUtil.BN(1, 10)
      var result = util.ethToWei(input)
      assert.equal(result, ethInWei, '18 zeroes')
    })

  })

  describe('#weiToEth', function() {

    it('should take a wei BN and return an eth BN', function() {
    var result = util.weiToEth(new ethUtil.BN(ethInWei))
    assert.equal(result, '1', 'equals 1 eth')
    })

  })

  describe('#formatBalance', function() {

    it('when given nothing', function() {
      var result = util.formatBalance()
      assert.equal(result, 'None', 'should return "None"')
    })

    it('should return eth as string followed by ETH', function() {
      var input = new ethUtil.BN(ethInWei, 10).toJSON()
      var result = util.formatBalance(input)
      assert.equal(result, '1.0000 ETH')
    })

    it('should return eth as string followed by ETH', function() {
      var input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON()
      var result = util.formatBalance(input)
      assert.equal(result, '0.5000 ETH')
    })

    it('should display four decimal points', function() {
      var input = "0x128dfa6a90b28000"
      var result = util.formatBalance(input)
      assert.equal(result, '1.3370 ETH')
    })

  })

  describe('normalizing values', function() {

    describe('#normalizeToWei', function() {
      it('should convert an eth to the appropriate equivalent values', function() {
        var valueTable = {
          wei:   '1000000000000000000',
          kwei:  '1000000000000000',
          mwei:  '1000000000000',
          gwei:  '1000000000',
          szabo: '1000000',
          finney:'1000',
          ether: '1',
          // kether:'0.001',
          // mether:'0.000001',
          // AUDIT: We're getting BN numbers on these ones.
          // I think they're big enough to ignore for now.
          // gether:'0.000000001',
          // tether:'0.000000000001',
        }
        var oneEthBn = new ethUtil.BN(ethInWei, 10)

        for(var currency in valueTable) {

          var value = new ethUtil.BN(valueTable[currency], 10)
          var output = util.normalizeToWei(value, currency)
          assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`)
        }
      })
    })

    describe('#normalizeNumberToWei', function() {

      it('should handle a simple use case', function() {
        var input = 0.0002
        var output = util.normalizeNumberToWei(input, 'ether')
        var str = output.toString(10)
        assert.equal(str, '200000000000000')
      })

      it('should convert a kwei number to the appropriate equivalent wei', function() {
        var result = util.normalizeNumberToWei(1.111, 'kwei')
        assert.equal(result.toString(10), '1111', 'accepts decimals')
      })

      it('should convert a ether number to the appropriate equivalent wei', function() {
        var result = util.normalizeNumberToWei(1.111, 'ether')
        assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals')
      })
    })
  })
})