2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
*/
|
2019-09-06 17:48:26 -04:00
|
|
|
var bn_js_1 = require("bn.js");
|
2019-05-14 18:48:48 -04:00
|
|
|
var bytes_1 = require("@ethersproject/bytes");
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2019-05-14 18:48:48 -04:00
|
|
|
var _constructorGuard = {};
|
|
|
|
var MAX_SAFE = 0x1fffffffffffff;
|
2019-06-11 17:57:04 -04:00
|
|
|
function isBigNumberish(value) {
|
|
|
|
return (value != null) && (BigNumber.isBigNumber(value) ||
|
|
|
|
(typeof (value) === "number" && (value % 1) === 0) ||
|
|
|
|
(typeof (value) === "string" && !!value.match(/^-?[0-9]+$/)) ||
|
|
|
|
bytes_1.isHexString(value) ||
|
|
|
|
(typeof (value) === "bigint") ||
|
|
|
|
bytes_1.isBytes(value));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-06-11 17:57:04 -04:00
|
|
|
exports.isBigNumberish = isBigNumberish;
|
2019-05-14 18:48:48 -04:00
|
|
|
var BigNumber = /** @class */ (function () {
|
|
|
|
function BigNumber(constructorGuard, hex) {
|
|
|
|
var _newTarget = this.constructor;
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.checkNew(_newTarget, BigNumber);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
2020-04-23 23:35:39 -04:00
|
|
|
logger.throwError("cannot call constructor directly; use BigNumber.from", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:48:48 -04:00
|
|
|
operation: "new (BigNumber)"
|
|
|
|
});
|
|
|
|
}
|
2019-06-11 17:57:04 -04:00
|
|
|
this._hex = hex;
|
|
|
|
this._isBigNumber = true;
|
|
|
|
Object.freeze(this);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
BigNumber.prototype.fromTwos = function (value) {
|
|
|
|
return toBigNumber(toBN(this).fromTwos(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.toTwos = function (value) {
|
|
|
|
return toBigNumber(toBN(this).toTwos(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.abs = function () {
|
|
|
|
if (this._hex[0] === "-") {
|
|
|
|
return BigNumber.from(this._hex.substring(1));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
BigNumber.prototype.add = function (other) {
|
|
|
|
return toBigNumber(toBN(this).add(toBN(other)));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.sub = function (other) {
|
|
|
|
return toBigNumber(toBN(this).sub(toBN(other)));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.div = function (other) {
|
|
|
|
var o = BigNumber.from(other);
|
|
|
|
if (o.isZero()) {
|
|
|
|
throwFault("division by zero", "div");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).div(toBN(other)));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.mul = function (other) {
|
|
|
|
return toBigNumber(toBN(this).mul(toBN(other)));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.mod = function (other) {
|
2020-04-15 18:28:04 -04:00
|
|
|
var value = toBN(other);
|
|
|
|
if (value.isNeg()) {
|
|
|
|
throwFault("cannot modulo negative values", "mod");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).umod(value));
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
BigNumber.prototype.pow = function (other) {
|
|
|
|
return toBigNumber(toBN(this).pow(toBN(other)));
|
|
|
|
};
|
2020-04-15 18:28:04 -04:00
|
|
|
BigNumber.prototype.and = function (other) {
|
|
|
|
var value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
|
|
|
throwFault("cannot 'and' negative values", "and");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).and(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.or = function (other) {
|
|
|
|
var value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
|
|
|
throwFault("cannot 'or' negative values", "or");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).or(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.xor = function (other) {
|
|
|
|
var value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
|
|
|
throwFault("cannot 'xor' negative values", "xor");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).xor(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.mask = function (value) {
|
|
|
|
if (this.isNegative() || value < 0) {
|
|
|
|
throwFault("cannot mask negative values", "mask");
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
return toBigNumber(toBN(this).maskn(value));
|
|
|
|
};
|
2020-04-15 18:28:04 -04:00
|
|
|
BigNumber.prototype.shl = function (value) {
|
|
|
|
if (this.isNegative() || value < 0) {
|
|
|
|
throwFault("cannot shift negative values", "shl");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).shln(value));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.shr = function (value) {
|
|
|
|
if (this.isNegative() || value < 0) {
|
|
|
|
throwFault("cannot shift negative values", "shr");
|
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).shrn(value));
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
BigNumber.prototype.eq = function (other) {
|
|
|
|
return toBN(this).eq(toBN(other));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.lt = function (other) {
|
|
|
|
return toBN(this).lt(toBN(other));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.lte = function (other) {
|
|
|
|
return toBN(this).lte(toBN(other));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.gt = function (other) {
|
|
|
|
return toBN(this).gt(toBN(other));
|
|
|
|
};
|
|
|
|
BigNumber.prototype.gte = function (other) {
|
|
|
|
return toBN(this).gte(toBN(other));
|
|
|
|
};
|
2020-04-15 18:28:04 -04:00
|
|
|
BigNumber.prototype.isNegative = function () {
|
|
|
|
return (this._hex[0] === "-");
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
BigNumber.prototype.isZero = function () {
|
|
|
|
return toBN(this).isZero();
|
|
|
|
};
|
|
|
|
BigNumber.prototype.toNumber = function () {
|
|
|
|
try {
|
|
|
|
return toBN(this).toNumber();
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
throwFault("overflow", "toNumber", this.toString());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
BigNumber.prototype.toString = function () {
|
|
|
|
// Lots of people expect this, which we do not support, so check
|
|
|
|
if (arguments.length !== 0) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("bigNumber.toString does not accept parameters", logger_1.Logger.errors.UNEXPECTED_ARGUMENT, {});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return toBN(this).toString(10);
|
|
|
|
};
|
|
|
|
BigNumber.prototype.toHexString = function () {
|
|
|
|
return this._hex;
|
|
|
|
};
|
|
|
|
BigNumber.from = function (value) {
|
|
|
|
if (value instanceof BigNumber) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (typeof (value) === "string") {
|
|
|
|
if (value.match(/-?0x[0-9a-f]+/i)) {
|
|
|
|
return new BigNumber(_constructorGuard, toHex(value));
|
|
|
|
}
|
|
|
|
if (value.match(/^-?[0-9]+$/)) {
|
2019-09-06 17:48:26 -04:00
|
|
|
return new BigNumber(_constructorGuard, toHex(new bn_js_1.BN(value)));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (typeof (value) === "number") {
|
|
|
|
if (value % 1) {
|
|
|
|
throwFault("underflow", "BigNumber.from", value);
|
|
|
|
}
|
|
|
|
if (value >= MAX_SAFE || value <= -MAX_SAFE) {
|
|
|
|
throwFault("overflow", "BigNumber.from", value);
|
|
|
|
}
|
|
|
|
return BigNumber.from(String(value));
|
|
|
|
}
|
|
|
|
if (typeof (value) === "bigint") {
|
|
|
|
return BigNumber.from(value.toString());
|
|
|
|
}
|
|
|
|
if (bytes_1.isBytes(value)) {
|
|
|
|
return BigNumber.from(bytes_1.hexlify(value));
|
|
|
|
}
|
|
|
|
if (value._hex && bytes_1.isHexString(value._hex)) {
|
|
|
|
return BigNumber.from(value._hex);
|
|
|
|
}
|
|
|
|
if (value.toHexString) {
|
|
|
|
value = value.toHexString();
|
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return BigNumber.from(value);
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
BigNumber.isBigNumber = function (value) {
|
2019-06-11 17:57:04 -04:00
|
|
|
return !!(value && value._isBigNumber);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
return BigNumber;
|
|
|
|
}());
|
|
|
|
exports.BigNumber = BigNumber;
|
|
|
|
// Normalize the hex string
|
|
|
|
function toHex(value) {
|
|
|
|
// For BN, call on the hex string
|
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
return toHex(value.toString(16));
|
|
|
|
}
|
|
|
|
// If negative, prepend the negative sign to the normalized positive value
|
|
|
|
if (value[0] === "-") {
|
|
|
|
// Strip off the negative sign
|
|
|
|
value = value.substring(1);
|
|
|
|
// Cannot have mulitple negative signs (e.g. "--0x04")
|
|
|
|
if (value[0] === "-") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid hex", "value", value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// Call toHex on the positive component
|
|
|
|
value = toHex(value);
|
|
|
|
// Do not allow "-0x00"
|
|
|
|
if (value === "0x00") {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
// Negate the value
|
|
|
|
return "-" + value;
|
|
|
|
}
|
|
|
|
// Add a "0x" prefix if missing
|
|
|
|
if (value.substring(0, 2) !== "0x") {
|
|
|
|
value = "0x" + value;
|
|
|
|
}
|
|
|
|
// Normalize zero
|
|
|
|
if (value === "0x") {
|
|
|
|
return "0x00";
|
|
|
|
}
|
|
|
|
// Make the string even length
|
|
|
|
if (value.length % 2) {
|
|
|
|
value = "0x0" + value.substring(2);
|
|
|
|
}
|
|
|
|
// Trim to smallest even-length string
|
|
|
|
while (value.length > 4 && value.substring(0, 4) === "0x00") {
|
|
|
|
value = "0x" + value.substring(4);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
function toBigNumber(value) {
|
|
|
|
return BigNumber.from(toHex(value));
|
|
|
|
}
|
|
|
|
function toBN(value) {
|
|
|
|
var hex = BigNumber.from(value).toHexString();
|
|
|
|
if (hex[0] === "-") {
|
2019-09-06 17:48:26 -04:00
|
|
|
return (new bn_js_1.BN("-" + hex.substring(3), 16));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-09-06 17:48:26 -04:00
|
|
|
return new bn_js_1.BN(hex.substring(2), 16);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
function throwFault(fault, operation, value) {
|
|
|
|
var params = { fault: fault, operation: operation };
|
|
|
|
if (value != null) {
|
|
|
|
params.value = value;
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
return logger.throwError(fault, logger_1.Logger.errors.NUMERIC_FAULT, params);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|