/ embark-ui / src / services / unitConverter.js
unitConverter.js
 1  import Units from 'ethereumjs-units';
 2  
 3  const UNITS = [
 4    { key: 'wei', name: 'Wei' },
 5    { key: 'kwei', name: 'KWei' },
 6    { key: 'mwei', name: 'MWei' },
 7    { key: 'gwei', name: 'Szabo' },
 8    { key: 'finney', name: 'Finney' },
 9    { key: 'ether', name: 'Ether' },
10    { key: 'kether', name: 'KEther' },
11    { key: 'mether', name: 'MEther' },
12    { key: 'gether', name: 'GEther' },
13    { key: 'tether', name: 'TEther' }
14  ];
15  
16  const safeConvert = (value, from, to) => {
17    try {
18      value = Units.convert(value, from, to);
19    } catch (e) {
20      value = '';
21    }
22    return value;
23  };
24  
25  
26  export const calculateUnits = (value, from) => {
27    return UNITS.map((unit) => {
28      unit.value = safeConvert(value, from, unit.key);
29      return unit;
30    });
31  };