|
|
@ -1,13 +1,13 @@ |
|
|
|
var assert = require('assert') |
|
|
|
const assert = require('assert') |
|
|
|
var sinon = require('sinon') |
|
|
|
const sinon = require('sinon') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
|
|
|
|
|
|
|
|
var path = require('path') |
|
|
|
const path = require('path') |
|
|
|
var util = require(path.join(__dirname, '..', '..', 'ui', 'app', 'helpers', 'utils', 'util.js')) |
|
|
|
const util = require(path.join(__dirname, '..', '..', 'ui', 'app', 'helpers', 'utils', 'util.js')) |
|
|
|
|
|
|
|
|
|
|
|
describe('util', function () { |
|
|
|
describe('util', function () { |
|
|
|
var ethInWei = '1' |
|
|
|
let ethInWei = '1' |
|
|
|
for (var i = 0; i < 18; i++) { |
|
|
|
for (let i = 0; i < 18; i++) { |
|
|
|
ethInWei += '0' |
|
|
|
ethInWei += '0' |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -47,52 +47,52 @@ describe('util', function () { |
|
|
|
|
|
|
|
|
|
|
|
describe('#addressSummary', function () { |
|
|
|
describe('#addressSummary', function () { |
|
|
|
it('should add case-sensitive checksum', function () { |
|
|
|
it('should add case-sensitive checksum', function () { |
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
var result = util.addressSummary(address) |
|
|
|
const result = util.addressSummary(address) |
|
|
|
assert.equal(result, '0xFDEa65C8...b825') |
|
|
|
assert.equal(result, '0xFDEa65C8...b825') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should accept arguments for firstseg, lastseg, and keepPrefix', function () { |
|
|
|
it('should accept arguments for firstseg, lastseg, and keepPrefix', function () { |
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
var result = util.addressSummary(address, 4, 4, false) |
|
|
|
const result = util.addressSummary(address, 4, 4, false) |
|
|
|
assert.equal(result, 'FDEa...b825') |
|
|
|
assert.equal(result, 'FDEa...b825') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('#isValidAddress', function () { |
|
|
|
describe('#isValidAddress', function () { |
|
|
|
it('should allow 40-char non-prefixed hex', function () { |
|
|
|
it('should allow 40-char non-prefixed hex', function () { |
|
|
|
var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(result) |
|
|
|
assert.ok(result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should allow 42-char non-prefixed hex', function () { |
|
|
|
it('should allow 42-char non-prefixed hex', function () { |
|
|
|
var address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(result) |
|
|
|
assert.ok(result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should not allow less non hex-prefixed', function () { |
|
|
|
it('should not allow less non hex-prefixed', function () { |
|
|
|
var address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85' |
|
|
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(!result) |
|
|
|
assert.ok(!result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should not allow less hex-prefixed', function () { |
|
|
|
it('should not allow less hex-prefixed', function () { |
|
|
|
var address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85' |
|
|
|
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(!result) |
|
|
|
assert.ok(!result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should recognize correct capitalized checksum', function () { |
|
|
|
it('should recognize correct capitalized checksum', function () { |
|
|
|
var address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825' |
|
|
|
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(result) |
|
|
|
assert.ok(result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should recognize incorrect capitalized checksum', function () { |
|
|
|
it('should recognize incorrect capitalized checksum', function () { |
|
|
|
var address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825' |
|
|
|
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825' |
|
|
|
var result = util.isValidAddress(address) |
|
|
|
const result = util.isValidAddress(address) |
|
|
|
assert.ok(!result) |
|
|
|
assert.ok(!result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
@ -107,58 +107,58 @@ describe('util', function () { |
|
|
|
|
|
|
|
|
|
|
|
describe('#numericBalance', function () { |
|
|
|
describe('#numericBalance', function () { |
|
|
|
it('should return a BN 0 if given nothing', function () { |
|
|
|
it('should return a BN 0 if given nothing', function () { |
|
|
|
var result = util.numericBalance() |
|
|
|
const result = util.numericBalance() |
|
|
|
assert.equal(result.toString(10), 0) |
|
|
|
assert.equal(result.toString(10), 0) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should work with hex prefix', function () { |
|
|
|
it('should work with hex prefix', function () { |
|
|
|
var result = util.numericBalance('0x012') |
|
|
|
const result = util.numericBalance('0x012') |
|
|
|
assert.equal(result.toString(10), '18') |
|
|
|
assert.equal(result.toString(10), '18') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should work with no hex prefix', function () { |
|
|
|
it('should work with no hex prefix', function () { |
|
|
|
var result = util.numericBalance('012') |
|
|
|
const result = util.numericBalance('012') |
|
|
|
assert.equal(result.toString(10), '18') |
|
|
|
assert.equal(result.toString(10), '18') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('#formatBalance', function () { |
|
|
|
describe('#formatBalance', function () { |
|
|
|
it('when given nothing', function () { |
|
|
|
it('when given nothing', function () { |
|
|
|
var result = util.formatBalance() |
|
|
|
const result = util.formatBalance() |
|
|
|
assert.equal(result, 'None', 'should return "None"') |
|
|
|
assert.equal(result, 'None', 'should return "None"') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should return eth as string followed by ETH', function () { |
|
|
|
it('should return eth as string followed by ETH', function () { |
|
|
|
var input = new ethUtil.BN(ethInWei, 10).toJSON() |
|
|
|
const input = new ethUtil.BN(ethInWei, 10).toJSON() |
|
|
|
var result = util.formatBalance(input, 4) |
|
|
|
const result = util.formatBalance(input, 4) |
|
|
|
assert.equal(result, '1.0000 ETH') |
|
|
|
assert.equal(result, '1.0000 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should return eth as string followed by ETH', function () { |
|
|
|
it('should return eth as string followed by ETH', function () { |
|
|
|
var input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON() |
|
|
|
const input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON() |
|
|
|
var result = util.formatBalance(input, 3) |
|
|
|
const result = util.formatBalance(input, 3) |
|
|
|
assert.equal(result, '0.500 ETH') |
|
|
|
assert.equal(result, '0.500 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should display specified decimal points', function () { |
|
|
|
it('should display specified decimal points', function () { |
|
|
|
var input = '0x128dfa6a90b28000' |
|
|
|
const input = '0x128dfa6a90b28000' |
|
|
|
var result = util.formatBalance(input, 2) |
|
|
|
const result = util.formatBalance(input, 2) |
|
|
|
assert.equal(result, '1.33 ETH') |
|
|
|
assert.equal(result, '1.33 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
it('should default to 3 decimal points', function () { |
|
|
|
it('should default to 3 decimal points', function () { |
|
|
|
var input = '0x128dfa6a90b28000' |
|
|
|
const input = '0x128dfa6a90b28000' |
|
|
|
var result = util.formatBalance(input) |
|
|
|
const result = util.formatBalance(input) |
|
|
|
assert.equal(result, '1.337 ETH') |
|
|
|
assert.equal(result, '1.337 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
it('should show 2 significant digits for tiny balances', function () { |
|
|
|
it('should show 2 significant digits for tiny balances', function () { |
|
|
|
var input = '0x1230fa6a90b28' |
|
|
|
const input = '0x1230fa6a90b28' |
|
|
|
var result = util.formatBalance(input) |
|
|
|
const result = util.formatBalance(input) |
|
|
|
assert.equal(result, '0.00032 ETH') |
|
|
|
assert.equal(result, '0.00032 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
it('should not parse the balance and return value with 2 decimal points with ETH at the end', function () { |
|
|
|
it('should not parse the balance and return value with 2 decimal points with ETH at the end', function () { |
|
|
|
var value = '1.2456789' |
|
|
|
const value = '1.2456789' |
|
|
|
var needsParse = false |
|
|
|
const needsParse = false |
|
|
|
var result = util.formatBalance(value, 2, needsParse) |
|
|
|
const result = util.formatBalance(value, 2, needsParse) |
|
|
|
assert.equal(result, '1.24 ETH') |
|
|
|
assert.equal(result, '1.24 ETH') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
@ -166,7 +166,7 @@ describe('util', function () { |
|
|
|
describe('normalizing values', function () { |
|
|
|
describe('normalizing values', function () { |
|
|
|
describe('#normalizeToWei', function () { |
|
|
|
describe('#normalizeToWei', function () { |
|
|
|
it('should convert an eth to the appropriate equivalent values', function () { |
|
|
|
it('should convert an eth to the appropriate equivalent values', function () { |
|
|
|
var valueTable = { |
|
|
|
const valueTable = { |
|
|
|
wei: '1000000000000000000', |
|
|
|
wei: '1000000000000000000', |
|
|
|
kwei: '1000000000000000', |
|
|
|
kwei: '1000000000000000', |
|
|
|
mwei: '1000000000000', |
|
|
|
mwei: '1000000000000', |
|
|
@ -181,11 +181,11 @@ describe('util', function () { |
|
|
|
// gether:'0.000000001',
|
|
|
|
// gether:'0.000000001',
|
|
|
|
// tether:'0.000000000001',
|
|
|
|
// tether:'0.000000000001',
|
|
|
|
} |
|
|
|
} |
|
|
|
var oneEthBn = new ethUtil.BN(ethInWei, 10) |
|
|
|
const oneEthBn = new ethUtil.BN(ethInWei, 10) |
|
|
|
|
|
|
|
|
|
|
|
for (var currency in valueTable) { |
|
|
|
for (const currency in valueTable) { |
|
|
|
var value = new ethUtil.BN(valueTable[currency], 10) |
|
|
|
const value = new ethUtil.BN(valueTable[currency], 10) |
|
|
|
var output = util.normalizeToWei(value, currency) |
|
|
|
const output = util.normalizeToWei(value, currency) |
|
|
|
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`) |
|
|
|
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
@ -193,66 +193,66 @@ describe('util', function () { |
|
|
|
|
|
|
|
|
|
|
|
describe('#normalizeEthStringToWei', function () { |
|
|
|
describe('#normalizeEthStringToWei', function () { |
|
|
|
it('should convert decimal eth to pure wei BN', function () { |
|
|
|
it('should convert decimal eth to pure wei BN', function () { |
|
|
|
var input = '1.23456789' |
|
|
|
const input = '1.23456789' |
|
|
|
var output = util.normalizeEthStringToWei(input) |
|
|
|
const output = util.normalizeEthStringToWei(input) |
|
|
|
assert.equal(output.toString(10), '1234567890000000000') |
|
|
|
assert.equal(output.toString(10), '1234567890000000000') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should convert 1 to expected wei', function () { |
|
|
|
it('should convert 1 to expected wei', function () { |
|
|
|
var input = '1' |
|
|
|
const input = '1' |
|
|
|
var output = util.normalizeEthStringToWei(input) |
|
|
|
const output = util.normalizeEthStringToWei(input) |
|
|
|
assert.equal(output.toString(10), ethInWei) |
|
|
|
assert.equal(output.toString(10), ethInWei) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should account for overflow numbers gracefully by dropping extra precision.', function () { |
|
|
|
it('should account for overflow numbers gracefully by dropping extra precision.', function () { |
|
|
|
var input = '1.11111111111111111111' |
|
|
|
const input = '1.11111111111111111111' |
|
|
|
var output = util.normalizeEthStringToWei(input) |
|
|
|
const output = util.normalizeEthStringToWei(input) |
|
|
|
assert.equal(output.toString(10), '1111111111111111111') |
|
|
|
assert.equal(output.toString(10), '1111111111111111111') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should not truncate very exact wei values that do not have extra precision.', function () { |
|
|
|
it('should not truncate very exact wei values that do not have extra precision.', function () { |
|
|
|
var input = '1.100000000000000001' |
|
|
|
const input = '1.100000000000000001' |
|
|
|
var output = util.normalizeEthStringToWei(input) |
|
|
|
const output = util.normalizeEthStringToWei(input) |
|
|
|
assert.equal(output.toString(10), '1100000000000000001') |
|
|
|
assert.equal(output.toString(10), '1100000000000000001') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('#normalizeNumberToWei', function () { |
|
|
|
describe('#normalizeNumberToWei', function () { |
|
|
|
it('should handle a simple use case', function () { |
|
|
|
it('should handle a simple use case', function () { |
|
|
|
var input = 0.0002 |
|
|
|
const input = 0.0002 |
|
|
|
var output = util.normalizeNumberToWei(input, 'ether') |
|
|
|
const output = util.normalizeNumberToWei(input, 'ether') |
|
|
|
var str = output.toString(10) |
|
|
|
const str = output.toString(10) |
|
|
|
assert.equal(str, '200000000000000') |
|
|
|
assert.equal(str, '200000000000000') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should convert a kwei number to the appropriate equivalent wei', function () { |
|
|
|
it('should convert a kwei number to the appropriate equivalent wei', function () { |
|
|
|
var result = util.normalizeNumberToWei(1.111, 'kwei') |
|
|
|
const result = util.normalizeNumberToWei(1.111, 'kwei') |
|
|
|
assert.equal(result.toString(10), '1111', 'accepts decimals') |
|
|
|
assert.equal(result.toString(10), '1111', 'accepts decimals') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should convert a ether number to the appropriate equivalent wei', function () { |
|
|
|
it('should convert a ether number to the appropriate equivalent wei', function () { |
|
|
|
var result = util.normalizeNumberToWei(1.111, 'ether') |
|
|
|
const result = util.normalizeNumberToWei(1.111, 'ether') |
|
|
|
assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals') |
|
|
|
assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals') |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
describe('#isHex', function () { |
|
|
|
describe('#isHex', function () { |
|
|
|
it('should return true when given a hex string', function () { |
|
|
|
it('should return true when given a hex string', function () { |
|
|
|
var result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') |
|
|
|
const result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') |
|
|
|
assert(result) |
|
|
|
assert(result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should return false when given a non-hex string', function () { |
|
|
|
it('should return false when given a non-hex string', function () { |
|
|
|
var result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714imnotreal') |
|
|
|
const result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714imnotreal') |
|
|
|
assert(!result) |
|
|
|
assert(!result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should return false when given a string containing a non letter/number character', function () { |
|
|
|
it('should return false when given a string containing a non letter/number character', function () { |
|
|
|
var result = util.isHex('c3ab8ff13720!8ad9047dd39466b3c%8974e592c2fa383d4a396071imnotreal') |
|
|
|
const result = util.isHex('c3ab8ff13720!8ad9047dd39466b3c%8974e592c2fa383d4a396071imnotreal') |
|
|
|
assert(!result) |
|
|
|
assert(!result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should return true when given a hex string with hex-prefix', function () { |
|
|
|
it('should return true when given a hex string with hex-prefix', function () { |
|
|
|
var result = util.isHex('0xc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') |
|
|
|
const result = util.isHex('0xc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2') |
|
|
|
assert(result) |
|
|
|
assert(result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|