{"version":3,"sources":["../../src/module/percent.ts","../../src/module/fraction.ts","../../src/common/logger.ts","../../src/common/constant.ts","../../src/module/formatter.ts"],"sourcesContent":["import BN from \"bn.js\";\nimport { Rounding } from \"../common\";\nimport { Fraction } from \"./fraction\";\n\nexport const _100_PERCENT = new Fraction(new BN(100));\n\nexport class Percent extends Fraction {\n  public toSignificant(significantDigits = 5, format?: object, rounding?: Rounding): string {\n    return this.mul(_100_PERCENT).toSignificant(significantDigits, format, rounding);\n  }\n\n  public toFixed(decimalPlaces = 2, format?: object, rounding?: Rounding): string {\n    return this.mul(_100_PERCENT).toFixed(decimalPlaces, format, rounding);\n  }\n}\n","import _Big from \"big.js\";\nimport BN from \"bn.js\";\nimport _Decimal from \"decimal.js-light\";\n\nimport { BigNumberish } from \"../common/bignumber\";\nimport { createLogger } from \"../common/logger\";\n\nimport { parseBigNumberish, Rounding } from \"../common/constant\";\nimport toFormat, { WrappedBig } from \"./formatter\";\n\nconst logger = createLogger(\"module/fraction\");\n\nconst Big = toFormat(_Big);\ntype Big = WrappedBig;\n\nconst Decimal = toFormat(_Decimal);\n\nconst toSignificantRounding = {\n  [Rounding.ROUND_DOWN]: Decimal.ROUND_DOWN,\n  [Rounding.ROUND_HALF_UP]: Decimal.ROUND_HALF_UP,\n  [Rounding.ROUND_UP]: Decimal.ROUND_UP,\n};\n\nconst toFixedRounding = {\n  [Rounding.ROUND_DOWN]: _Big.roundDown,\n  [Rounding.ROUND_HALF_UP]: _Big.roundHalfUp,\n  [Rounding.ROUND_UP]: _Big.roundUp,\n};\n\nexport class Fraction {\n  public readonly numerator: BN;\n  public readonly denominator: BN;\n\n  public constructor(numerator: BigNumberish, denominator: BigNumberish = new BN(1)) {\n    this.numerator = parseBigNumberish(numerator);\n    this.denominator = parseBigNumberish(denominator);\n  }\n\n  public get quotient(): BN {\n    return this.numerator.div(this.denominator);\n  }\n\n  public invert(): Fraction {\n    return new Fraction(this.denominator, this.numerator);\n  }\n\n  public add(other: Fraction | BigNumberish): Fraction {\n    const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigNumberish(other));\n\n    if (this.denominator.eq(otherParsed.denominator)) {\n      return new Fraction(this.numerator.add(otherParsed.numerator), this.denominator);\n    }\n\n    return new Fraction(\n      this.numerator.mul(otherParsed.denominator).add(otherParsed.numerator.mul(this.denominator)),\n      this.denominator.mul(otherParsed.denominator),\n    );\n  }\n\n  public sub(other: Fraction | BigNumberish): Fraction {\n    const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigNumberish(other));\n\n    if (this.denominator.eq(otherParsed.denominator)) {\n      return new Fraction(this.numerator.sub(otherParsed.numerator), this.denominator);\n    }\n\n    return new Fraction(\n      this.numerator.mul(otherParsed.denominator).sub(otherParsed.numerator.mul(this.denominator)),\n      this.denominator.mul(otherParsed.denominator),\n    );\n  }\n\n  public mul(other: Fraction | BigNumberish): Fraction {\n    const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigNumberish(other));\n\n    return new Fraction(this.numerator.mul(otherParsed.numerator), this.denominator.mul(otherParsed.denominator));\n  }\n\n  public div(other: Fraction | BigNumberish): Fraction {\n    const otherParsed = other instanceof Fraction ? other : new Fraction(parseBigNumberish(other));\n\n    return new Fraction(this.numerator.mul(otherParsed.denominator), this.denominator.mul(otherParsed.numerator));\n  }\n\n  public toSignificant(\n    significantDigits: number,\n    format: object = { groupSeparator: \"\" },\n    rounding: Rounding = Rounding.ROUND_HALF_UP,\n  ): string {\n    if (!Number.isInteger(significantDigits)) logger.logWithError(`${significantDigits} is not an integer.`);\n    if (significantDigits <= 0) logger.logWithError(`${significantDigits} is not positive.`);\n\n    Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });\n    const quotient = new Decimal(this.numerator.toString())\n      .div(this.denominator.toString())\n      .toSignificantDigits(significantDigits);\n    return quotient.toFormat(quotient.decimalPlaces(), format);\n  }\n\n  public toFixed(\n    decimalPlaces: number,\n    format: object = { groupSeparator: \"\" },\n    rounding: Rounding = Rounding.ROUND_HALF_UP,\n  ): string {\n    if (!Number.isInteger(decimalPlaces)) logger.logWithError(`${decimalPlaces} is not an integer.`);\n    if (decimalPlaces < 0) logger.logWithError(`${decimalPlaces} is negative.`);\n\n    Big.DP = decimalPlaces;\n    Big.RM = toFixedRounding[rounding] || 1;\n    return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);\n  }\n\n  public isZero(): boolean {\n    return this.numerator.isZero();\n  }\n}\n","import { get, set } from \"lodash\";\n\nexport type ModuleName = \"Common.Api\";\n\nexport enum LogLevel {\n  Error,\n  Warning,\n  Info,\n  Debug,\n}\nexport class Logger {\n  private logLevel: LogLevel;\n  private name: string;\n  constructor(params: { name: string; logLevel?: LogLevel }) {\n    this.logLevel = params.logLevel !== undefined ? params.logLevel : LogLevel.Error;\n    this.name = params.name;\n  }\n\n  set level(logLevel: LogLevel) {\n    this.logLevel = logLevel;\n  }\n  get time(): string {\n    return Date.now().toString();\n  }\n  get moduleName(): string {\n    return this.name;\n  }\n\n  private isLogLevel(level: LogLevel): boolean {\n    return level <= this.logLevel;\n  }\n\n  public error(...props): Logger {\n    if (!this.isLogLevel(LogLevel.Error)) return this;\n    console.error(this.time, this.name, \"sdk logger error\", ...props);\n    return this;\n  }\n\n  public logWithError(...props): Logger {\n    // this.error(...props)\n    const msg = props.map((arg) => (typeof arg === \"object\" ? JSON.stringify(arg) : arg)).join(\", \");\n    throw new Error(msg);\n  }\n\n  public warning(...props): Logger {\n    if (!this.isLogLevel(LogLevel.Warning)) return this;\n    console.warn(this.time, this.name, \"sdk logger warning\", ...props);\n    return this;\n  }\n\n  public info(...props): Logger {\n    if (!this.isLogLevel(LogLevel.Info)) return this;\n    console.info(this.time, this.name, \"sdk logger info\", ...props);\n    return this;\n  }\n\n  public debug(...props): Logger {\n    if (!this.isLogLevel(LogLevel.Debug)) return this;\n    console.debug(this.time, this.name, \"sdk logger debug\", ...props);\n    return this;\n  }\n}\n\nconst moduleLoggers: { [key in ModuleName]?: Logger } = {};\nconst moduleLevels: { [key in ModuleName]?: LogLevel } = {};\n\nexport function createLogger(moduleName: string): Logger {\n  let logger = get(moduleLoggers, moduleName);\n  if (!logger) {\n    // default level is error\n    const logLevel = get(moduleLevels, moduleName);\n\n    logger = new Logger({ name: moduleName, logLevel });\n    set(moduleLoggers, moduleName, logger);\n  }\n\n  return logger;\n}\n\nexport function setLoggerLevel(moduleName: string, level: LogLevel): void {\n  set(moduleLevels, moduleName, level);\n\n  const logger = get(moduleLoggers, moduleName);\n  if (logger) logger.level = level;\n}\n","import BN from \"bn.js\";\nimport { BigNumberish } from \"./bignumber\";\nimport { createLogger } from \"./logger\";\n\nexport enum Rounding {\n  ROUND_DOWN,\n  ROUND_HALF_UP,\n  ROUND_UP,\n}\n\nconst MAX_SAFE = 0x1fffffffffffff;\n\nexport function parseBigNumberish(value: BigNumberish): BN {\n  const logger = createLogger(\"Sega_parseBigNumberish\");\n  // BN\n  if (value instanceof BN) {\n    return value;\n  }\n\n  if (typeof value === \"string\") {\n    if (value.match(/^-?[0-9]+$/)) {\n      return new BN(value);\n    }\n    logger.logWithError(`invalid BigNumberish string: ${value}`);\n  }\n\n  if (typeof value === \"number\") {\n    if (value % 1) {\n      logger.logWithError(`BigNumberish number underflow: ${value}`);\n    }\n\n    if (value >= MAX_SAFE || value <= -MAX_SAFE) {\n      logger.logWithError(`BigNumberish number overflow: ${value}`);\n    }\n\n    return new BN(String(value));\n  }\n\n  if (typeof value === \"bigint\") {\n    return new BN(value.toString());\n  }\n  logger.error(`invalid BigNumberish value: ${value}`);\n  return new BN(0); // never reach, because logWithError will throw error\n}\n","import Big, { BigConstructor, BigSource, RoundingMode } from \"big.js\";\nimport Decimal, { Config, Numeric } from \"decimal.js-light\";\nimport _toFarmat from \"toformat\";\n\ntype TakeStatic<T> = { [P in keyof T]: T[P] };\ninterface FormatOptions {\n  decimalSeparator?: string;\n  groupSeparator?: string;\n  groupSize?: number;\n  fractionGroupSeparator?: string;\n  fractionGroupSize?: number;\n}\ninterface WrappedBigConstructor extends TakeStatic<BigConstructor> {\n  new (value: BigSource): WrappedBig;\n  (value: BigSource): WrappedBig;\n  (): WrappedBigConstructor;\n\n  format: FormatOptions;\n}\nexport interface WrappedBig extends Big {\n  add(n: BigSource): WrappedBig;\n  abs(): WrappedBig;\n  div(n: BigSource): WrappedBig;\n  minus(n: BigSource): WrappedBig;\n  mod(n: BigSource): WrappedBig;\n  mul(n: BigSource): WrappedBig;\n  plus(n: BigSource): WrappedBig;\n  pow(exp: number): WrappedBig;\n  round(dp?: number, rm?: RoundingMode): WrappedBig;\n  sqrt(): WrappedBig;\n  sub(n: BigSource): WrappedBig;\n  times(n: BigSource): WrappedBig;\n  toFormat(): string;\n  toFormat(options: FormatOptions): string;\n  toFormat(fractionLength: number): string;\n  toFormat(fractionLength: number, options: FormatOptions): string;\n  toFormat(fractionLength: number, missionUnknown: number): string;\n  toFormat(fractionLength: number, missionUnknown: number, options: FormatOptions): string;\n}\n\ntype DecimalConstructor = typeof Decimal;\ninterface WrappedDecimalConstructor extends TakeStatic<DecimalConstructor> {\n  new (value: Numeric): WrappedDecimal;\n  clone(config?: Config): WrappedDecimalConstructor;\n  config(config: Config): WrappedDecimal;\n  set(config: Config): WrappedDecimal;\n  format: FormatOptions;\n}\nexport interface WrappedDecimal extends Decimal {\n  absoluteValue(): WrappedDecimal;\n  abs(): WrappedDecimal;\n  dividedBy(y: Numeric): WrappedDecimal;\n  div(y: Numeric): WrappedDecimal;\n  dividedToIntegerBy(y: Numeric): WrappedDecimal;\n  idiv(y: Numeric): WrappedDecimal;\n  logarithm(base?: Numeric): WrappedDecimal;\n  log(base?: Numeric): WrappedDecimal;\n  minus(y: Numeric): WrappedDecimal;\n  sub(y: Numeric): WrappedDecimal;\n  modulo(y: Numeric): WrappedDecimal;\n  mod(y: Numeric): WrappedDecimal;\n  naturalExponetial(): WrappedDecimal;\n  exp(): WrappedDecimal;\n  naturalLogarithm(): WrappedDecimal;\n  ln(): WrappedDecimal;\n  negated(): WrappedDecimal;\n  neg(): WrappedDecimal;\n  plus(y: Numeric): WrappedDecimal;\n  add(y: Numeric): WrappedDecimal;\n  squareRoot(): WrappedDecimal;\n  sqrt(): WrappedDecimal;\n  times(y: Numeric): WrappedDecimal;\n  mul(y: Numeric): WrappedDecimal;\n  toWrappedDecimalPlaces(dp?: number, rm?: number): WrappedDecimal;\n  todp(dp?: number, rm?: number): WrappedDecimal;\n  toInteger(): WrappedDecimal;\n  toint(): WrappedDecimal;\n  toPower(y: Numeric): WrappedDecimal;\n  pow(y: Numeric): WrappedDecimal;\n  toSignificantDigits(sd?: number, rm?: number): WrappedDecimal;\n  tosd(sd?: number, rm?: number): WrappedDecimal;\n  toFormat(options: FormatOptions): string;\n  toFormat(fractionLength: number): string;\n  toFormat(fractionLength: number, options: FormatOptions): string;\n  toFormat(fractionLength: number, missionUnknown: number): string;\n  toFormat(fractionLength: number, missionUnknown: number, options: FormatOptions): string;\n}\n\nconst toFormat: {\n  (fn: BigConstructor): WrappedBigConstructor;\n  (fn: DecimalConstructor): WrappedDecimalConstructor;\n} = _toFarmat;\nexport default toFormat;\n"],"mappings":"AAAA,OAAOA,MAAQ,QCAf,OAAOC,MAAU,SACjB,OAAOC,MAAQ,QACf,OAAOC,MAAc,mBCFrB,OAAS,OAAAC,EAAK,OAAAC,MAAW,SAUlB,IAAMC,EAAN,KAAa,CAGlB,YAAYC,EAA+C,CACzD,KAAK,SAAWA,EAAO,WAAa,OAAYA,EAAO,SAAW,EAClE,KAAK,KAAOA,EAAO,IACrB,CAEA,IAAI,MAAMC,EAAoB,CAC5B,KAAK,SAAWA,CAClB,CACA,IAAI,MAAe,CACjB,OAAO,KAAK,IAAI,EAAE,SAAS,CAC7B,CACA,IAAI,YAAqB,CACvB,OAAO,KAAK,IACd,CAEQ,WAAWC,EAA0B,CAC3C,OAAOA,GAAS,KAAK,QACvB,CAEO,SAASC,EAAe,CAC7B,OAAK,KAAK,WAAW,CAAc,GACnC,QAAQ,MAAM,KAAK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ACxC,OAAOG,EAAS,SAASA,EAAS,cAAc,EAAGF,CAAM,CAC3D,CAEO,QACLG,EACAH,EAAiB,CAAE,eAAgB,EAAG,EACtCC,IACQ,CACR,OAAK,OAAO,UAAUE,CAAa,GAAGrB,EAAO,aAAa,GAAGqB,CAAa,qBAAqB,EAC3FA,EAAgB,GAAGrB,EAAO,aAAa,GAAGqB,CAAa,eAAe,EAE1EnB,EAAI,GAAKmB,EACTnB,EAAI,GAAKM,EAAgBW,CAAQ,GAAK,EAC/B,IAAIjB,EAAI,KAAK,UAAU,SAAS,CAAC,EAAE,IAAI,KAAK,YAAY,SAAS,CAAC,EAAE,SAASmB,EAAeH,CAAM,CAC3G,CAEO,QAAkB,CACvB,OAAO,KAAK,UAAU,OAAO,CAC/B,CACF,ED/GO,IAAMI,EAAe,IAAIC,EAAS,IAAIC,EAAG,GAAG,CAAC,EAEvCC,EAAN,cAAsBF,CAAS,CAC7B,cAAcG,EAAoB,EAAGC,EAAiBC,EAA6B,CACxF,OAAO,KAAK,IAAIN,CAAY,EAAE,cAAcI,EAAmBC,EAAQC,CAAQ,CACjF,CAEO,QAAQC,EAAgB,EAAGF,EAAiBC,EAA6B,CAC9E,OAAO,KAAK,IAAIN,CAAY,EAAE,QAAQO,EAAeF,EAAQC,CAAQ,CACvE,CACF","names":["BN","_Big","BN","_Decimal","get","set","Logger","params","logLevel","level","props","msg","arg","moduleLoggers","moduleLevels","createLogger","moduleName","logger","get","set","BN","MAX_SAFE","parseBigNumberish","value","logger","createLogger","BN","_toFarmat","toFormat","formatter_default","logger","createLogger","Big","formatter_default","_Big","Decimal","_Decimal","toSignificantRounding","toFixedRounding","Fraction","_Fraction","numerator","denominator","BN","parseBigNumberish","other","otherParsed","significantDigits","format","rounding","quotient","decimalPlaces","_100_PERCENT","Fraction","BN","Percent","significantDigits","format","rounding","decimalPlaces"]}