2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-08-03 04:34:10 +03:00
|
|
|
import { BigNumber, bigNumberify } from './bignumber';
|
|
|
|
import { Zero, NegativeOne } from './constants';
|
2018-07-16 10:27:49 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
// Imported Types
|
|
|
|
import { BigNumberish } from './bignumber';
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
import * as errors from './errors';
|
|
|
|
|
|
|
|
const names = [
|
2018-01-10 02:25:44 +03:00
|
|
|
'wei',
|
|
|
|
'kwei',
|
|
|
|
'Mwei',
|
|
|
|
'Gwei',
|
|
|
|
'szabo',
|
|
|
|
'finny',
|
|
|
|
'ether',
|
|
|
|
];
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
type UnitInfo = {
|
|
|
|
decimals: number;
|
|
|
|
tenPower: BigNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
var unitInfos: { [key: string]: UnitInfo } = {};
|
|
|
|
|
|
|
|
function _getUnitInfo(value: string): UnitInfo {
|
|
|
|
return {
|
|
|
|
decimals: value.length - 1,
|
|
|
|
tenPower: bigNumberify(value)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build cache of common units
|
|
|
|
(function() {
|
2018-04-05 22:46:44 +03:00
|
|
|
|
|
|
|
// Cache the common units
|
2018-06-23 03:30:50 +03:00
|
|
|
let value = '1';
|
2018-04-05 22:46:44 +03:00
|
|
|
names.forEach(function(name) {
|
2018-06-23 03:30:50 +03:00
|
|
|
let info = _getUnitInfo(value);
|
2018-01-10 02:25:44 +03:00
|
|
|
unitInfos[name.toLowerCase()] = info;
|
|
|
|
unitInfos[String(info.decimals)] = info;
|
|
|
|
value += '000';
|
|
|
|
});
|
2018-06-23 03:30:50 +03:00
|
|
|
})();
|
|
|
|
|
|
|
|
function getUnitInfo(name: string | number): UnitInfo {
|
2018-01-10 02:25:44 +03:00
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
// Try the cache
|
|
|
|
var info = unitInfos[String(name).toLowerCase()];
|
|
|
|
|
|
|
|
if (!info && typeof(name) === 'number' && parseInt(String(name)) == name && name >= 0 && name <= 256) {
|
|
|
|
var value = '1';
|
|
|
|
for (var i = 0; i < name; i++) { value += '0'; }
|
|
|
|
info = _getUnitInfo(value);
|
2018-01-10 02:25:44 +03:00
|
|
|
}
|
2018-06-23 03:30:50 +03:00
|
|
|
|
|
|
|
// Make sure we got something
|
|
|
|
if (!info) {
|
2018-06-25 01:41:28 +03:00
|
|
|
errors.throwError('invalid unitType', errors.INVALID_ARGUMENT, { arg: 'name', value: name });
|
2018-06-23 03:30:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2018-01-10 02:25:44 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
export function formatUnits(value: BigNumberish, unitType?: string | number, options?: any): string {
|
2018-06-13 22:39:39 +03:00
|
|
|
/*
|
2018-01-10 02:25:44 +03:00
|
|
|
if (typeof(unitType) === 'object' && !options) {
|
|
|
|
options = unitType;
|
|
|
|
unitType = undefined;
|
|
|
|
}
|
2018-04-05 22:46:44 +03:00
|
|
|
if (unitType == null) { unitType = 18; }
|
2018-06-13 22:39:39 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
var unitInfo = getUnitInfo(unitType);
|
|
|
|
|
|
|
|
// Make sure wei is a big number (convert as necessary)
|
|
|
|
value = bigNumberify(value);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-08-03 04:34:10 +03:00
|
|
|
var negative = value.lt(Zero);
|
|
|
|
if (negative) { value = value.mul(NegativeOne); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
var fraction = value.mod(unitInfo.tenPower).toString();
|
2018-01-10 02:25:44 +03:00
|
|
|
while (fraction.length < unitInfo.decimals) { fraction = '0' + fraction; }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
// Strip off trailing zeros (but keep one if would otherwise be bare decimal point)
|
2016-08-24 02:35:19 +03:00
|
|
|
if (!options.pad) {
|
|
|
|
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
var whole = value.div(unitInfo.tenPower).toString();
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
if (options.commify) {
|
|
|
|
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
value = whole + '.' + fraction;
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
if (negative) { value = '-' + value; }
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
export function parseUnits(value: string, unitType?: string | number): BigNumber {
|
2018-04-05 22:46:44 +03:00
|
|
|
if (unitType == null) { unitType = 18; }
|
|
|
|
var unitInfo = getUnitInfo(unitType);
|
2018-01-10 02:25:44 +03:00
|
|
|
|
|
|
|
if (typeof(value) !== 'string' || !value.match(/^-?[0-9.,]+$/)) {
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.throwError('invalid decimal value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
2016-08-24 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
// Remove commas
|
|
|
|
var value = value.replace(/,/g,'');
|
2017-02-24 22:41:24 +03:00
|
|
|
|
2018-07-15 00:19:08 +03:00
|
|
|
if (unitInfo.decimals === 0) {
|
|
|
|
return bigNumberify(value);
|
|
|
|
}
|
|
|
|
|
2016-08-24 02:35:19 +03:00
|
|
|
// Is it negative?
|
2017-04-05 00:22:53 +03:00
|
|
|
var negative = (value.substring(0, 1) === '-');
|
|
|
|
if (negative) { value = value.substring(1); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
if (value === '.') {
|
|
|
|
errors.throwError('missing value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
|
|
|
}
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
// Split it into a whole and fractional part
|
2018-06-15 11:18:17 +03:00
|
|
|
let comps = value.split('.');
|
2018-06-13 22:39:39 +03:00
|
|
|
if (comps.length > 2) {
|
|
|
|
errors.throwError('too many decimal points', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
|
|
|
}
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
let whole = comps[0], fraction = comps[1];
|
2016-08-24 02:35:19 +03:00
|
|
|
if (!whole) { whole = '0'; }
|
|
|
|
if (!fraction) { fraction = '0'; }
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
// Prevent underflow
|
|
|
|
if (fraction.length > unitInfo.decimals) {
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.throwError(
|
|
|
|
'underflow occurred',
|
|
|
|
errors.NUMERIC_FAULT,
|
|
|
|
{ operation: 'division', fault: "underflow" }
|
|
|
|
);
|
2018-01-10 02:25:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fully pad the string with zeros to get to wei
|
|
|
|
while (fraction.length < unitInfo.decimals) { fraction += '0'; }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
let wholeValue = bigNumberify(whole);
|
|
|
|
let fractionValue = bigNumberify(fraction);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
let wei = (wholeValue.mul(unitInfo.tenPower)).add(fractionValue);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-08-03 04:34:10 +03:00
|
|
|
if (negative) { wei = wei.mul(NegativeOne); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
return wei;
|
|
|
|
}
|
|
|
|
|
2018-07-23 03:05:45 +03:00
|
|
|
export function formatEther(wei: BigNumberish, options?: any): string {
|
2018-01-10 02:25:44 +03:00
|
|
|
return formatUnits(wei, 18, options);
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
export function parseEther(ether: string): BigNumber {
|
2018-01-10 02:25:44 +03:00
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|
|
|
|
|