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);
|
|
|
|
var tenPower18 = new bigNumberify('1000000000000000000');
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
function formatEther(wei, options) {
|
2017-02-24 22:41:24 +03:00
|
|
|
wei = bigNumberify(wei);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
|
|
|
var negative = wei.lt(zero);
|
|
|
|
if (negative) { wei = wei.mul(negative1); }
|
|
|
|
|
|
|
|
var fraction = wei.mod(tenPower18).toString(10);
|
|
|
|
while (fraction.length < 18) { fraction = '0' + fraction; }
|
|
|
|
|
|
|
|
if (!options.pad) {
|
|
|
|
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
var whole = wei.div(tenPower18).toString(10);
|
|
|
|
|
|
|
|
if (options.commify) {
|
|
|
|
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = whole + '.' + fraction;
|
|
|
|
|
|
|
|
if (negative) { value = '-' + value; }
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseEther(ether) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (typeof(ether) !== 'string' || !ether.match(/^-?[0-9.,]+$/)) {
|
2017-04-05 00:22:53 +03:00
|
|
|
throwError('invalid value', { input: ether });
|
2016-08-24 02:35:19 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 00:22:53 +03:00
|
|
|
var value = ether.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
|
|
|
|
2017-04-05 00:22:53 +03:00
|
|
|
if (value === '.') { throwError('invalid value', { input: ether }); }
|
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('.');
|
|
|
|
if (comps.length > 2) { throwError('too many decimal points', { input: ether }); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
var whole = comps[0], fraction = comps[1];
|
|
|
|
if (!whole) { whole = '0'; }
|
|
|
|
if (!fraction) { fraction = '0'; }
|
2017-04-05 00:22:53 +03:00
|
|
|
if (fraction.length > 18) { throwError('too many decimal places', { input: ether, decimals: fraction.length }); }
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
while (fraction.length < 18) { fraction += '0'; }
|
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
whole = bigNumberify(whole);
|
|
|
|
fraction = bigNumberify(fraction);
|
2016-08-24 02:35:19 +03:00
|
|
|
|
|
|
|
var wei = (whole.mul(tenPower18)).add(fraction);
|
|
|
|
|
|
|
|
if (negative) { wei = wei.mul(negative1); }
|
|
|
|
|
|
|
|
return wei;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
formatEther: formatEther,
|
|
|
|
parseEther: parseEther,
|
|
|
|
}
|