2021-02-04 19:15:23 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { addCurrencies, conversionUtil } from './conversion-util';
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|