Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export const version = "bignumber/5.0.8";
|
||||
//# sourceMappingURL=_version.js.map
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
"use strict";
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
import { BN } from "bn.js";
|
||||
import { hexlify, isBytes, isHexString } from "@ethersproject/bytes";
|
||||
import { Logger } from "@ethersproject/logger";
|
||||
import { version } from "./_version";
|
||||
const logger = new Logger(version);
|
||||
const _constructorGuard = {};
|
||||
const MAX_SAFE = 0x1fffffffffffff;
|
||||
export function isBigNumberish(value) {
|
||||
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));
|
||||
}
|
||||
export class BigNumber {
|
||||
constructor(constructorGuard, hex) {
|
||||
logger.checkNew(new.target, BigNumber);
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new (BigNumber)"
|
||||
});
|
||||
}
|
||||
this._hex = hex;
|
||||
this._isBigNumber = true;
|
||||
Object.freeze(this);
|
||||
}
|
||||
fromTwos(value) {
|
||||
return toBigNumber(toBN(this).fromTwos(value));
|
||||
}
|
||||
toTwos(value) {
|
||||
return toBigNumber(toBN(this).toTwos(value));
|
||||
}
|
||||
abs() {
|
||||
if (this._hex[0] === "-") {
|
||||
return BigNumber.from(this._hex.substring(1));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
add(other) {
|
||||
return toBigNumber(toBN(this).add(toBN(other)));
|
||||
}
|
||||
sub(other) {
|
||||
return toBigNumber(toBN(this).sub(toBN(other)));
|
||||
}
|
||||
div(other) {
|
||||
const o = BigNumber.from(other);
|
||||
if (o.isZero()) {
|
||||
throwFault("division by zero", "div");
|
||||
}
|
||||
return toBigNumber(toBN(this).div(toBN(other)));
|
||||
}
|
||||
mul(other) {
|
||||
return toBigNumber(toBN(this).mul(toBN(other)));
|
||||
}
|
||||
mod(other) {
|
||||
const value = toBN(other);
|
||||
if (value.isNeg()) {
|
||||
throwFault("cannot modulo negative values", "mod");
|
||||
}
|
||||
return toBigNumber(toBN(this).umod(value));
|
||||
}
|
||||
pow(other) {
|
||||
const value = toBN(other);
|
||||
if (value.isNeg()) {
|
||||
throwFault("cannot raise to negative values", "pow");
|
||||
}
|
||||
return toBigNumber(toBN(this).pow(value));
|
||||
}
|
||||
and(other) {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'and' negative values", "and");
|
||||
}
|
||||
return toBigNumber(toBN(this).and(value));
|
||||
}
|
||||
or(other) {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'or' negative values", "or");
|
||||
}
|
||||
return toBigNumber(toBN(this).or(value));
|
||||
}
|
||||
xor(other) {
|
||||
const value = toBN(other);
|
||||
if (this.isNegative() || value.isNeg()) {
|
||||
throwFault("cannot 'xor' negative values", "xor");
|
||||
}
|
||||
return toBigNumber(toBN(this).xor(value));
|
||||
}
|
||||
mask(value) {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot mask negative values", "mask");
|
||||
}
|
||||
return toBigNumber(toBN(this).maskn(value));
|
||||
}
|
||||
shl(value) {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot shift negative values", "shl");
|
||||
}
|
||||
return toBigNumber(toBN(this).shln(value));
|
||||
}
|
||||
shr(value) {
|
||||
if (this.isNegative() || value < 0) {
|
||||
throwFault("cannot shift negative values", "shr");
|
||||
}
|
||||
return toBigNumber(toBN(this).shrn(value));
|
||||
}
|
||||
eq(other) {
|
||||
return toBN(this).eq(toBN(other));
|
||||
}
|
||||
lt(other) {
|
||||
return toBN(this).lt(toBN(other));
|
||||
}
|
||||
lte(other) {
|
||||
return toBN(this).lte(toBN(other));
|
||||
}
|
||||
gt(other) {
|
||||
return toBN(this).gt(toBN(other));
|
||||
}
|
||||
gte(other) {
|
||||
return toBN(this).gte(toBN(other));
|
||||
}
|
||||
isNegative() {
|
||||
return (this._hex[0] === "-");
|
||||
}
|
||||
isZero() {
|
||||
return toBN(this).isZero();
|
||||
}
|
||||
toNumber() {
|
||||
try {
|
||||
return toBN(this).toNumber();
|
||||
}
|
||||
catch (error) {
|
||||
throwFault("overflow", "toNumber", this.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
toString() {
|
||||
// Lots of people expect this, which we do not support, so check
|
||||
if (arguments.length !== 0) {
|
||||
logger.throwError("bigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
|
||||
}
|
||||
return toBN(this).toString(10);
|
||||
}
|
||||
toHexString() {
|
||||
return this._hex;
|
||||
}
|
||||
toJSON(key) {
|
||||
return { type: "BigNumber", hex: this.toHexString() };
|
||||
}
|
||||
static from(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]+$/)) {
|
||||
return new BigNumber(_constructorGuard, toHex(new BN(value)));
|
||||
}
|
||||
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
||||
}
|
||||
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));
|
||||
}
|
||||
const anyValue = value;
|
||||
if (typeof (anyValue) === "bigint") {
|
||||
return BigNumber.from(anyValue.toString());
|
||||
}
|
||||
if (isBytes(anyValue)) {
|
||||
return BigNumber.from(hexlify(anyValue));
|
||||
}
|
||||
if (anyValue) {
|
||||
// Hexable interface (takes piority)
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
||||
}
|
||||
static isBigNumber(value) {
|
||||
return !!(value && value._isBigNumber);
|
||||
}
|
||||
}
|
||||
// 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] === "-") {
|
||||
logger.throwArgumentError("invalid hex", "value", value);
|
||||
}
|
||||
// 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) {
|
||||
const hex = BigNumber.from(value).toHexString();
|
||||
if (hex[0] === "-") {
|
||||
return (new BN("-" + hex.substring(3), 16));
|
||||
}
|
||||
return new BN(hex.substring(2), 16);
|
||||
}
|
||||
function throwFault(fault, operation, value) {
|
||||
const params = { fault: fault, operation: operation };
|
||||
if (value != null) {
|
||||
params.value = value;
|
||||
}
|
||||
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
|
||||
}
|
||||
//# sourceMappingURL=bignumber.js.map
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
"use strict";
|
||||
import { arrayify, hexZeroPad, isBytes } from "@ethersproject/bytes";
|
||||
import { Logger } from "@ethersproject/logger";
|
||||
import { version } from "./_version";
|
||||
const logger = new Logger(version);
|
||||
import { BigNumber, isBigNumberish } from "./bignumber";
|
||||
const _constructorGuard = {};
|
||||
const Zero = BigNumber.from(0);
|
||||
const NegativeOne = BigNumber.from(-1);
|
||||
function throwFault(message, fault, operation, value) {
|
||||
const params = { fault: fault, operation: operation };
|
||||
if (value !== undefined) {
|
||||
params.value = value;
|
||||
}
|
||||
return logger.throwError(message, Logger.errors.NUMERIC_FAULT, params);
|
||||
}
|
||||
// Constant to pull zeros from for multipliers
|
||||
let zeros = "0";
|
||||
while (zeros.length < 256) {
|
||||
zeros += zeros;
|
||||
}
|
||||
// Returns a string "1" followed by decimal "0"s
|
||||
function getMultiplier(decimals) {
|
||||
if (typeof (decimals) !== "number") {
|
||||
try {
|
||||
decimals = BigNumber.from(decimals).toNumber();
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (typeof (decimals) === "number" && decimals >= 0 && decimals <= 256 && !(decimals % 1)) {
|
||||
return ("1" + zeros.substring(0, decimals));
|
||||
}
|
||||
return logger.throwArgumentError("invalid decimal size", "decimals", decimals);
|
||||
}
|
||||
export function formatFixed(value, decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
const multiplier = getMultiplier(decimals);
|
||||
// Make sure wei is a big number (convert as necessary)
|
||||
value = BigNumber.from(value);
|
||||
const negative = value.lt(Zero);
|
||||
if (negative) {
|
||||
value = value.mul(NegativeOne);
|
||||
}
|
||||
let fraction = value.mod(multiplier).toString();
|
||||
while (fraction.length < multiplier.length - 1) {
|
||||
fraction = "0" + fraction;
|
||||
}
|
||||
// Strip training 0
|
||||
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
||||
const whole = value.div(multiplier).toString();
|
||||
value = whole + "." + fraction;
|
||||
if (negative) {
|
||||
value = "-" + value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
export function parseFixed(value, decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
const multiplier = getMultiplier(decimals);
|
||||
if (typeof (value) !== "string" || !value.match(/^-?[0-9.,]+$/)) {
|
||||
logger.throwArgumentError("invalid decimal value", "value", value);
|
||||
}
|
||||
if (multiplier.length - 1 === 0) {
|
||||
return BigNumber.from(value);
|
||||
}
|
||||
// Is it negative?
|
||||
const negative = (value.substring(0, 1) === "-");
|
||||
if (negative) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
if (value === ".") {
|
||||
logger.throwArgumentError("missing value", "value", value);
|
||||
}
|
||||
// Split it into a whole and fractional part
|
||||
const comps = value.split(".");
|
||||
if (comps.length > 2) {
|
||||
logger.throwArgumentError("too many decimal points", "value", value);
|
||||
}
|
||||
let whole = comps[0], fraction = comps[1];
|
||||
if (!whole) {
|
||||
whole = "0";
|
||||
}
|
||||
if (!fraction) {
|
||||
fraction = "0";
|
||||
}
|
||||
// Prevent underflow
|
||||
if (fraction.length > multiplier.length - 1) {
|
||||
throwFault("fractional component exceeds decimals", "underflow", "parseFixed");
|
||||
}
|
||||
// Fully pad the string with zeros to get to wei
|
||||
while (fraction.length < multiplier.length - 1) {
|
||||
fraction += "0";
|
||||
}
|
||||
const wholeValue = BigNumber.from(whole);
|
||||
const fractionValue = BigNumber.from(fraction);
|
||||
let wei = (wholeValue.mul(multiplier)).add(fractionValue);
|
||||
if (negative) {
|
||||
wei = wei.mul(NegativeOne);
|
||||
}
|
||||
return wei;
|
||||
}
|
||||
export class FixedFormat {
|
||||
constructor(constructorGuard, signed, width, decimals) {
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot use FixedFormat constructor; use FixedFormat.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new FixedFormat"
|
||||
});
|
||||
}
|
||||
this.signed = signed;
|
||||
this.width = width;
|
||||
this.decimals = decimals;
|
||||
this.name = (signed ? "" : "u") + "fixed" + String(width) + "x" + String(decimals);
|
||||
this._multiplier = getMultiplier(decimals);
|
||||
Object.freeze(this);
|
||||
}
|
||||
static from(value) {
|
||||
if (value instanceof FixedFormat) {
|
||||
return value;
|
||||
}
|
||||
let signed = true;
|
||||
let width = 128;
|
||||
let decimals = 18;
|
||||
if (typeof (value) === "string") {
|
||||
if (value === "fixed") {
|
||||
// defaults...
|
||||
}
|
||||
else if (value === "ufixed") {
|
||||
signed = false;
|
||||
}
|
||||
else if (value != null) {
|
||||
const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
||||
if (!match) {
|
||||
logger.throwArgumentError("invalid fixed format", "format", value);
|
||||
}
|
||||
signed = (match[1] !== "u");
|
||||
width = parseInt(match[2]);
|
||||
decimals = parseInt(match[3]);
|
||||
}
|
||||
}
|
||||
else if (value) {
|
||||
const check = (key, type, defaultValue) => {
|
||||
if (value[key] == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (typeof (value[key]) !== type) {
|
||||
logger.throwArgumentError("invalid fixed format (" + key + " not " + type + ")", "format." + key, value[key]);
|
||||
}
|
||||
return value[key];
|
||||
};
|
||||
signed = check("signed", "boolean", signed);
|
||||
width = check("width", "number", width);
|
||||
decimals = check("decimals", "number", decimals);
|
||||
}
|
||||
if (width % 8) {
|
||||
logger.throwArgumentError("invalid fixed format width (not byte aligned)", "format.width", width);
|
||||
}
|
||||
if (decimals > 80) {
|
||||
logger.throwArgumentError("invalid fixed format (decimals too large)", "format.decimals", decimals);
|
||||
}
|
||||
return new FixedFormat(_constructorGuard, signed, width, decimals);
|
||||
}
|
||||
}
|
||||
export class FixedNumber {
|
||||
constructor(constructorGuard, hex, value, format) {
|
||||
logger.checkNew(new.target, FixedNumber);
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot use FixedNumber constructor; use FixedNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new FixedFormat"
|
||||
});
|
||||
}
|
||||
this.format = format;
|
||||
this._hex = hex;
|
||||
this._value = value;
|
||||
this._isFixedNumber = true;
|
||||
Object.freeze(this);
|
||||
}
|
||||
_checkFormat(other) {
|
||||
if (this.format.name !== other.format.name) {
|
||||
logger.throwArgumentError("incompatible format; use fixedNumber.toFormat", "other", other);
|
||||
}
|
||||
}
|
||||
addUnsafe(other) {
|
||||
this._checkFormat(other);
|
||||
const a = parseFixed(this._value, this.format.decimals);
|
||||
const b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.add(b), this.format.decimals, this.format);
|
||||
}
|
||||
subUnsafe(other) {
|
||||
this._checkFormat(other);
|
||||
const a = parseFixed(this._value, this.format.decimals);
|
||||
const b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.sub(b), this.format.decimals, this.format);
|
||||
}
|
||||
mulUnsafe(other) {
|
||||
this._checkFormat(other);
|
||||
const a = parseFixed(this._value, this.format.decimals);
|
||||
const b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.mul(b).div(this.format._multiplier), this.format.decimals, this.format);
|
||||
}
|
||||
divUnsafe(other) {
|
||||
this._checkFormat(other);
|
||||
const a = parseFixed(this._value, this.format.decimals);
|
||||
const b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.mul(this.format._multiplier).div(b), this.format.decimals, this.format);
|
||||
}
|
||||
floor() {
|
||||
let comps = this.toString().split(".");
|
||||
let result = FixedNumber.from(comps[0], this.format);
|
||||
const hasFraction = !comps[1].match(/^(0*)$/);
|
||||
if (this.isNegative() && hasFraction) {
|
||||
result = result.subUnsafe(ONE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
ceiling() {
|
||||
let comps = this.toString().split(".");
|
||||
let result = FixedNumber.from(comps[0], this.format);
|
||||
const hasFraction = !comps[1].match(/^(0*)$/);
|
||||
if (!this.isNegative() && hasFraction) {
|
||||
result = result.addUnsafe(ONE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// @TODO: Support other rounding algorithms
|
||||
round(decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
// If we are already in range, we're done
|
||||
let comps = this.toString().split(".");
|
||||
if (decimals < 0 || decimals > 80 || (decimals % 1)) {
|
||||
logger.throwArgumentError("invalid decimal count", "decimals", decimals);
|
||||
}
|
||||
if (comps[1].length <= decimals) {
|
||||
return this;
|
||||
}
|
||||
const factor = FixedNumber.from("1" + zeros.substring(0, decimals));
|
||||
return this.mulUnsafe(factor).addUnsafe(BUMP).floor().divUnsafe(factor);
|
||||
}
|
||||
isZero() {
|
||||
return (this._value === "0.0");
|
||||
}
|
||||
isNegative() {
|
||||
return (this._value[0] === "-");
|
||||
}
|
||||
toString() { return this._value; }
|
||||
toHexString(width) {
|
||||
if (width == null) {
|
||||
return this._hex;
|
||||
}
|
||||
if (width % 8) {
|
||||
logger.throwArgumentError("invalid byte width", "width", width);
|
||||
}
|
||||
const hex = BigNumber.from(this._hex).fromTwos(this.format.width).toTwos(width).toHexString();
|
||||
return hexZeroPad(hex, width / 8);
|
||||
}
|
||||
toUnsafeFloat() { return parseFloat(this.toString()); }
|
||||
toFormat(format) {
|
||||
return FixedNumber.fromString(this._value, format);
|
||||
}
|
||||
static fromValue(value, decimals, format) {
|
||||
// If decimals looks more like a format, and there is no format, shift the parameters
|
||||
if (format == null && decimals != null && !isBigNumberish(decimals)) {
|
||||
format = decimals;
|
||||
decimals = null;
|
||||
}
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
return FixedNumber.fromString(formatFixed(value, decimals), FixedFormat.from(format));
|
||||
}
|
||||
static fromString(value, format) {
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
const fixedFormat = FixedFormat.from(format);
|
||||
const numeric = parseFixed(value, fixedFormat.decimals);
|
||||
if (!fixedFormat.signed && numeric.lt(Zero)) {
|
||||
throwFault("unsigned value cannot be negative", "overflow", "value", value);
|
||||
}
|
||||
let hex = null;
|
||||
if (fixedFormat.signed) {
|
||||
hex = numeric.toTwos(fixedFormat.width).toHexString();
|
||||
}
|
||||
else {
|
||||
hex = numeric.toHexString();
|
||||
hex = hexZeroPad(hex, fixedFormat.width / 8);
|
||||
}
|
||||
const decimal = formatFixed(numeric, fixedFormat.decimals);
|
||||
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
||||
}
|
||||
static fromBytes(value, format) {
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
const fixedFormat = FixedFormat.from(format);
|
||||
if (arrayify(value).length > fixedFormat.width / 8) {
|
||||
throw new Error("overflow");
|
||||
}
|
||||
let numeric = BigNumber.from(value);
|
||||
if (fixedFormat.signed) {
|
||||
numeric = numeric.fromTwos(fixedFormat.width);
|
||||
}
|
||||
const hex = numeric.toTwos((fixedFormat.signed ? 0 : 1) + fixedFormat.width).toHexString();
|
||||
const decimal = formatFixed(numeric, fixedFormat.decimals);
|
||||
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
||||
}
|
||||
static from(value, format) {
|
||||
if (typeof (value) === "string") {
|
||||
return FixedNumber.fromString(value, format);
|
||||
}
|
||||
if (isBytes(value)) {
|
||||
return FixedNumber.fromBytes(value, format);
|
||||
}
|
||||
try {
|
||||
return FixedNumber.fromValue(value, 0, format);
|
||||
}
|
||||
catch (error) {
|
||||
// Allow NUMERIC_FAULT to bubble up
|
||||
if (error.code !== Logger.errors.INVALID_ARGUMENT) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return logger.throwArgumentError("invalid FixedNumber value", "value", value);
|
||||
}
|
||||
static isFixedNumber(value) {
|
||||
return !!(value && value._isFixedNumber);
|
||||
}
|
||||
}
|
||||
const ONE = FixedNumber.from(1);
|
||||
const BUMP = FixedNumber.from("0.5");
|
||||
//# sourceMappingURL=fixednumber.js.map
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export { BigNumber } from "./bignumber";
|
||||
export { formatFixed, FixedFormat, FixedNumber, parseFixed } from "./fixednumber";
|
||||
//# sourceMappingURL=index.js.map
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.version = "bignumber/5.0.8";
|
||||
//# sourceMappingURL=_version.js.map
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
"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.
|
||||
*
|
||||
*/
|
||||
var bn_js_1 = require("bn.js");
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var logger_1 = require("@ethersproject/logger");
|
||||
var _version_1 = require("./_version");
|
||||
var logger = new logger_1.Logger(_version_1.version);
|
||||
var _constructorGuard = {};
|
||||
var MAX_SAFE = 0x1fffffffffffff;
|
||||
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));
|
||||
}
|
||||
exports.isBigNumberish = isBigNumberish;
|
||||
var BigNumber = /** @class */ (function () {
|
||||
function BigNumber(constructorGuard, hex) {
|
||||
var _newTarget = this.constructor;
|
||||
logger.checkNew(_newTarget, BigNumber);
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot call constructor directly; use BigNumber.from", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new (BigNumber)"
|
||||
});
|
||||
}
|
||||
this._hex = hex;
|
||||
this._isBigNumber = true;
|
||||
Object.freeze(this);
|
||||
}
|
||||
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) {
|
||||
var value = toBN(other);
|
||||
if (value.isNeg()) {
|
||||
throwFault("cannot modulo negative values", "mod");
|
||||
}
|
||||
return toBigNumber(toBN(this).umod(value));
|
||||
};
|
||||
BigNumber.prototype.pow = function (other) {
|
||||
var value = toBN(other);
|
||||
if (value.isNeg()) {
|
||||
throwFault("cannot raise to negative values", "pow");
|
||||
}
|
||||
return toBigNumber(toBN(this).pow(value));
|
||||
};
|
||||
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");
|
||||
}
|
||||
return toBigNumber(toBN(this).maskn(value));
|
||||
};
|
||||
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));
|
||||
};
|
||||
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));
|
||||
};
|
||||
BigNumber.prototype.isNegative = function () {
|
||||
return (this._hex[0] === "-");
|
||||
};
|
||||
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) {
|
||||
logger.throwError("bigNumber.toString does not accept parameters", logger_1.Logger.errors.UNEXPECTED_ARGUMENT, {});
|
||||
}
|
||||
return toBN(this).toString(10);
|
||||
};
|
||||
BigNumber.prototype.toHexString = function () {
|
||||
return this._hex;
|
||||
};
|
||||
BigNumber.prototype.toJSON = function (key) {
|
||||
return { type: "BigNumber", hex: this.toHexString() };
|
||||
};
|
||||
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]+$/)) {
|
||||
return new BigNumber(_constructorGuard, toHex(new bn_js_1.BN(value)));
|
||||
}
|
||||
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
||||
}
|
||||
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));
|
||||
}
|
||||
var anyValue = value;
|
||||
if (typeof (anyValue) === "bigint") {
|
||||
return BigNumber.from(anyValue.toString());
|
||||
}
|
||||
if (bytes_1.isBytes(anyValue)) {
|
||||
return BigNumber.from(bytes_1.hexlify(anyValue));
|
||||
}
|
||||
if (anyValue) {
|
||||
// Hexable interface (takes piority)
|
||||
if (anyValue.toHexString) {
|
||||
var hex = anyValue.toHexString();
|
||||
if (typeof (hex) === "string") {
|
||||
return BigNumber.from(hex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// For now, handle legacy JSON-ified values (goes away in v6)
|
||||
var hex = anyValue._hex;
|
||||
// New-form JSON
|
||||
if (hex == null && anyValue.type === "BigNumber") {
|
||||
hex = anyValue.hex;
|
||||
}
|
||||
if (typeof (hex) === "string") {
|
||||
if (bytes_1.isHexString(hex) || (hex[0] === "-" && bytes_1.isHexString(hex.substring(1)))) {
|
||||
return BigNumber.from(hex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
||||
};
|
||||
BigNumber.isBigNumber = function (value) {
|
||||
return !!(value && value._isBigNumber);
|
||||
};
|
||||
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] === "-") {
|
||||
logger.throwArgumentError("invalid hex", "value", value);
|
||||
}
|
||||
// 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] === "-") {
|
||||
return (new bn_js_1.BN("-" + hex.substring(3), 16));
|
||||
}
|
||||
return new bn_js_1.BN(hex.substring(2), 16);
|
||||
}
|
||||
function throwFault(fault, operation, value) {
|
||||
var params = { fault: fault, operation: operation };
|
||||
if (value != null) {
|
||||
params.value = value;
|
||||
}
|
||||
return logger.throwError(fault, logger_1.Logger.errors.NUMERIC_FAULT, params);
|
||||
}
|
||||
//# sourceMappingURL=bignumber.js.map
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var logger_1 = require("@ethersproject/logger");
|
||||
var _version_1 = require("./_version");
|
||||
var logger = new logger_1.Logger(_version_1.version);
|
||||
var bignumber_1 = require("./bignumber");
|
||||
var _constructorGuard = {};
|
||||
var Zero = bignumber_1.BigNumber.from(0);
|
||||
var NegativeOne = bignumber_1.BigNumber.from(-1);
|
||||
function throwFault(message, fault, operation, value) {
|
||||
var params = { fault: fault, operation: operation };
|
||||
if (value !== undefined) {
|
||||
params.value = value;
|
||||
}
|
||||
return logger.throwError(message, logger_1.Logger.errors.NUMERIC_FAULT, params);
|
||||
}
|
||||
// Constant to pull zeros from for multipliers
|
||||
var zeros = "0";
|
||||
while (zeros.length < 256) {
|
||||
zeros += zeros;
|
||||
}
|
||||
// Returns a string "1" followed by decimal "0"s
|
||||
function getMultiplier(decimals) {
|
||||
if (typeof (decimals) !== "number") {
|
||||
try {
|
||||
decimals = bignumber_1.BigNumber.from(decimals).toNumber();
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (typeof (decimals) === "number" && decimals >= 0 && decimals <= 256 && !(decimals % 1)) {
|
||||
return ("1" + zeros.substring(0, decimals));
|
||||
}
|
||||
return logger.throwArgumentError("invalid decimal size", "decimals", decimals);
|
||||
}
|
||||
function formatFixed(value, decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
var multiplier = getMultiplier(decimals);
|
||||
// Make sure wei is a big number (convert as necessary)
|
||||
value = bignumber_1.BigNumber.from(value);
|
||||
var negative = value.lt(Zero);
|
||||
if (negative) {
|
||||
value = value.mul(NegativeOne);
|
||||
}
|
||||
var fraction = value.mod(multiplier).toString();
|
||||
while (fraction.length < multiplier.length - 1) {
|
||||
fraction = "0" + fraction;
|
||||
}
|
||||
// Strip training 0
|
||||
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
||||
var whole = value.div(multiplier).toString();
|
||||
value = whole + "." + fraction;
|
||||
if (negative) {
|
||||
value = "-" + value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
exports.formatFixed = formatFixed;
|
||||
function parseFixed(value, decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
var multiplier = getMultiplier(decimals);
|
||||
if (typeof (value) !== "string" || !value.match(/^-?[0-9.,]+$/)) {
|
||||
logger.throwArgumentError("invalid decimal value", "value", value);
|
||||
}
|
||||
if (multiplier.length - 1 === 0) {
|
||||
return bignumber_1.BigNumber.from(value);
|
||||
}
|
||||
// Is it negative?
|
||||
var negative = (value.substring(0, 1) === "-");
|
||||
if (negative) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
if (value === ".") {
|
||||
logger.throwArgumentError("missing value", "value", value);
|
||||
}
|
||||
// Split it into a whole and fractional part
|
||||
var comps = value.split(".");
|
||||
if (comps.length > 2) {
|
||||
logger.throwArgumentError("too many decimal points", "value", value);
|
||||
}
|
||||
var whole = comps[0], fraction = comps[1];
|
||||
if (!whole) {
|
||||
whole = "0";
|
||||
}
|
||||
if (!fraction) {
|
||||
fraction = "0";
|
||||
}
|
||||
// Prevent underflow
|
||||
if (fraction.length > multiplier.length - 1) {
|
||||
throwFault("fractional component exceeds decimals", "underflow", "parseFixed");
|
||||
}
|
||||
// Fully pad the string with zeros to get to wei
|
||||
while (fraction.length < multiplier.length - 1) {
|
||||
fraction += "0";
|
||||
}
|
||||
var wholeValue = bignumber_1.BigNumber.from(whole);
|
||||
var fractionValue = bignumber_1.BigNumber.from(fraction);
|
||||
var wei = (wholeValue.mul(multiplier)).add(fractionValue);
|
||||
if (negative) {
|
||||
wei = wei.mul(NegativeOne);
|
||||
}
|
||||
return wei;
|
||||
}
|
||||
exports.parseFixed = parseFixed;
|
||||
var FixedFormat = /** @class */ (function () {
|
||||
function FixedFormat(constructorGuard, signed, width, decimals) {
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot use FixedFormat constructor; use FixedFormat.from", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new FixedFormat"
|
||||
});
|
||||
}
|
||||
this.signed = signed;
|
||||
this.width = width;
|
||||
this.decimals = decimals;
|
||||
this.name = (signed ? "" : "u") + "fixed" + String(width) + "x" + String(decimals);
|
||||
this._multiplier = getMultiplier(decimals);
|
||||
Object.freeze(this);
|
||||
}
|
||||
FixedFormat.from = function (value) {
|
||||
if (value instanceof FixedFormat) {
|
||||
return value;
|
||||
}
|
||||
var signed = true;
|
||||
var width = 128;
|
||||
var decimals = 18;
|
||||
if (typeof (value) === "string") {
|
||||
if (value === "fixed") {
|
||||
// defaults...
|
||||
}
|
||||
else if (value === "ufixed") {
|
||||
signed = false;
|
||||
}
|
||||
else if (value != null) {
|
||||
var match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
||||
if (!match) {
|
||||
logger.throwArgumentError("invalid fixed format", "format", value);
|
||||
}
|
||||
signed = (match[1] !== "u");
|
||||
width = parseInt(match[2]);
|
||||
decimals = parseInt(match[3]);
|
||||
}
|
||||
}
|
||||
else if (value) {
|
||||
var check = function (key, type, defaultValue) {
|
||||
if (value[key] == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (typeof (value[key]) !== type) {
|
||||
logger.throwArgumentError("invalid fixed format (" + key + " not " + type + ")", "format." + key, value[key]);
|
||||
}
|
||||
return value[key];
|
||||
};
|
||||
signed = check("signed", "boolean", signed);
|
||||
width = check("width", "number", width);
|
||||
decimals = check("decimals", "number", decimals);
|
||||
}
|
||||
if (width % 8) {
|
||||
logger.throwArgumentError("invalid fixed format width (not byte aligned)", "format.width", width);
|
||||
}
|
||||
if (decimals > 80) {
|
||||
logger.throwArgumentError("invalid fixed format (decimals too large)", "format.decimals", decimals);
|
||||
}
|
||||
return new FixedFormat(_constructorGuard, signed, width, decimals);
|
||||
};
|
||||
return FixedFormat;
|
||||
}());
|
||||
exports.FixedFormat = FixedFormat;
|
||||
var FixedNumber = /** @class */ (function () {
|
||||
function FixedNumber(constructorGuard, hex, value, format) {
|
||||
var _newTarget = this.constructor;
|
||||
logger.checkNew(_newTarget, FixedNumber);
|
||||
if (constructorGuard !== _constructorGuard) {
|
||||
logger.throwError("cannot use FixedNumber constructor; use FixedNumber.from", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
||||
operation: "new FixedFormat"
|
||||
});
|
||||
}
|
||||
this.format = format;
|
||||
this._hex = hex;
|
||||
this._value = value;
|
||||
this._isFixedNumber = true;
|
||||
Object.freeze(this);
|
||||
}
|
||||
FixedNumber.prototype._checkFormat = function (other) {
|
||||
if (this.format.name !== other.format.name) {
|
||||
logger.throwArgumentError("incompatible format; use fixedNumber.toFormat", "other", other);
|
||||
}
|
||||
};
|
||||
FixedNumber.prototype.addUnsafe = function (other) {
|
||||
this._checkFormat(other);
|
||||
var a = parseFixed(this._value, this.format.decimals);
|
||||
var b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.add(b), this.format.decimals, this.format);
|
||||
};
|
||||
FixedNumber.prototype.subUnsafe = function (other) {
|
||||
this._checkFormat(other);
|
||||
var a = parseFixed(this._value, this.format.decimals);
|
||||
var b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.sub(b), this.format.decimals, this.format);
|
||||
};
|
||||
FixedNumber.prototype.mulUnsafe = function (other) {
|
||||
this._checkFormat(other);
|
||||
var a = parseFixed(this._value, this.format.decimals);
|
||||
var b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.mul(b).div(this.format._multiplier), this.format.decimals, this.format);
|
||||
};
|
||||
FixedNumber.prototype.divUnsafe = function (other) {
|
||||
this._checkFormat(other);
|
||||
var a = parseFixed(this._value, this.format.decimals);
|
||||
var b = parseFixed(other._value, other.format.decimals);
|
||||
return FixedNumber.fromValue(a.mul(this.format._multiplier).div(b), this.format.decimals, this.format);
|
||||
};
|
||||
FixedNumber.prototype.floor = function () {
|
||||
var comps = this.toString().split(".");
|
||||
var result = FixedNumber.from(comps[0], this.format);
|
||||
var hasFraction = !comps[1].match(/^(0*)$/);
|
||||
if (this.isNegative() && hasFraction) {
|
||||
result = result.subUnsafe(ONE);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
FixedNumber.prototype.ceiling = function () {
|
||||
var comps = this.toString().split(".");
|
||||
var result = FixedNumber.from(comps[0], this.format);
|
||||
var hasFraction = !comps[1].match(/^(0*)$/);
|
||||
if (!this.isNegative() && hasFraction) {
|
||||
result = result.addUnsafe(ONE);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
// @TODO: Support other rounding algorithms
|
||||
FixedNumber.prototype.round = function (decimals) {
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
// If we are already in range, we're done
|
||||
var comps = this.toString().split(".");
|
||||
if (decimals < 0 || decimals > 80 || (decimals % 1)) {
|
||||
logger.throwArgumentError("invalid decimal count", "decimals", decimals);
|
||||
}
|
||||
if (comps[1].length <= decimals) {
|
||||
return this;
|
||||
}
|
||||
var factor = FixedNumber.from("1" + zeros.substring(0, decimals));
|
||||
return this.mulUnsafe(factor).addUnsafe(BUMP).floor().divUnsafe(factor);
|
||||
};
|
||||
FixedNumber.prototype.isZero = function () {
|
||||
return (this._value === "0.0");
|
||||
};
|
||||
FixedNumber.prototype.isNegative = function () {
|
||||
return (this._value[0] === "-");
|
||||
};
|
||||
FixedNumber.prototype.toString = function () { return this._value; };
|
||||
FixedNumber.prototype.toHexString = function (width) {
|
||||
if (width == null) {
|
||||
return this._hex;
|
||||
}
|
||||
if (width % 8) {
|
||||
logger.throwArgumentError("invalid byte width", "width", width);
|
||||
}
|
||||
var hex = bignumber_1.BigNumber.from(this._hex).fromTwos(this.format.width).toTwos(width).toHexString();
|
||||
return bytes_1.hexZeroPad(hex, width / 8);
|
||||
};
|
||||
FixedNumber.prototype.toUnsafeFloat = function () { return parseFloat(this.toString()); };
|
||||
FixedNumber.prototype.toFormat = function (format) {
|
||||
return FixedNumber.fromString(this._value, format);
|
||||
};
|
||||
FixedNumber.fromValue = function (value, decimals, format) {
|
||||
// If decimals looks more like a format, and there is no format, shift the parameters
|
||||
if (format == null && decimals != null && !bignumber_1.isBigNumberish(decimals)) {
|
||||
format = decimals;
|
||||
decimals = null;
|
||||
}
|
||||
if (decimals == null) {
|
||||
decimals = 0;
|
||||
}
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
return FixedNumber.fromString(formatFixed(value, decimals), FixedFormat.from(format));
|
||||
};
|
||||
FixedNumber.fromString = function (value, format) {
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
var fixedFormat = FixedFormat.from(format);
|
||||
var numeric = parseFixed(value, fixedFormat.decimals);
|
||||
if (!fixedFormat.signed && numeric.lt(Zero)) {
|
||||
throwFault("unsigned value cannot be negative", "overflow", "value", value);
|
||||
}
|
||||
var hex = null;
|
||||
if (fixedFormat.signed) {
|
||||
hex = numeric.toTwos(fixedFormat.width).toHexString();
|
||||
}
|
||||
else {
|
||||
hex = numeric.toHexString();
|
||||
hex = bytes_1.hexZeroPad(hex, fixedFormat.width / 8);
|
||||
}
|
||||
var decimal = formatFixed(numeric, fixedFormat.decimals);
|
||||
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
||||
};
|
||||
FixedNumber.fromBytes = function (value, format) {
|
||||
if (format == null) {
|
||||
format = "fixed";
|
||||
}
|
||||
var fixedFormat = FixedFormat.from(format);
|
||||
if (bytes_1.arrayify(value).length > fixedFormat.width / 8) {
|
||||
throw new Error("overflow");
|
||||
}
|
||||
var numeric = bignumber_1.BigNumber.from(value);
|
||||
if (fixedFormat.signed) {
|
||||
numeric = numeric.fromTwos(fixedFormat.width);
|
||||
}
|
||||
var hex = numeric.toTwos((fixedFormat.signed ? 0 : 1) + fixedFormat.width).toHexString();
|
||||
var decimal = formatFixed(numeric, fixedFormat.decimals);
|
||||
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
||||
};
|
||||
FixedNumber.from = function (value, format) {
|
||||
if (typeof (value) === "string") {
|
||||
return FixedNumber.fromString(value, format);
|
||||
}
|
||||
if (bytes_1.isBytes(value)) {
|
||||
return FixedNumber.fromBytes(value, format);
|
||||
}
|
||||
try {
|
||||
return FixedNumber.fromValue(value, 0, format);
|
||||
}
|
||||
catch (error) {
|
||||
// Allow NUMERIC_FAULT to bubble up
|
||||
if (error.code !== logger_1.Logger.errors.INVALID_ARGUMENT) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return logger.throwArgumentError("invalid FixedNumber value", "value", value);
|
||||
};
|
||||
FixedNumber.isFixedNumber = function (value) {
|
||||
return !!(value && value._isFixedNumber);
|
||||
};
|
||||
return FixedNumber;
|
||||
}());
|
||||
exports.FixedNumber = FixedNumber;
|
||||
var ONE = FixedNumber.from(1);
|
||||
var BUMP = FixedNumber.from("0.5");
|
||||
//# sourceMappingURL=fixednumber.js.map
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bignumber_1 = require("./bignumber");
|
||||
exports.BigNumber = bignumber_1.BigNumber;
|
||||
var fixednumber_1 = require("./fixednumber");
|
||||
exports.formatFixed = fixednumber_1.formatFixed;
|
||||
exports.FixedFormat = fixednumber_1.FixedFormat;
|
||||
exports.FixedNumber = fixednumber_1.FixedNumber;
|
||||
exports.parseFixed = fixednumber_1.parseFixed;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"_from": "@ethersproject/bignumber@^5.0.7",
|
||||
"_id": "@ethersproject/bignumber@5.0.8",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-KXFVAFKS1jdTXYN8BE5Oj+ZfPMh28iRdFeNGBVT6cUFdtiPVqeXqc0ggvBqA3A1VoFFGgM7oAeaagA393aORHA==",
|
||||
"_location": "/@ethersproject/bignumber",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "@ethersproject/bignumber@^5.0.7",
|
||||
"name": "@ethersproject/bignumber",
|
||||
"escapedName": "@ethersproject%2fbignumber",
|
||||
"scope": "@ethersproject",
|
||||
"rawSpec": "^5.0.7",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^5.0.7"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@ethersproject/abi",
|
||||
"/@ethersproject/abstract-provider",
|
||||
"/@ethersproject/abstract-signer",
|
||||
"/@ethersproject/address",
|
||||
"/@ethersproject/constants",
|
||||
"/@ethersproject/hash",
|
||||
"/@ethersproject/transactions"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/@ethersproject/bignumber/-/bignumber-5.0.8.tgz",
|
||||
"_shasum": "cee33bd8eb0266176def0d371b45274b1d2c4ec0",
|
||||
"_spec": "@ethersproject/bignumber@^5.0.7",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\@ethersproject\\transactions",
|
||||
"author": {
|
||||
"name": "Richard Moore",
|
||||
"email": "me@ricmoo.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ethers-io/ethers.js/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"@ethersproject/bytes": "^5.0.4",
|
||||
"@ethersproject/logger": "^5.0.5",
|
||||
"bn.js": "^4.4.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "BigNumber library used in ethers.js.",
|
||||
"ethereum": "donations.ethers.eth",
|
||||
"gitHead": "ffa4a2d0549fe2c606bda3130f4a3f3d788f8dcf",
|
||||
"homepage": "https://github.com/ethers-io/ethers.js#readme",
|
||||
"keywords": [
|
||||
"Ethereum",
|
||||
"bignumber",
|
||||
"bn"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "./lib/index.js",
|
||||
"module": "./lib.esm/index.js",
|
||||
"name": "@ethersproject/bignumber",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/ethers-io/ethers.js.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"tarballHash": "0x13de0d9c0138d0d056ed658b8f8744fab45fcdf4b52dcd9456bfd7285a4288ce",
|
||||
"types": "./lib/index.d.ts",
|
||||
"version": "5.0.8"
|
||||
}
|
||||
Reference in new issue
Block a user