2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { arrayify, BytesLike, hexZeroPad, isBytes } from "@ethersproject/bytes";
|
2019-08-02 01:04:06 +03:00
|
|
|
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
import { BigNumber, BigNumberish, isBigNumberish } from "./bignumber";
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
const _constructorGuard = { };
|
|
|
|
|
|
|
|
const Zero = BigNumber.from(0);
|
|
|
|
const NegativeOne = BigNumber.from(-1);
|
|
|
|
|
|
|
|
function throwFault(message: string, fault: string, operation: string, value?: any): never {
|
2019-11-01 17:33:51 +03:00
|
|
|
const params: any = { fault: fault, operation: operation };
|
2019-05-15 01:25:46 +03:00
|
|
|
if (value !== undefined) { params.value = value; }
|
2019-08-02 01:04:06 +03:00
|
|
|
return logger.throwError(message, Logger.errors.NUMERIC_FAULT, params);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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: BigNumberish): string {
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
return logger.throwArgumentError("invalid decimal size", "decimals", decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatFixed(value: BigNumberish, decimals?: string | BigNumberish): string {
|
|
|
|
if (decimals == null) { decimals = 0; }
|
2019-11-01 17:33:51 +03:00
|
|
|
const multiplier = getMultiplier(decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
// Make sure wei is a big number (convert as necessary)
|
|
|
|
value = BigNumber.from(value);
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const negative = value.lt(Zero);
|
2019-05-15 01:25:46 +03:00
|
|
|
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];
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const whole = value.div(multiplier).toString();
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
value = whole + "." + fraction;
|
|
|
|
|
|
|
|
if (negative) { value = "-" + value; }
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseFixed(value: string, decimals?: BigNumberish): BigNumber {
|
|
|
|
if (decimals == null) { decimals = 0; }
|
2019-11-01 17:33:51 +03:00
|
|
|
const multiplier = getMultiplier(decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (typeof(value) !== "string" || !value.match(/^-?[0-9.,]+$/)) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid decimal value", "value", value);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (multiplier.length - 1 === 0) {
|
|
|
|
return BigNumber.from(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it negative?
|
2019-11-01 17:33:51 +03:00
|
|
|
const negative = (value.substring(0, 1) === "-");
|
2019-05-15 01:25:46 +03:00
|
|
|
if (negative) { value = value.substring(1); }
|
|
|
|
|
|
|
|
if (value === ".") {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("missing value", "value", value);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split it into a whole and fractional part
|
2019-11-01 17:33:51 +03:00
|
|
|
const comps = value.split(".");
|
2019-05-15 01:25:46 +03:00
|
|
|
if (comps.length > 2) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("too many decimal points", "value", value);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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"; }
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const wholeValue = BigNumber.from(whole);
|
|
|
|
const fractionValue = BigNumber.from(fraction);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
let wei = (wholeValue.mul(multiplier)).add(fractionValue);
|
|
|
|
|
|
|
|
if (negative) { wei = wei.mul(NegativeOne); }
|
|
|
|
|
|
|
|
return wei;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FixedFormat {
|
|
|
|
readonly signed: boolean;
|
|
|
|
readonly width: number;
|
|
|
|
readonly decimals: number;
|
|
|
|
readonly name: string;
|
2019-06-11 05:25:46 +03:00
|
|
|
readonly _multiplier: string;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
constructor(constructorGuard: any, signed: boolean, width: number, decimals: number) {
|
2020-06-29 07:38:27 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
2020-06-29 07:45:47 +03:00
|
|
|
logger.throwError("cannot use FixedFormat constructor; use FixedFormat.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
2020-06-29 07:38:27 +03:00
|
|
|
operation: "new FixedFormat"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
this.signed = signed;
|
|
|
|
this.width = width;
|
|
|
|
this.decimals = decimals;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
this.name = (signed ? "": "u") + "fixed" + String(width) + "x" + String(decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
this._multiplier = getMultiplier(decimals);
|
|
|
|
|
|
|
|
Object.freeze(this);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static from(value: any): FixedFormat {
|
|
|
|
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) {
|
2019-11-01 17:33:51 +03:00
|
|
|
const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
2019-08-02 01:04:06 +03:00
|
|
|
if (!match) { logger.throwArgumentError("invalid fixed format", "format", value); }
|
2019-05-15 01:25:46 +03:00
|
|
|
signed = (match[1] !== "u");
|
|
|
|
width = parseInt(match[2]);
|
|
|
|
decimals = parseInt(match[3]);
|
|
|
|
}
|
|
|
|
} else if (value) {
|
2019-11-01 17:33:51 +03:00
|
|
|
const check = (key: string, type: string, defaultValue: any): any => {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (value[key] == null) { return defaultValue; }
|
|
|
|
if (typeof(value[key]) !== type) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid fixed format (" + key + " not " + type +")", "format." + key, value[key]);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
return value[key];
|
|
|
|
}
|
|
|
|
signed = check("signed", "boolean", signed);
|
|
|
|
width = check("width", "number", width);
|
|
|
|
decimals = check("decimals", "number", decimals);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width % 8) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid fixed format width (not byte aligned)", "format.width", width);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (decimals > 80) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid fixed format (decimals too large)", "format.decimals", decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new FixedFormat(_constructorGuard, signed, width, decimals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FixedNumber {
|
|
|
|
readonly format: FixedFormat;
|
|
|
|
readonly _hex: string;
|
|
|
|
readonly _value: string;
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
readonly _isFixedNumber: boolean;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
constructor(constructorGuard: any, hex: string, value: string, format?: FixedFormat) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkNew(new.target, FixedNumber);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2020-06-29 07:38:27 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
2020-06-29 07:45:47 +03:00
|
|
|
logger.throwError("cannot use FixedNumber constructor; use FixedNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
2020-06-29 07:38:27 +03:00
|
|
|
operation: "new FixedFormat"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
this.format = format;
|
|
|
|
this._hex = hex;
|
|
|
|
this._value = value;
|
|
|
|
|
|
|
|
this._isFixedNumber = true;
|
|
|
|
|
|
|
|
Object.freeze(this);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
_checkFormat(other: FixedNumber): void {
|
|
|
|
if (this.format.name !== other.format.name) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("incompatible format; use fixedNumber.toFormat", "other", other);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addUnsafe(other: FixedNumber): FixedNumber {
|
|
|
|
this._checkFormat(other);
|
2019-11-01 17:33:51 +03:00
|
|
|
const a = parseFixed(this._value, this.format.decimals);
|
|
|
|
const b = parseFixed(other._value, other.format.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
return FixedNumber.fromValue(a.add(b), this.format.decimals, this.format);
|
|
|
|
}
|
|
|
|
|
|
|
|
subUnsafe(other: FixedNumber): FixedNumber {
|
|
|
|
this._checkFormat(other);
|
2019-11-01 17:33:51 +03:00
|
|
|
const a = parseFixed(this._value, this.format.decimals);
|
|
|
|
const b = parseFixed(other._value, other.format.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
return FixedNumber.fromValue(a.sub(b), this.format.decimals, this.format);
|
|
|
|
}
|
|
|
|
|
|
|
|
mulUnsafe(other: FixedNumber): FixedNumber {
|
|
|
|
this._checkFormat(other);
|
2019-11-01 17:33:51 +03:00
|
|
|
const a = parseFixed(this._value, this.format.decimals);
|
|
|
|
const b = parseFixed(other._value, other.format.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
return FixedNumber.fromValue(a.mul(b).div(this.format._multiplier), this.format.decimals, this.format);
|
|
|
|
}
|
|
|
|
|
|
|
|
divUnsafe(other: FixedNumber): FixedNumber {
|
|
|
|
this._checkFormat(other);
|
2019-11-01 17:33:51 +03:00
|
|
|
const a = parseFixed(this._value, this.format.decimals);
|
|
|
|
const b = parseFixed(other._value, other.format.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
return FixedNumber.fromValue(a.mul(this.format._multiplier).div(b), this.format.decimals, this.format);
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:16:28 +03:00
|
|
|
floor(): FixedNumber {
|
|
|
|
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(): FixedNumber {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
// @TODO: Support other rounding algorithms
|
|
|
|
round(decimals?: number): FixedNumber {
|
|
|
|
if (decimals == null) { decimals = 0; }
|
2020-09-10 08:16:28 +03:00
|
|
|
|
|
|
|
// If we are already in range, we're done
|
|
|
|
let comps = this.toString().split(".");
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
if (decimals < 0 || decimals > 80 || (decimals % 1)) {
|
2020-04-22 09:42:25 +03:00
|
|
|
logger.throwArgumentError("invalid decimal count", "decimals", decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (comps[1].length <= decimals) { return this; }
|
|
|
|
|
2020-09-10 08:16:28 +03:00
|
|
|
const factor = FixedNumber.from("1" + zeros.substring(0, decimals));
|
|
|
|
return this.mulUnsafe(factor).addUnsafe(BUMP).floor().divUnsafe(factor);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 07:38:27 +03:00
|
|
|
isZero(): boolean {
|
|
|
|
return (this._value === "0.0");
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:16:28 +03:00
|
|
|
isNegative(): boolean {
|
|
|
|
return (this._value[0] === "-");
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
toString(): string { return this._value; }
|
|
|
|
|
|
|
|
toHexString(width?: number): string {
|
|
|
|
if (width == null) { return this._hex; }
|
2019-08-02 01:04:06 +03:00
|
|
|
if (width % 8) { logger.throwArgumentError("invalid byte width", "width", width); }
|
2019-11-01 17:33:51 +03:00
|
|
|
const hex = BigNumber.from(this._hex).fromTwos(this.format.width).toTwos(width).toHexString();
|
2019-05-15 01:25:46 +03:00
|
|
|
return hexZeroPad(hex, width / 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
toUnsafeFloat(): number { return parseFloat(this.toString()); }
|
|
|
|
|
|
|
|
toFormat(format: FixedFormat | string): FixedNumber {
|
|
|
|
return FixedNumber.fromString(this._value, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static fromValue(value: BigNumber, decimals?: BigNumberish, format?: FixedFormat | string): FixedNumber {
|
|
|
|
// If decimals looks more like a format, and there is no format, shift the parameters
|
2019-06-11 05:25:46 +03:00
|
|
|
if (format == null && decimals != null && !isBigNumberish(decimals)) {
|
2019-05-15 01:25:46 +03:00
|
|
|
format = decimals;
|
|
|
|
decimals = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decimals == null) { decimals = 0; }
|
|
|
|
if (format == null) { format = "fixed"; }
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
return FixedNumber.fromString(formatFixed(value, decimals), FixedFormat.from(format));
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static fromString(value: string, format?: FixedFormat | string): FixedNumber {
|
|
|
|
if (format == null) { format = "fixed"; }
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const fixedFormat = FixedFormat.from(format);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const numeric = parseFixed(value, fixedFormat.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (!fixedFormat.signed && numeric.lt(Zero)) {
|
|
|
|
throwFault("unsigned value cannot be negative", "overflow", "value", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let hex: string = null;
|
|
|
|
if (fixedFormat.signed) {
|
|
|
|
hex = numeric.toTwos(fixedFormat.width).toHexString();
|
|
|
|
} else {
|
|
|
|
hex = numeric.toHexString();
|
|
|
|
hex = hexZeroPad(hex, fixedFormat.width / 8);
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const decimal = formatFixed(numeric, fixedFormat.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromBytes(value: BytesLike, format?: FixedFormat | string): FixedNumber {
|
|
|
|
if (format == null) { format = "fixed"; }
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const fixedFormat = FixedFormat.from(format);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
if (arrayify(value).length > fixedFormat.width / 8) {
|
|
|
|
throw new Error("overflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
let numeric = BigNumber.from(value);
|
|
|
|
if (fixedFormat.signed) { numeric = numeric.fromTwos(fixedFormat.width); }
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const hex = numeric.toTwos((fixedFormat.signed ? 0: 1) + fixedFormat.width).toHexString();
|
|
|
|
const decimal = formatFixed(numeric, fixedFormat.decimals);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
return new FixedNumber(_constructorGuard, hex, decimal, fixedFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static from(value: any, format?: FixedFormat | string) {
|
|
|
|
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
|
2019-08-02 01:04:06 +03:00
|
|
|
if (error.code !== Logger.errors.INVALID_ARGUMENT) {
|
2019-05-15 01:25:46 +03:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
return logger.throwArgumentError("invalid FixedNumber value", "value", value);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static isFixedNumber(value: any): value is FixedNumber {
|
2019-06-11 05:25:46 +03:00
|
|
|
return !!(value && value._isFixedNumber);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 08:16:28 +03:00
|
|
|
|
|
|
|
const ONE = FixedNumber.from(1);
|
|
|
|
const BUMP = FixedNumber.from("0.5");
|