2018-06-24 11:48:02 +02:00
|
|
|
import assert from 'assert'
|
2020-07-08 22:17:53 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { addCurrencies, conversionUtil } from './conversion-util'
|
2018-06-24 11:48:02 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('conversion utils', function () {
|
|
|
|
describe('addCurrencies()', function () {
|
|
|
|
it('add whole numbers', function () {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(3, 9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
|
|
|
})
|
2018-06-24 11:56:07 +02:00
|
|
|
assert.equal(result.toNumber(), 12)
|
2018-06-24 11:48:02 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('add decimals', function () {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(1.3, 1.9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
|
|
|
})
|
2018-06-24 11:56:07 +02:00
|
|
|
assert.equal(result.toNumber(), 3.2)
|
2018-06-24 11:48:02 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('add repeating decimals', function () {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(1 / 3, 1 / 9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
|
|
|
})
|
2018-06-24 11:56:07 +02:00
|
|
|
assert.equal(result.toNumber(), 0.4444444444444444)
|
2018-06-24 11:48:02 +02:00
|
|
|
})
|
|
|
|
})
|
2020-07-08 22:17:53 +02:00
|
|
|
|
|
|
|
describe('conversionUtil', function () {
|
|
|
|
it('Returns expected types', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const conv1 = conversionUtil(1000000000000000000, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
const conv2 = conversionUtil(1, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
assert(
|
|
|
|
typeof conv1 === 'string',
|
|
|
|
'conversion 1 should return type string',
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
assert(conv2 instanceof BigNumber, 'conversion 2 should be a BigNumber')
|
|
|
|
})
|
|
|
|
it('Converts from dec to hex', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1000000000000000000', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
}),
|
|
|
|
'de0b6b3a7640000',
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1500000000000000000', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
}),
|
|
|
|
'14d1120d7b160000',
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts hex formatted numbers to dec', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('0xde0b6b3a7640000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
}),
|
|
|
|
1000000000000000000,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('0x14d1120d7b160000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
}),
|
|
|
|
1500000000000000000,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts WEI to ETH', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('0xde0b6b3a7640000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
}),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('0x14d1120d7b160000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
}),
|
|
|
|
1.5,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts ETH to WEI', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
}),
|
|
|
|
1000000000000000000,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
}),
|
|
|
|
1500000000000000000,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts ETH to GWEI', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
}),
|
|
|
|
1000000000,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
}),
|
|
|
|
1500000000,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts ETH to USD', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
}),
|
|
|
|
468.58,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
}),
|
|
|
|
702.87,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
it('Converts USD to ETH', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
conversionUtil('468.58', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
invertConversionRate: true,
|
|
|
|
}),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
assert.equal(
|
|
|
|
conversionUtil('702.87', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
invertConversionRate: true,
|
|
|
|
}),
|
|
|
|
1.5,
|
|
|
|
)
|
2020-07-08 22:17:53 +02:00
|
|
|
})
|
|
|
|
})
|
2018-06-24 11:48:02 +02:00
|
|
|
})
|