You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
9.2 KiB
259 lines
9.2 KiB
5 years ago
|
import assert from 'assert'
|
||
|
import sinon from 'sinon'
|
||
|
import ethUtil from 'ethereumjs-util'
|
||
5 years ago
|
import * as util from './util'
|
||
9 years ago
|
|
||
8 years ago
|
describe('util', function () {
|
||
5 years ago
|
let ethInWei = '1'
|
||
|
for (let i = 0; i < 18; i++) {
|
||
5 years ago
|
ethInWei += '0'
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
beforeEach(function () {
|
||
7 years ago
|
this.sinon = sinon.createSandbox()
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
afterEach(function () {
|
||
9 years ago
|
this.sinon.restore()
|
||
|
})
|
||
|
|
||
8 years ago
|
describe('#parseBalance', function () {
|
||
|
it('should render 0.01 eth correctly', function () {
|
||
9 years ago
|
const input = '0x2386F26FC10000'
|
||
|
const output = util.parseBalance(input)
|
||
|
assert.deepEqual(output, ['0', '01'])
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
it('should render 12.023 eth correctly', function () {
|
||
9 years ago
|
const input = 'A6DA46CCA6858000'
|
||
|
const output = util.parseBalance(input)
|
||
|
assert.deepEqual(output, ['12', '023'])
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
it('should render 0.0000000342422 eth correctly', function () {
|
||
9 years ago
|
const input = '0x7F8FE81C0'
|
||
|
const output = util.parseBalance(input)
|
||
|
assert.deepEqual(output, ['0', '0000000342422'])
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
it('should render 0 eth correctly', function () {
|
||
9 years ago
|
const input = '0x0'
|
||
|
const output = util.parseBalance(input)
|
||
|
assert.deepEqual(output, ['0', '0'])
|
||
|
})
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
describe('#addressSummary', function () {
|
||
|
it('should add case-sensitive checksum', function () {
|
||
5 years ago
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
||
|
const result = util.addressSummary(address)
|
||
9 years ago
|
assert.equal(result, '0xFDEa65C8...b825')
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
it('should accept arguments for firstseg, lastseg, and keepPrefix', function () {
|
||
5 years ago
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
||
|
const result = util.addressSummary(address, 4, 4, false)
|
||
9 years ago
|
assert.equal(result, 'FDEa...b825')
|
||
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
describe('#isValidAddress', function () {
|
||
|
it('should allow 40-char non-prefixed hex', function () {
|
||
5 years ago
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(result)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should allow 42-char non-prefixed hex', function () {
|
||
5 years ago
|
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(result)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should not allow less non hex-prefixed', function () {
|
||
5 years ago
|
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(!result)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should not allow less hex-prefixed', function () {
|
||
5 years ago
|
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(!result)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should recognize correct capitalized checksum', function () {
|
||
5 years ago
|
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(result)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should recognize incorrect capitalized checksum', function () {
|
||
5 years ago
|
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825'
|
||
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
assert.ok(!result)
|
||
|
})
|
||
9 years ago
|
|
||
8 years ago
|
it('should recognize this sample hashed address', function () {
|
||
9 years ago
|
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0'
|
||
9 years ago
|
const result = util.isValidAddress(address)
|
||
9 years ago
|
const hashed = ethUtil.toChecksumAddress(address.toLowerCase())
|
||
9 years ago
|
assert.equal(hashed, address, 'example is hashed correctly')
|
||
9 years ago
|
assert.ok(result, 'is valid by our check')
|
||
9 years ago
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
describe('#numericBalance', function () {
|
||
|
it('should return a BN 0 if given nothing', function () {
|
||
5 years ago
|
const result = util.numericBalance()
|
||
9 years ago
|
assert.equal(result.toString(10), 0)
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should work with hex prefix', function () {
|
||
5 years ago
|
const result = util.numericBalance('0x012')
|
||
9 years ago
|
assert.equal(result.toString(10), '18')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should work with no hex prefix', function () {
|
||
5 years ago
|
const result = util.numericBalance('012')
|
||
9 years ago
|
assert.equal(result.toString(10), '18')
|
||
|
})
|
||
|
})
|
||
|
|
||
8 years ago
|
describe('#formatBalance', function () {
|
||
|
it('when given nothing', function () {
|
||
5 years ago
|
const result = util.formatBalance()
|
||
9 years ago
|
assert.equal(result, 'None', 'should return "None"')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should return eth as string followed by ETH', function () {
|
||
5 years ago
|
const input = new ethUtil.BN(ethInWei, 10).toJSON()
|
||
|
const result = util.formatBalance(input, 4)
|
||
9 years ago
|
assert.equal(result, '1.0000 ETH')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should return eth as string followed by ETH', function () {
|
||
5 years ago
|
const input = new ethUtil.BN(ethInWei, 10).div(new ethUtil.BN('2', 10)).toJSON()
|
||
|
const result = util.formatBalance(input, 3)
|
||
9 years ago
|
assert.equal(result, '0.500 ETH')
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
it('should display specified decimal points', function () {
|
||
5 years ago
|
const input = '0x128dfa6a90b28000'
|
||
|
const result = util.formatBalance(input, 2)
|
||
9 years ago
|
assert.equal(result, '1.33 ETH')
|
||
9 years ago
|
})
|
||
8 years ago
|
it('should default to 3 decimal points', function () {
|
||
5 years ago
|
const input = '0x128dfa6a90b28000'
|
||
|
const result = util.formatBalance(input)
|
||
9 years ago
|
assert.equal(result, '1.337 ETH')
|
||
9 years ago
|
})
|
||
8 years ago
|
it('should show 2 significant digits for tiny balances', function () {
|
||
5 years ago
|
const input = '0x1230fa6a90b28'
|
||
|
const result = util.formatBalance(input)
|
||
9 years ago
|
assert.equal(result, '0.00032 ETH')
|
||
9 years ago
|
})
|
||
8 years ago
|
it('should not parse the balance and return value with 2 decimal points with ETH at the end', function () {
|
||
5 years ago
|
const value = '1.2456789'
|
||
|
const needsParse = false
|
||
|
const result = util.formatBalance(value, 2, needsParse)
|
||
8 years ago
|
assert.equal(result, '1.24 ETH')
|
||
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
describe('normalizing values', function () {
|
||
|
describe('#normalizeToWei', function () {
|
||
|
it('should convert an eth to the appropriate equivalent values', function () {
|
||
5 years ago
|
const valueTable = {
|
||
8 years ago
|
wei: '1000000000000000000',
|
||
|
kwei: '1000000000000000',
|
||
|
mwei: '1000000000000',
|
||
|
gwei: '1000000000',
|
||
9 years ago
|
szabo: '1000000',
|
||
8 years ago
|
finney: '1000',
|
||
9 years ago
|
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',
|
||
|
}
|
||
5 years ago
|
const oneEthBn = new ethUtil.BN(ethInWei, 10)
|
||
9 years ago
|
|
||
5 years ago
|
for (const currency in valueTable) {
|
||
|
const value = new ethUtil.BN(valueTable[currency], 10)
|
||
|
const output = util.normalizeToWei(value, currency)
|
||
9 years ago
|
assert.equal(output.toString(10), valueTable.wei, `value of ${output.toString(10)} ${currency} should convert to ${oneEthBn}`)
|
||
|
}
|
||
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
describe('#normalizeEthStringToWei', function () {
|
||
|
it('should convert decimal eth to pure wei BN', function () {
|
||
5 years ago
|
const input = '1.23456789'
|
||
|
const output = util.normalizeEthStringToWei(input)
|
||
9 years ago
|
assert.equal(output.toString(10), '1234567890000000000')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should convert 1 to expected wei', function () {
|
||
5 years ago
|
const input = '1'
|
||
|
const output = util.normalizeEthStringToWei(input)
|
||
9 years ago
|
assert.equal(output.toString(10), ethInWei)
|
||
|
})
|
||
7 years ago
|
|
||
|
it('should account for overflow numbers gracefully by dropping extra precision.', function () {
|
||
5 years ago
|
const input = '1.11111111111111111111'
|
||
|
const output = util.normalizeEthStringToWei(input)
|
||
7 years ago
|
assert.equal(output.toString(10), '1111111111111111111')
|
||
|
})
|
||
7 years ago
|
|
||
|
it('should not truncate very exact wei values that do not have extra precision.', function () {
|
||
5 years ago
|
const input = '1.100000000000000001'
|
||
|
const output = util.normalizeEthStringToWei(input)
|
||
7 years ago
|
assert.equal(output.toString(10), '1100000000000000001')
|
||
|
})
|
||
9 years ago
|
})
|
||
|
|
||
8 years ago
|
describe('#normalizeNumberToWei', function () {
|
||
|
it('should handle a simple use case', function () {
|
||
5 years ago
|
const input = 0.0002
|
||
|
const output = util.normalizeNumberToWei(input, 'ether')
|
||
|
const str = output.toString(10)
|
||
9 years ago
|
assert.equal(str, '200000000000000')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should convert a kwei number to the appropriate equivalent wei', function () {
|
||
5 years ago
|
const result = util.normalizeNumberToWei(1.111, 'kwei')
|
||
9 years ago
|
assert.equal(result.toString(10), '1111', 'accepts decimals')
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should convert a ether number to the appropriate equivalent wei', function () {
|
||
5 years ago
|
const result = util.normalizeNumberToWei(1.111, 'ether')
|
||
9 years ago
|
assert.equal(result.toString(10), '1111000000000000000', 'accepts decimals')
|
||
|
})
|
||
|
})
|
||
8 years ago
|
describe('#isHex', function () {
|
||
|
it('should return true when given a hex string', function () {
|
||
5 years ago
|
const result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2')
|
||
8 years ago
|
assert(result)
|
||
8 years ago
|
})
|
||
|
|
||
8 years ago
|
it('should return false when given a non-hex string', function () {
|
||
5 years ago
|
const result = util.isHex('c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714imnotreal')
|
||
8 years ago
|
assert(!result)
|
||
8 years ago
|
})
|
||
|
|
||
8 years ago
|
it('should return false when given a string containing a non letter/number character', function () {
|
||
5 years ago
|
const result = util.isHex('c3ab8ff13720!8ad9047dd39466b3c%8974e592c2fa383d4a396071imnotreal')
|
||
8 years ago
|
assert(!result)
|
||
8 years ago
|
})
|
||
|
|
||
8 years ago
|
it('should return true when given a hex string with hex-prefix', function () {
|
||
5 years ago
|
const result = util.isHex('0xc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2')
|
||
8 years ago
|
assert(result)
|
||
8 years ago
|
})
|
||
8 years ago
|
})
|
||
9 years ago
|
})
|
||
9 years ago
|
})
|