2017-11-12 19:45:52 +00:00
|
|
|
import {
|
|
|
|
fromWei,
|
|
|
|
Wei,
|
|
|
|
toWei,
|
|
|
|
toTokenBase,
|
|
|
|
fromTokenBase,
|
2017-12-11 20:36:22 +00:00
|
|
|
convertTokenBase,
|
2017-11-12 19:45:52 +00:00
|
|
|
getDecimal,
|
|
|
|
TokenValue
|
|
|
|
} from 'libs/units';
|
|
|
|
|
|
|
|
const Units = {
|
|
|
|
wei: '1',
|
|
|
|
kwei: '1000',
|
|
|
|
ada: '1000',
|
|
|
|
femtoether: '1000',
|
|
|
|
mwei: '1000000',
|
|
|
|
babbage: '1000000',
|
|
|
|
picoether: '1000000',
|
|
|
|
gwei: '1000000000',
|
|
|
|
shannon: '1000000000',
|
|
|
|
nanoether: '1000000000',
|
|
|
|
nano: '1000000000',
|
|
|
|
szabo: '1000000000000',
|
|
|
|
microether: '1000000000000',
|
|
|
|
micro: '1000000000000',
|
|
|
|
finney: '1000000000000000',
|
|
|
|
milliether: '1000000000000000',
|
|
|
|
milli: '1000000000000000',
|
|
|
|
ether: '1000000000000000000',
|
|
|
|
kether: '1000000000000000000000',
|
|
|
|
grand: '1000000000000000000000',
|
|
|
|
einstein: '1000000000000000000000',
|
|
|
|
mether: '1000000000000000000000000',
|
|
|
|
gether: '1000000000000000000000000000',
|
|
|
|
tether: '1000000000000000000000000000000'
|
|
|
|
};
|
2017-08-23 06:57:18 +00:00
|
|
|
|
|
|
|
describe('Units', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
describe('ethereum units', () => {
|
|
|
|
describe('ether', () => {
|
|
|
|
const wei = Wei(Units.ether);
|
|
|
|
const ether = fromWei(wei, 'ether');
|
|
|
|
it('should equal one ether', () => {
|
|
|
|
expect(ether).toEqual('1');
|
|
|
|
});
|
2017-08-23 06:57:18 +00:00
|
|
|
|
2017-11-12 19:45:52 +00:00
|
|
|
it('should equal 1 * 10^18 wei', () => {
|
|
|
|
const converted = toWei(ether, getDecimal('ether'));
|
|
|
|
expect(converted.toString()).toEqual(Units.ether);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('gwei', () => {
|
|
|
|
const wei = Wei(`2${Units.gwei}`);
|
|
|
|
const gwei = fromWei(wei, 'gwei');
|
|
|
|
it('should equal 21 gwei', () => {
|
|
|
|
expect(gwei).toEqual('21');
|
|
|
|
});
|
|
|
|
it('should equal 21 * 10^9 wei', () => {
|
|
|
|
const converted = toWei(gwei, getDecimal('gwei'));
|
|
|
|
expect(converted.toString()).toEqual(wei.toString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('kwei', () => {
|
|
|
|
const wei = Wei('1623');
|
|
|
|
const kwei = fromWei(wei, 'kwei');
|
|
|
|
it('should equal 1.623 kwei', () => {
|
|
|
|
expect(kwei).toEqual('1.623');
|
|
|
|
});
|
|
|
|
it('should equal 1.623 * 10^3 wei', () => {
|
|
|
|
const converted = toWei(kwei, getDecimal('kwei'));
|
|
|
|
expect(converted.toString()).toEqual('1623');
|
2017-08-23 06:57:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-11-12 19:45:52 +00:00
|
|
|
describe('token units', () => {
|
|
|
|
describe('token 1', () => {
|
|
|
|
const tokens = '732156.34592016';
|
|
|
|
const decimal = 18;
|
|
|
|
const tokenBase = toTokenBase(tokens, decimal);
|
2017-12-11 20:36:22 +00:00
|
|
|
it('toTokenBase should equal 732156345920160000000000', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(tokenBase.toString()).toEqual('732156345920160000000000');
|
|
|
|
});
|
2017-12-11 20:36:22 +00:00
|
|
|
it('fromTokenBase should equal 732156.34592016', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(fromTokenBase(tokenBase, decimal)).toEqual(tokens);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('token 2', () => {
|
|
|
|
const tokens = '8000';
|
|
|
|
const decimal = 8;
|
|
|
|
const converted = fromTokenBase(TokenValue(tokens), decimal);
|
2017-12-11 20:36:22 +00:00
|
|
|
it('fromTokenBase should equal 0.00008', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(converted).toEqual('0.00008');
|
|
|
|
});
|
2017-12-11 20:36:22 +00:00
|
|
|
it('toTokenBase should equal 8000', () => {
|
2017-11-12 19:45:52 +00:00
|
|
|
expect(toTokenBase(converted, decimal));
|
2017-08-23 06:57:18 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-11 20:36:22 +00:00
|
|
|
describe('convertTokenBase', () => {
|
|
|
|
const conversions = [
|
|
|
|
{
|
|
|
|
oldDecimal: 0,
|
|
|
|
newDecimal: 18,
|
|
|
|
startValue: '42',
|
|
|
|
endValue: '42000000000000000000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
oldDecimal: 6,
|
|
|
|
newDecimal: 12,
|
|
|
|
startValue: '547834782',
|
|
|
|
endValue: '547834782000000'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
oldDecimal: 18,
|
|
|
|
newDecimal: 18,
|
|
|
|
startValue: '311095801958902158012580',
|
|
|
|
endValue: '311095801958902158012580'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
conversions.forEach(c => {
|
|
|
|
it(`should convert decimal ${c.oldDecimal} to decimal ${c.newDecimal}`, () => {
|
|
|
|
const tokenValue = TokenValue(c.startValue);
|
|
|
|
const converted = convertTokenBase(tokenValue, c.oldDecimal, c.newDecimal);
|
|
|
|
expect(converted.toString()).toEqual(c.endValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-23 06:57:18 +00:00
|
|
|
});
|
|
|
|
});
|