MyCrypto/spec/libs/units.spec.ts

100 lines
2.8 KiB
TypeScript
Raw Normal View History

Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 11:45:52 -08:00
import {
fromWei,
Wei,
toWei,
toTokenBase,
fromTokenBase,
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'
};
describe('Units', () => {
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 11:45:52 -08: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');
});
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 11:45:52 -08: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');
});
});
});
Replace bignumber.js with bn.js (#319) * Add definition file for bn.js * Remove types-bn * make isBN a static property * progress commit -- swap out bignumber.js for bn.js * Swap out bignumber for bn in vendor * Change modn to number return * Start to strip out units lib for a string manipulation based lib * Convert codebase to only base units * Get rid of useless component * Handle only wei in values * Use unit conversion in sidebar * Automatically strip hex prefix, and handle decimal edge case * Handle base 16 wei in transactions * Make a render callback component for dealing with unit conversion * Switch contracts to use bn.js, and get transaction values from signedTx instead of state * Get send transaction working with bn.js * Remove redundant hex stripping, return base value of tokens * Cleanup unit file * Re-implement toFixed for strings * Use formatNumber in codebase * Cleanup code * Undo package test changes * Update snapshot and remove console logs * Use TokenValue / Wei more consistently where applicable * Add typing to deterministicWallets, fix confirmation modal, make UnitDisplay more flexible * Clean up prop handling in UnitDisplay * Change instanceof to typeof check, change boolean of displayBalance * Fix tsc errors * Fix token row displaying wrong decimals * Fix deterministic modal token display * Handle hex and non hex strings automatically in BN conversion * Fix handling of strings and numbers for BN * add web3 fixes & comments * Display short balances on deterministic modals * add more tests, fix rounding * Add spacer to balance sidebar network name * Fix tsc error
2017-11-12 11:45:52 -08:00
describe('token units', () => {
describe('token 1', () => {
const tokens = '732156.34592016';
const decimal = 18;
const tokenBase = toTokenBase(tokens, decimal);
it('should equal 732156345920160000000000', () => {
expect(tokenBase.toString()).toEqual('732156345920160000000000');
});
it('should equal 732156.34592016', () => {
expect(fromTokenBase(tokenBase, decimal)).toEqual(tokens);
});
});
describe('token 2', () => {
const tokens = '8000';
const decimal = 8;
const converted = fromTokenBase(TokenValue(tokens), decimal);
it('should equal 0.00008', () => {
expect(converted).toEqual('0.00008');
});
it('should equal 8000', () => {
expect(toTokenBase(converted, decimal));
});
});
});
});