2021-02-04 19:15:23 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
2021-11-29 18:40:48 +01:00
|
|
|
import {
|
|
|
|
addCurrencies,
|
|
|
|
conversionUtil,
|
|
|
|
divideCurrencies,
|
|
|
|
} from './conversion.utils';
|
2018-06-24 11:48:02 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('conversion utils', () => {
|
|
|
|
describe('addCurrencies()', () => {
|
|
|
|
it('add whole numbers', () => {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(3, 9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.toNumber()).toStrictEqual(12);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-24 11:48:02 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('add decimals', () => {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(1.3, 1.9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.toNumber()).toStrictEqual(3.2);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-24 11:48:02 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('add repeating decimals', () => {
|
2020-11-13 07:08:18 +01:00
|
|
|
const result = addCurrencies(1 / 3, 1 / 9, {
|
|
|
|
aBase: 10,
|
|
|
|
bBase: 10,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result.toNumber()).toStrictEqual(0.4444444444444444);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-07-08 22:17:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('conversionUtil', () => {
|
|
|
|
it('returns expected types', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const conv1 = conversionUtil(1000000000000000000, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
const conv2 = conversionUtil(1, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(typeof conv1 === 'string').toStrictEqual(true);
|
|
|
|
expect(conv2 instanceof BigNumber).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts from dec to hex', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1000000000000000000', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('de0b6b3a7640000');
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1500000000000000000', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('14d1120d7b160000');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts hex formatted numbers to dec', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('0xde0b6b3a7640000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1000000000000000000');
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('0x14d1120d7b160000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1500000000000000000');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts WEI to ETH', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('0xde0b6b3a7640000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1');
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('0x14d1120d7b160000', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1.5');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts ETH to WEI', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
2020-12-03 16:46:22 +01:00
|
|
|
}).toNumber(),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(1000000000000000000);
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
2020-12-03 16:46:22 +01:00
|
|
|
}).toNumber(),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(1500000000000000000);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts ETH to GWEI', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'GWEI',
|
2020-12-03 16:46:22 +01:00
|
|
|
}).toNumber(),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(1000000000);
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'GWEI',
|
2020-12-03 16:46:22 +01:00
|
|
|
}).toNumber(),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(1500000000);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts ETH to USD', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('468.58');
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('1.5', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('702.87');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('converts USD to ETH', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('468.58', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
invertConversionRate: true,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1');
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil('702.87', {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
toCurrency: 'usd',
|
|
|
|
conversionRate: 468.58,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
invertConversionRate: true,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('1.5');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2021-11-29 18:40:48 +01:00
|
|
|
|
|
|
|
describe('divideCurrencies()', () => {
|
|
|
|
it('should correctly divide decimal values', () => {
|
|
|
|
const result = divideCurrencies(9, 3, {
|
|
|
|
dividendBase: 10,
|
|
|
|
divisorBase: 10,
|
|
|
|
});
|
|
|
|
expect(result.toNumber()).toStrictEqual(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly divide hexadecimal values', () => {
|
|
|
|
const result = divideCurrencies(1000, 0xa, {
|
|
|
|
dividendBase: 16,
|
|
|
|
divisorBase: 16,
|
|
|
|
});
|
|
|
|
expect(result.toNumber()).toStrictEqual(0x100);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly divide hexadecimal value from decimal value', () => {
|
|
|
|
const result = divideCurrencies(0x3e8, 0xa, {
|
|
|
|
dividendBase: 16,
|
|
|
|
divisorBase: 16,
|
|
|
|
});
|
|
|
|
expect(result.toNumber()).toStrictEqual(0x100);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error for wrong base value', () => {
|
|
|
|
expect(() => {
|
|
|
|
divideCurrencies(0x3e8, 0xa, {
|
|
|
|
dividendBase: 10.5,
|
|
|
|
divisorBase: 7,
|
|
|
|
});
|
|
|
|
}).toThrow('Must specify valid dividendBase and divisorBase');
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|