2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
2018-07-16 10:59:25 +03:00
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
|
|
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
|
|
return function (d, b) {
|
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
2018-06-13 22:39:39 +03:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
/**
|
|
|
|
* BigNumber
|
|
|
|
*
|
|
|
|
* A wrapper around the BN.js object. We use the BN.js library
|
|
|
|
* because it is used by elliptic, so it is required regardles.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
var bn_js_1 = __importDefault(require("bn.js"));
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-06-17 23:32:57 +03:00
|
|
|
var properties_1 = require("./properties");
|
2018-07-16 10:59:25 +03:00
|
|
|
var types_1 = require("./types");
|
2018-07-13 03:14:04 +03:00
|
|
|
var errors = __importStar(require("./errors"));
|
|
|
|
var BN_1 = new bn_js_1.default.BN(-1);
|
|
|
|
function toHex(bn) {
|
2018-06-23 03:30:50 +03:00
|
|
|
var value = bn.toString(16);
|
|
|
|
if (value[0] === '-') {
|
2018-07-13 03:14:04 +03:00
|
|
|
if ((value.length % 2) === 0) {
|
|
|
|
return '-0x0' + value.substring(1);
|
|
|
|
}
|
|
|
|
return "-0x" + value.substring(1);
|
2018-06-23 03:30:50 +03:00
|
|
|
}
|
2018-07-13 03:14:04 +03:00
|
|
|
if ((value.length % 2) === 1) {
|
|
|
|
return '0x0' + value;
|
|
|
|
}
|
|
|
|
return '0x' + value;
|
|
|
|
}
|
|
|
|
function toBN(value) {
|
|
|
|
return bigNumberify(value)._bn;
|
2018-06-23 03:30:50 +03:00
|
|
|
}
|
2018-07-13 03:14:04 +03:00
|
|
|
function toBigNumber(bn) {
|
2018-07-16 10:59:25 +03:00
|
|
|
return new BigNumber(toHex(bn));
|
2018-07-13 03:14:04 +03:00
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
var BigNumber = /** @class */ (function (_super) {
|
|
|
|
__extends(BigNumber, _super);
|
|
|
|
function BigNumber(value) {
|
|
|
|
var _this = _super.call(this) || this;
|
|
|
|
errors.checkNew(_this, BigNumber);
|
2018-06-13 22:39:39 +03:00
|
|
|
if (typeof (value) === 'string') {
|
2018-06-17 23:47:28 +03:00
|
|
|
if (bytes_1.isHexString(value)) {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (value == '0x') {
|
|
|
|
value = '0x0';
|
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', value);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:47:28 +03:00
|
|
|
else if (value[0] === '-' && bytes_1.isHexString(value.substring(1))) {
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', value);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (value.match(/^-?[0-9]*$/)) {
|
|
|
|
if (value == '') {
|
|
|
|
value = '0';
|
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', toHex(new bn_js_1.default.BN(value)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-07-03 22:48:37 +03:00
|
|
|
else {
|
|
|
|
errors.throwError('invalid BigNumber string value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (typeof (value) === 'number') {
|
2018-06-23 03:30:50 +03:00
|
|
|
if (parseInt(String(value)) !== value) {
|
|
|
|
errors.throwError('underflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'underflow', value: value, outputValue: parseInt(String(value)) });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
try {
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', toHex(new bn_js_1.default.BN(value)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
errors.throwError('overflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'overflow', details: error.message });
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 11:00:56 +03:00
|
|
|
else if (value instanceof BigNumber) {
|
|
|
|
properties_1.defineReadOnly(_this, '_hex', value._hex);
|
2018-07-13 03:14:04 +03:00
|
|
|
}
|
|
|
|
else if (value.toHexString) {
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', toHex(toBN(value.toHexString())));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:47:28 +03:00
|
|
|
else if (bytes_1.isArrayish(value)) {
|
2018-07-16 10:59:25 +03:00
|
|
|
properties_1.defineReadOnly(_this, '_hex', toHex(new bn_js_1.default.BN(bytes_1.hexlify(value).substring(2), 16)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
errors.throwError('invalid BigNumber value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
|
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
return _this;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
Object.defineProperty(BigNumber.prototype, "_bn", {
|
2018-07-13 03:14:04 +03:00
|
|
|
get: function () {
|
|
|
|
if (this._hex[0] === '-') {
|
|
|
|
return (new bn_js_1.default.BN(this._hex.substring(3), 16)).mul(BN_1);
|
|
|
|
}
|
|
|
|
return new bn_js_1.default.BN(this._hex.substring(2), 16);
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.fromTwos = function (value) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.fromTwos(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.toTwos = function (value) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.toTwos(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.add = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.add(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.sub = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.sub(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.div = function (other) {
|
2018-06-23 03:30:50 +03:00
|
|
|
var o = bigNumberify(other);
|
2018-06-13 22:39:39 +03:00
|
|
|
if (o.isZero()) {
|
|
|
|
errors.throwError('division by zero', errors.NUMERIC_FAULT, { operation: 'divide', fault: 'division by zero' });
|
|
|
|
}
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.div(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.mul = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.mul(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.mod = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.mod(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.pow = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.pow(toBN(other)));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.maskn = function (value) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return toBigNumber(this._bn.maskn(value));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.eq = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._bn.eq(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.lt = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._bn.lt(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.lte = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._bn.lte(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.gt = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._bn.gt(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.gte = function (other) {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._bn.gte(toBN(other));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.isZero = function () {
|
2018-06-13 22:39:39 +03:00
|
|
|
return this._bn.isZero();
|
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.toNumber = function () {
|
2018-06-13 22:39:39 +03:00
|
|
|
try {
|
|
|
|
return this._bn.toNumber();
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
errors.throwError('overflow', errors.NUMERIC_FAULT, { operation: 'setValue', fault: 'overflow', details: error.message });
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.toString = function () {
|
2018-06-13 22:39:39 +03:00
|
|
|
return this._bn.toString(10);
|
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
BigNumber.prototype.toHexString = function () {
|
2018-07-13 03:14:04 +03:00
|
|
|
return this._hex;
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
2018-07-16 10:59:25 +03:00
|
|
|
return BigNumber;
|
|
|
|
}(types_1.BigNumber));
|
2018-06-13 22:39:39 +03:00
|
|
|
function bigNumberify(value) {
|
2018-07-16 10:59:25 +03:00
|
|
|
if (value instanceof BigNumber) {
|
2018-06-13 22:39:39 +03:00
|
|
|
return value;
|
|
|
|
}
|
2018-07-16 10:59:25 +03:00
|
|
|
return new BigNumber(value);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
exports.bigNumberify = bigNumberify;
|
|
|
|
exports.ConstantNegativeOne = bigNumberify(-1);
|
|
|
|
exports.ConstantZero = bigNumberify(0);
|
|
|
|
exports.ConstantOne = bigNumberify(1);
|
|
|
|
exports.ConstantTwo = bigNumberify(2);
|
2018-06-23 03:30:50 +03:00
|
|
|
exports.ConstantWeiPerEther = bigNumberify('1000000000000000000');
|