@etherspot/data-utils
Version:
Etherspot Data Utils
354 lines (349 loc) • 9.58 kB
JavaScript
import {
hexlifyValue
} from "./chunk-MZME4UK2.mjs";
import {
isBytes
} from "./chunk-ZRPFBHDV.mjs";
import {
isHex
} from "./chunk-NH5DXPP4.mjs";
import {
Logger
} from "./chunk-3SR2Y2NU.mjs";
// src/sdk/common/types/bignumber.ts
import _BN from "bn.js";
// src/sdk/common/utils/is-big-number.ts
function isBigNumber(value) {
return value instanceof BigNumber;
}
// src/sdk/common/utils/deep-compare.ts
function deepCompare(a, b) {
let result = false;
const aType = typeof a;
if (aType === typeof b) {
switch (aType) {
case "object":
if (a === null || b === null) {
result = a === b;
} else if (a === b) {
result = true;
} else if (isBigNumber(a) && isBigNumber(b)) {
result = a.eq(b);
} else if (a instanceof Date && b instanceof Date) {
result = a.getTime() === b.getTime();
} else {
const aIsArray = Array.isArray(a);
const bIsArray = Array.isArray(b);
if (aIsArray && bIsArray) {
const aLength = a.length;
const bLength = b.length;
if (aLength === bLength) {
result = true;
for (let index = 0; index < aLength; index += 1) {
if (!deepCompare(a[index], b[index])) {
result = false;
break;
}
}
}
} else if (!aIsArray && !bIsArray) {
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length === bKeys.length) {
result = true;
for (const key of aKeys) {
if (!deepCompare(a[key], b[key])) {
result = false;
break;
}
}
}
}
}
break;
case "function":
result = true;
break;
default:
result = a === b;
}
}
return result;
}
// src/sdk/common/types/bignumber.ts
var BN = _BN.BN;
var version = "logger/5.7.0";
var logger = new Logger(version);
var _constructorGuard = {};
var MAX_SAFE = 9007199254740991;
function isBigNumberish(value) {
return value != null && (BigNumber.isBigNumber(value) || typeof value === "number" && value % 1 === 0 || typeof value === "string" && !!value.match(/^-?[0-9]+$/) || isHex(value) || typeof value === "bigint" || isBytes(value));
}
var _warnedToStringRadix = false;
var BigNumber = class _BigNumber {
constructor(constructorGuard, hex) {
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("division-by-zero", "mod");
}
return toBigNumber(toBN(this).umod(value));
}
pow(other) {
const value = toBN(other);
if (value.isNeg()) {
throwFault("negative-power", "pow");
}
return toBigNumber(toBN(this).pow(value));
}
and(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "and");
}
return toBigNumber(toBN(this).and(value));
}
or(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "or");
}
return toBigNumber(toBN(this).or(value));
}
xor(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "xor");
}
return toBigNumber(toBN(this).xor(value));
}
mask(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "mask");
}
return toBigNumber(toBN(this).maskn(value));
}
shl(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "shl");
}
return toBigNumber(toBN(this).shln(value));
}
shr(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "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;
}
toBigInt() {
try {
return BigInt(this.toString());
} catch (e) {
}
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
value: this.toString()
});
}
toString() {
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 {
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(hexlifyValue(anyValue));
}
if (anyValue) {
if (anyValue.toHexString) {
const hex = anyValue.toHexString();
if (typeof hex === "string") {
return _BigNumber.from(hex);
}
} else {
let hex = anyValue._hex;
if (hex == null && anyValue.type === "BigNumber") {
hex = anyValue.hex;
}
if (typeof hex === "string") {
if (isHex(hex) || hex[0] === "-" && isHex(hex.substring(1))) {
return _BigNumber.from(hex);
}
}
}
}
return logger.throwArgumentError("invalid BigNumber value", "value", value);
}
static isBigNumber(value) {
return !!(value && value._isBigNumber);
}
};
function toHex(value) {
if (typeof value !== "string") {
return toHex(value.toString(16));
}
if (value[0] === "-") {
value = value.substring(1);
if (value[0] === "-") {
throw new Error(`invalid hex value: ${value}`);
}
value = toHex(value);
if (value === "0x00") {
return value;
}
return "-" + value;
}
if (value.substring(0, 2) !== "0x") {
value = "0x" + value;
}
if (value === "0x") {
return "0x00";
}
if (value.length % 2) {
value = "0x0" + value.substring(2);
}
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 _base36To16(value) {
return new BN(value, 36).toString(16);
}
function _base16To36(value) {
return new BN(value, 16).toString(36);
}
function throwFault(fault, operation, value) {
const params = { fault, operation };
if (value != null) {
params.value = value;
}
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
}
export {
version,
isBigNumberish,
BigNumber,
_base36To16,
_base16To36,
throwFault,
isBigNumber,
deepCompare
};
//# sourceMappingURL=chunk-DEX5RLI7.mjs.map