2017-02-24 22:41:24 +03:00
|
|
|
var bigNumberify = require('./bignumber.js').bigNumberify;
|
2017-04-05 00:22:53 +03:00
|
|
|
var throwError = require('./throw-error');
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
var zero = new bigNumberify(0);
|
|
|
|
var negative1 = new bigNumberify(-1);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
var names = [
|
|
|
|
'wei',
|
|
|
|
'kwei',
|
|
|
|
'Mwei',
|
|
|
|
'Gwei',
|
|
|
|
'szabo',
|
|
|
|
'finny',
|
|
|
|
'ether',
|
|
|
|
];
|
|
|
|
|
|
|
|
var getUnitInfo = (function() {
|
|
|
|
var unitInfos = {};
|
|
|
|
|
|
|
|
var value = '1';
|
|
|
|
names.forEach(function(name) {
|
|
|
|
var info = {
|
|
|
|
decimals: value.length - 1,
|
|
|
|
tenPower: bigNumberify(value),
|
|
|
|
name: name
|
|
|
|
};
|
|
|
|
unitInfos[name.toLowerCase()] = info;
|
|
|
|
unitInfos[String(info.decimals)] = info;
|
|
|
|
value += '000';
|
|
|
|
});
|
|
|
|
|
|
|
|
return function(name) {
|
|
|
|
return unitInfos[String(name).toLowerCase()];
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
function formatUnits(value, unitType, options) {
|
|
|
|
if (typeof(unitType) === 'object' && !options) {
|
|
|
|
options = unitType;
|
|
|
|
unitType = undefined;
|
|
|
|
}
|
|
|
|
if (unitType == null) { unitType = 18; }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
var negative = value.lt(zero);
|
|
|
|
if (negative) { value = value.mul(negative1); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
var fraction = value.mod(unitInfo.tenPower).toString(10);
|
|
|
|
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-01-10 02:25:44 +03:00
|
|
|
var whole = value.div(unitInfo.tenPower).toString(10);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
if (options.commify) {
|
|
|
|
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = whole + '.' + fraction;
|
|
|
|
|
|
|
|
if (negative) { value = '-' + value; }
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
function parseUnits(value, unitType) {
|
|
|
|
var unitInfo = getUnitInfo(unitType || 18);
|
|
|
|
if (!unitInfo) { throwError('invalid unitType', { unitType: unitType }); }
|
|
|
|
|
|
|
|
if (typeof(value) !== 'string' || !value.match(/^-?[0-9.,]+$/)) {
|
|
|
|
throwError('invalid value', { input: 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
|
|
|
|
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-01-10 02:25:44 +03:00
|
|
|
if (value === '.') { throwError('invalid value', { input: value }); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
// Split it into a whole and fractional part
|
2017-04-05 00:22:53 +03:00
|
|
|
var comps = value.split('.');
|
2018-01-10 02:25:44 +03:00
|
|
|
if (comps.length > 2) { throwError('too many decimal points', { input: value }); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
var whole = comps[0], fraction = comps[1];
|
|
|
|
if (!whole) { whole = '0'; }
|
|
|
|
if (!fraction) { fraction = '0'; }
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
// Prevent underflow
|
|
|
|
if (fraction.length > unitInfo.decimals) {
|
|
|
|
throwError('too many decimal places', { input: value, decimals: fraction.length });
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
whole = bigNumberify(whole);
|
|
|
|
fraction = bigNumberify(fraction);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
var wei = (whole.mul(unitInfo.tenPower)).add(fraction);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
if (negative) { wei = wei.mul(negative1); }
|
|
|
|
|
|
|
|
return wei;
|
|
|
|
}
|
|
|
|
|
2018-01-10 02:25:44 +03:00
|
|
|
function formatEther(wei, options) {
|
|
|
|
return formatUnits(wei, 18, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseEther(ether) {
|
|
|
|
return parseUnits(ether, 18);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
function convert(value, fromUnit, toUnit) {
|
|
|
|
var fromUnitInfo = getUnitInfo(fromUnit);
|
|
|
|
if (!fromUnitInfo) { throwError('invalid unit name', { unitType: fromUnit }); }
|
|
|
|
var toUnitInfo = getUnitInfo(toUnit);
|
|
|
|
if (!fromUnitInfo) { throwError('invalid unit name', { unitType: toUnit }); }
|
|
|
|
// @TODO: Is
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-08-24 02:35:19 +03:00
|
|
|
module.exports = {
|
|
|
|
formatEther: formatEther,
|
|
|
|
parseEther: parseEther,
|
2018-01-10 02:25:44 +03:00
|
|
|
|
|
|
|
formatUnits: formatUnits,
|
|
|
|
parseUnits: parseUnits,
|
|
|
|
|
|
|
|
// convert: convert,
|
2016-08-24 02:35:19 +03:00
|
|
|
}
|