2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BigNumber
|
|
|
|
*
|
|
|
|
* A wrapper around the BN.js object. We use the BN.js library
|
2020-11-18 17:25:58 -05:00
|
|
|
* because it is used by elliptic, so it is required regardless.
|
2019-05-14 18:25:46 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-11-14 17:42:36 -05:00
|
|
|
import _BN from "bn.js";
|
|
|
|
import BN = _BN.BN;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
import { Bytes, Hexable, hexlify, isBytes, isHexString } from "@ethersproject/bytes";
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
const _constructorGuard = { };
|
|
|
|
|
|
|
|
const MAX_SAFE = 0x1fffffffffffff;
|
|
|
|
|
|
|
|
|
2021-04-22 22:34:06 -04:00
|
|
|
export type BigNumberish = BigNumber | Bytes | bigint | string | number;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
export function isBigNumberish(value: any): value is BigNumberish {
|
|
|
|
return (value != null) && (
|
|
|
|
BigNumber.isBigNumber(value) ||
|
|
|
|
(typeof(value) === "number" && (value % 1) === 0) ||
|
|
|
|
(typeof(value) === "string" && !!value.match(/^-?[0-9]+$/)) ||
|
|
|
|
isHexString(value) ||
|
|
|
|
(typeof(value) === "bigint") ||
|
|
|
|
isBytes(value)
|
|
|
|
);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-11-22 16:34:07 -05:00
|
|
|
// Only warn about passing 10 into radix once
|
|
|
|
let _warnedToStringRadix = false;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class BigNumber implements Hexable {
|
|
|
|
readonly _hex: string;
|
2019-06-10 22:25:46 -04:00
|
|
|
readonly _isBigNumber: boolean;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
constructor(constructorGuard: any, hex: string) {
|
|
|
|
if (constructorGuard !== _constructorGuard) {
|
2020-04-22 02:42:25 -04:00
|
|
|
logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:25:46 -04:00
|
|
|
operation: "new (BigNumber)"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
this._hex = hex;
|
|
|
|
this._isBigNumber = true;
|
|
|
|
|
|
|
|
Object.freeze(this);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fromTwos(value: number): BigNumber {
|
|
|
|
return toBigNumber(toBN(this).fromTwos(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
toTwos(value: number): BigNumber {
|
|
|
|
return toBigNumber(toBN(this).toTwos(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
abs(): BigNumber {
|
|
|
|
if (this._hex[0] === "-") {
|
|
|
|
return BigNumber.from(this._hex.substring(1));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(other: BigNumberish): BigNumber {
|
|
|
|
return toBigNumber(toBN(this).add(toBN(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub(other: BigNumberish): BigNumber {
|
|
|
|
return toBigNumber(toBN(this).sub(toBN(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
div(other: BigNumberish): BigNumber {
|
2019-11-01 23:33:51 +09:00
|
|
|
const o = BigNumber.from(other);
|
2019-05-14 18:25:46 -04:00
|
|
|
if (o.isZero()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("division-by-zero", "div");
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).div(toBN(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mul(other: BigNumberish): BigNumber {
|
|
|
|
return toBigNumber(toBN(this).mul(toBN(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mod(other: BigNumberish): BigNumber {
|
2020-04-15 15:39:26 -04:00
|
|
|
const value = toBN(other);
|
|
|
|
if (value.isNeg()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("division-by-zero", "mod");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).umod(value));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pow(other: BigNumberish): BigNumber {
|
2020-07-04 23:41:05 -04:00
|
|
|
const value = toBN(other);
|
|
|
|
if (value.isNeg()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("negative-power", "pow");
|
2020-07-04 23:41:05 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).pow(value));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:39:26 -04:00
|
|
|
and(other: BigNumberish): BigNumber {
|
|
|
|
const value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("unbound-bitwise-result", "and");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).and(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
or(other: BigNumberish): BigNumber {
|
|
|
|
const value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("unbound-bitwise-result", "or");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).or(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
xor(other: BigNumberish): BigNumber {
|
|
|
|
const value = toBN(other);
|
|
|
|
if (this.isNegative() || value.isNeg()) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("unbound-bitwise-result", "xor");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).xor(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
mask(value: number): BigNumber {
|
|
|
|
if (this.isNegative() || value < 0) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("negative-width", "mask");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
return toBigNumber(toBN(this).maskn(value));
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:39:26 -04:00
|
|
|
shl(value: number): BigNumber {
|
|
|
|
if (this.isNegative() || value < 0) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("negative-width", "shl");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).shln(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
shr(value: number): BigNumber {
|
|
|
|
if (this.isNegative() || value < 0) {
|
2022-01-24 16:32:41 -05:00
|
|
|
throwFault("negative-width", "shr");
|
2020-04-15 15:39:26 -04:00
|
|
|
}
|
|
|
|
return toBigNumber(toBN(this).shrn(value));
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
eq(other: BigNumberish): boolean {
|
|
|
|
return toBN(this).eq(toBN(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
lt(other: BigNumberish): boolean {
|
|
|
|
return toBN(this).lt(toBN(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
lte(other: BigNumberish): boolean {
|
|
|
|
return toBN(this).lte(toBN(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
gt(other: BigNumberish): boolean {
|
|
|
|
return toBN(this).gt(toBN(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
gte(other: BigNumberish): boolean {
|
|
|
|
return toBN(this).gte(toBN(other));
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:39:26 -04:00
|
|
|
isNegative(): boolean {
|
|
|
|
return (this._hex[0] === "-");
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
isZero(): boolean {
|
|
|
|
return toBN(this).isZero();
|
|
|
|
}
|
|
|
|
|
|
|
|
toNumber(): number {
|
|
|
|
try {
|
|
|
|
return toBN(this).toNumber();
|
|
|
|
} catch (error) {
|
|
|
|
throwFault("overflow", "toNumber", this.toString());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:09:39 -04:00
|
|
|
toBigInt(): bigint {
|
2021-03-30 14:28:43 -04:00
|
|
|
try {
|
|
|
|
return BigInt(this.toString());
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
value: this.toString()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
toString(): string {
|
2020-11-22 16:34:07 -05:00
|
|
|
// Lots of people expect this, which we do not support, so check (See: #889)
|
|
|
|
if (arguments.length > 0) {
|
|
|
|
if (arguments[0] === 10) {
|
|
|
|
if (!_warnedToStringRadix) {
|
|
|
|
_warnedToStringRadix = true;
|
|
|
|
logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
|
|
|
|
}
|
|
|
|
} else if (arguments[0] === 16) {
|
|
|
|
logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, { });
|
|
|
|
} else {
|
2020-11-22 16:38:28 -05:00
|
|
|
logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, { });
|
2020-11-22 16:34:07 -05:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return toBN(this).toString(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
toHexString(): string {
|
|
|
|
return this._hex;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:15:03 -04:00
|
|
|
toJSON(key?: string): any {
|
|
|
|
return { type: "BigNumber", hex: this.toHexString() };
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
static from(value: any): BigNumber {
|
|
|
|
if (value instanceof BigNumber) { return value; }
|
|
|
|
|
|
|
|
if (typeof(value) === "string") {
|
2020-07-07 22:18:02 -04:00
|
|
|
if (value.match(/^-?0x[0-9a-f]+$/i)) {
|
2019-05-14 18:25:46 -04:00
|
|
|
return new BigNumber(_constructorGuard, toHex(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.match(/^-?[0-9]+$/)) {
|
2019-09-06 12:25:17 -04:00
|
|
|
return new BigNumber(_constructorGuard, toHex(new BN(value)));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
2019-05-14 18:25:46 -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));
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:15:03 -04:00
|
|
|
const anyValue = <any>value;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-08-24 23:15:03 -04:00
|
|
|
if (typeof(anyValue) === "bigint") {
|
|
|
|
return BigNumber.from(anyValue.toString());
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 23:15:03 -04:00
|
|
|
if (isBytes(anyValue)) {
|
|
|
|
return BigNumber.from(hexlify(anyValue));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 23:15:03 -04:00
|
|
|
if (anyValue) {
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// Hexable interface (takes priority)
|
2020-08-24 23:15:03 -04:00
|
|
|
if (anyValue.toHexString) {
|
|
|
|
const hex = anyValue.toHexString();
|
|
|
|
if (typeof(hex) === "string") {
|
|
|
|
return BigNumber.from(hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// For now, handle legacy JSON-ified values (goes away in v6)
|
|
|
|
let hex = anyValue._hex;
|
|
|
|
|
|
|
|
// New-form JSON
|
|
|
|
if (hex == null && anyValue.type === "BigNumber") {
|
|
|
|
hex = anyValue.hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(hex) === "string") {
|
|
|
|
if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
|
|
|
|
return BigNumber.from(hex);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static isBigNumber(value: any): value is BigNumber {
|
2019-06-10 22:25:46 -04:00
|
|
|
return !!(value && value._isBigNumber);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the hex string
|
2019-09-06 12:25:17 -04:00
|
|
|
function toHex(value: string | BN): string {
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// Cannot have multiple negative signs (e.g. "--0x04")
|
2019-08-01 18:04:06 -04:00
|
|
|
if (value[0] === "-") { logger.throwArgumentError("invalid hex", "value", value); }
|
2019-05-14 18:25:46 -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;
|
|
|
|
}
|
|
|
|
|
2019-09-06 12:25:17 -04:00
|
|
|
function toBigNumber(value: BN): BigNumber {
|
2019-05-14 18:25:46 -04:00
|
|
|
return BigNumber.from(toHex(value));
|
|
|
|
}
|
|
|
|
|
2019-09-06 12:25:17 -04:00
|
|
|
function toBN(value: BigNumberish): BN {
|
2019-11-01 23:33:51 +09:00
|
|
|
const hex = BigNumber.from(value).toHexString();
|
2019-05-14 18:25:46 -04:00
|
|
|
if (hex[0] === "-") {
|
2019-09-06 12:25:17 -04:00
|
|
|
return (new BN("-" + hex.substring(3), 16));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-09-06 12:25:17 -04:00
|
|
|
return new BN(hex.substring(2), 16);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function throwFault(fault: string, operation: string, value?: any): never {
|
2019-11-01 23:33:51 +09:00
|
|
|
const params: any = { fault: fault, operation: operation };
|
2019-05-14 18:25:46 -04:00
|
|
|
if (value != null) { params.value = value; }
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-11-14 17:42:36 -05:00
|
|
|
|
|
|
|
// value should have no prefix
|
|
|
|
export function _base36To16(value: string): string {
|
|
|
|
return (new BN(value, 36)).toString(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// value should have no prefix
|
|
|
|
export function _base16To36(value: string): string {
|
|
|
|
return (new BN(value, 16)).toString(36);
|
|
|
|
}
|