fixednumber.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /**
  2. * The **FixedNumber** class permits using values with decimal places,
  3. * using fixed-pont math.
  4. *
  5. * Fixed-point math is still based on integers under-the-hood, but uses an
  6. * internal offset to store fractional components below, and each operation
  7. * corrects for this after each operation.
  8. *
  9. * @_section: api/utils/fixed-point-math:Fixed-Point Maths [about-fixed-point-math]
  10. */
  11. import { getBytes } from "./data.js";
  12. import { assert, assertArgument, assertPrivate } from "./errors.js";
  13. import {
  14. getBigInt, getNumber, fromTwos, mask, toBigInt
  15. } from "./maths.js";
  16. import { defineProperties } from "./properties.js";
  17. import type { BigNumberish, BytesLike, Numeric } from "./index.js";
  18. const BN_N1 = BigInt(-1);
  19. const BN_0 = BigInt(0);
  20. const BN_1 = BigInt(1);
  21. const BN_5 = BigInt(5);
  22. const _guard = { };
  23. // Constant to pull zeros from for multipliers
  24. let Zeros = "0000";
  25. while (Zeros.length < 80) { Zeros += Zeros; }
  26. // Returns a string "1" followed by decimal "0"s
  27. function getTens(decimals: number): bigint {
  28. let result = Zeros;
  29. while (result.length < decimals) { result += result; }
  30. return BigInt("1" + result.substring(0, decimals));
  31. }
  32. /*
  33. * Returns a new FixedFormat for %%value%%.
  34. *
  35. * If %%value%% is specified as a ``number``, the bit-width is
  36. * 128 bits and %%value%% is used for the ``decimals``.
  37. *
  38. * A string %%value%% may begin with ``fixed`` or ``ufixed``
  39. * for signed and unsigned respectfully. If no other properties
  40. * are specified, the bit-width is 128-bits with 18 decimals.
  41. *
  42. * To specify the bit-width and demicals, append them separated
  43. * by an ``"x"`` to the %%value%%.
  44. *
  45. * For example, ``ufixed128x18`` describes an unsigned, 128-bit
  46. * wide format with 18 decimals.
  47. *
  48. * If %%value%% is an other object, its properties for ``signed``,
  49. * ``width`` and ``decimals`` are checked.
  50. */
  51. /**
  52. * A description of a fixed-point arithmetic field.
  53. *
  54. * When specifying the fixed format, the values override the default of
  55. * a ``fixed128x18``, which implies a signed 128-bit value with 18
  56. * decimals of precision.
  57. *
  58. * The alias ``fixed`` and ``ufixed`` can be used for ``fixed128x18`` and
  59. * ``ufixed128x18`` respectively.
  60. *
  61. * When a fixed format string begins with a ``u``, it indicates the field
  62. * is unsigned, so any negative values will overflow. The first number
  63. * indicates the bit-width and the second number indicates the decimal
  64. * precision.
  65. *
  66. * When a ``number`` is used for a fixed format, it indicates the number
  67. * of decimal places, and the default width and signed-ness will be used.
  68. *
  69. * The bit-width must be byte aligned and the decimals can be at most 80.
  70. */
  71. export type FixedFormat = number | string | {
  72. signed?: boolean,
  73. width?: number,
  74. decimals?: number
  75. };
  76. function checkValue(val: bigint, format: _FixedFormat, safeOp?: string): bigint {
  77. const width = BigInt(format.width);
  78. if (format.signed) {
  79. const limit = (BN_1 << (width - BN_1));
  80. assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
  81. operation: <string>safeOp, fault: "overflow", value: val
  82. });
  83. if (val > BN_0) {
  84. val = fromTwos(mask(val, width), width);
  85. } else {
  86. val = -fromTwos(mask(-val, width), width);
  87. }
  88. } else {
  89. const limit = (BN_1 << width);
  90. assert(safeOp == null || (val >= 0 && val < limit), "overflow", "NUMERIC_FAULT", {
  91. operation: <string>safeOp, fault: "overflow", value: val
  92. });
  93. val = (((val % limit) + limit) % limit) & (limit - BN_1);
  94. }
  95. return val;
  96. }
  97. type _FixedFormat = { signed: boolean, width: number, decimals: number, name: string }
  98. function getFormat(value?: FixedFormat): _FixedFormat {
  99. if (typeof(value) === "number") { value = `fixed128x${value}` }
  100. let signed = true;
  101. let width = 128;
  102. let decimals = 18;
  103. if (typeof(value) === "string") {
  104. // Parse the format string
  105. if (value === "fixed") {
  106. // defaults...
  107. } else if (value === "ufixed") {
  108. signed = false;
  109. } else {
  110. const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
  111. assertArgument(match, "invalid fixed format", "format", value);
  112. signed = (match[1] !== "u");
  113. width = parseInt(match[2]);
  114. decimals = parseInt(match[3]);
  115. }
  116. } else if (value) {
  117. // Extract the values from the object
  118. const v: any = value;
  119. const check = (key: string, type: string, defaultValue: any): any => {
  120. if (v[key] == null) { return defaultValue; }
  121. assertArgument(typeof(v[key]) === type,
  122. "invalid fixed format (" + key + " not " + type +")", "format." + key, v[key]);
  123. return v[key];
  124. }
  125. signed = check("signed", "boolean", signed);
  126. width = check("width", "number", width);
  127. decimals = check("decimals", "number", decimals);
  128. }
  129. assertArgument((width % 8) === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
  130. assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
  131. const name = (signed ? "": "u") + "fixed" + String(width) + "x" + String(decimals);
  132. return { signed, width, decimals, name };
  133. }
  134. function toString(val: bigint, decimals: number) {
  135. let negative = "";
  136. if (val < BN_0) {
  137. negative = "-";
  138. val *= BN_N1;
  139. }
  140. let str = val.toString();
  141. // No decimal point for whole values
  142. if (decimals === 0) { return (negative + str); }
  143. // Pad out to the whole component (including a whole digit)
  144. while (str.length <= decimals) { str = Zeros + str; }
  145. // Insert the decimal point
  146. const index = str.length - decimals;
  147. str = str.substring(0, index) + "." + str.substring(index);
  148. // Trim the whole component (leaving at least one 0)
  149. while (str[0] === "0" && str[1] !== ".") {
  150. str = str.substring(1);
  151. }
  152. // Trim the decimal component (leaving at least one 0)
  153. while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") {
  154. str = str.substring(0, str.length - 1);
  155. }
  156. return (negative + str);
  157. }
  158. /**
  159. * A FixedNumber represents a value over its [[FixedFormat]]
  160. * arithmetic field.
  161. *
  162. * A FixedNumber can be used to perform math, losslessly, on
  163. * values which have decmial places.
  164. *
  165. * A FixedNumber has a fixed bit-width to store values in, and stores all
  166. * values internally by multiplying the value by 10 raised to the power of
  167. * %%decimals%%.
  168. *
  169. * If operations are performed that cause a value to grow too high (close to
  170. * positive infinity) or too low (close to negative infinity), the value
  171. * is said to //overflow//.
  172. *
  173. * For example, an 8-bit signed value, with 0 decimals may only be within
  174. * the range ``-128`` to ``127``; so ``-128 - 1`` will overflow and become
  175. * ``127``. Likewise, ``127 + 1`` will overflow and become ``-127``.
  176. *
  177. * Many operation have a normal and //unsafe// variant. The normal variant
  178. * will throw a [[NumericFaultError]] on any overflow, while the //unsafe//
  179. * variant will silently allow overflow, corrupting its value value.
  180. *
  181. * If operations are performed that cause a value to become too small
  182. * (close to zero), the value loses precison and is said to //underflow//.
  183. *
  184. * For example, an value with 1 decimal place may store a number as small
  185. * as ``0.1``, but the value of ``0.1 / 2`` is ``0.05``, which cannot fit
  186. * into 1 decimal place, so underflow occurs which means precision is lost
  187. * and the value becomes ``0``.
  188. *
  189. * Some operations have a normal and //signalling// variant. The normal
  190. * variant will silently ignore underflow, while the //signalling// variant
  191. * will thow a [[NumericFaultError]] on underflow.
  192. */
  193. export class FixedNumber {
  194. /**
  195. * The specific fixed-point arithmetic field for this value.
  196. */
  197. readonly format!: string;
  198. readonly #format: _FixedFormat;
  199. // The actual value (accounting for decimals)
  200. #val: bigint;
  201. // A base-10 value to multiple values by to maintain the magnitude
  202. readonly #tens: bigint;
  203. /**
  204. * This is a property so console.log shows a human-meaningful value.
  205. *
  206. * @private
  207. */
  208. readonly _value!: string;
  209. // Use this when changing this file to get some typing info,
  210. // but then switch to any to mask the internal type
  211. //constructor(guard: any, value: bigint, format: _FixedFormat) {
  212. /**
  213. * @private
  214. */
  215. constructor(guard: any, value: bigint, format: any) {
  216. assertPrivate(guard, _guard, "FixedNumber");
  217. this.#val = value;
  218. this.#format = format;
  219. const _value = toString(value, format.decimals);
  220. defineProperties<FixedNumber>(this, { format: format.name, _value });
  221. this.#tens = getTens(format.decimals);
  222. }
  223. /**
  224. * If true, negative values are permitted, otherwise only
  225. * positive values and zero are allowed.
  226. */
  227. get signed(): boolean { return this.#format.signed; }
  228. /**
  229. * The number of bits available to store the value.
  230. */
  231. get width(): number { return this.#format.width; }
  232. /**
  233. * The number of decimal places in the fixed-point arithment field.
  234. */
  235. get decimals(): number { return this.#format.decimals; }
  236. /**
  237. * The value as an integer, based on the smallest unit the
  238. * [[decimals]] allow.
  239. */
  240. get value(): bigint { return this.#val; }
  241. #checkFormat(other: FixedNumber): void {
  242. assertArgument(this.format === other.format,
  243. "incompatible format; use fixedNumber.toFormat", "other", other);
  244. }
  245. #checkValue(val: bigint, safeOp?: string): FixedNumber {
  246. /*
  247. const width = BigInt(this.width);
  248. if (this.signed) {
  249. const limit = (BN_1 << (width - BN_1));
  250. assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
  251. operation: <string>safeOp, fault: "overflow", value: val
  252. });
  253. if (val > BN_0) {
  254. val = fromTwos(mask(val, width), width);
  255. } else {
  256. val = -fromTwos(mask(-val, width), width);
  257. }
  258. } else {
  259. const masked = mask(val, width);
  260. assert(safeOp == null || (val >= 0 && val === masked), "overflow", "NUMERIC_FAULT", {
  261. operation: <string>safeOp, fault: "overflow", value: val
  262. });
  263. val = masked;
  264. }
  265. */
  266. val = checkValue(val, this.#format, safeOp);
  267. return new FixedNumber(_guard, val, this.#format);
  268. }
  269. #add(o: FixedNumber, safeOp?: string): FixedNumber {
  270. this.#checkFormat(o);
  271. return this.#checkValue(this.#val + o.#val, safeOp);
  272. }
  273. /**
  274. * Returns a new [[FixedNumber]] with the result of %%this%% added
  275. * to %%other%%, ignoring overflow.
  276. */
  277. addUnsafe(other: FixedNumber): FixedNumber { return this.#add(other); }
  278. /**
  279. * Returns a new [[FixedNumber]] with the result of %%this%% added
  280. * to %%other%%. A [[NumericFaultError]] is thrown if overflow
  281. * occurs.
  282. */
  283. add(other: FixedNumber): FixedNumber { return this.#add(other, "add"); }
  284. #sub(o: FixedNumber, safeOp?: string): FixedNumber {
  285. this.#checkFormat(o);
  286. return this.#checkValue(this.#val - o.#val, safeOp);
  287. }
  288. /**
  289. * Returns a new [[FixedNumber]] with the result of %%other%% subtracted
  290. * from %%this%%, ignoring overflow.
  291. */
  292. subUnsafe(other: FixedNumber): FixedNumber { return this.#sub(other); }
  293. /**
  294. * Returns a new [[FixedNumber]] with the result of %%other%% subtracted
  295. * from %%this%%. A [[NumericFaultError]] is thrown if overflow
  296. * occurs.
  297. */
  298. sub(other: FixedNumber): FixedNumber { return this.#sub(other, "sub"); }
  299. #mul(o: FixedNumber, safeOp?: string): FixedNumber {
  300. this.#checkFormat(o);
  301. return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
  302. }
  303. /**
  304. * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
  305. * by %%other%%, ignoring overflow and underflow (precision loss).
  306. */
  307. mulUnsafe(other: FixedNumber): FixedNumber { return this.#mul(other); }
  308. /**
  309. * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
  310. * by %%other%%. A [[NumericFaultError]] is thrown if overflow
  311. * occurs.
  312. */
  313. mul(other: FixedNumber): FixedNumber { return this.#mul(other, "mul"); }
  314. /**
  315. * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
  316. * by %%other%%. A [[NumericFaultError]] is thrown if overflow
  317. * occurs or if underflow (precision loss) occurs.
  318. */
  319. mulSignal(other: FixedNumber): FixedNumber {
  320. this.#checkFormat(other);
  321. const value = this.#val * other.#val;
  322. assert((value % this.#tens) === BN_0, "precision lost during signalling mul", "NUMERIC_FAULT", {
  323. operation: "mulSignal", fault: "underflow", value: this
  324. });
  325. return this.#checkValue(value / this.#tens, "mulSignal");
  326. }
  327. #div(o: FixedNumber, safeOp?: string): FixedNumber {
  328. assert(o.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
  329. operation: "div", fault: "divide-by-zero", value: this
  330. });
  331. this.#checkFormat(o);
  332. return this.#checkValue((this.#val * this.#tens) / o.#val, safeOp);
  333. }
  334. /**
  335. * Returns a new [[FixedNumber]] with the result of %%this%% divided
  336. * by %%other%%, ignoring underflow (precision loss). A
  337. * [[NumericFaultError]] is thrown if overflow occurs.
  338. */
  339. divUnsafe(other: FixedNumber): FixedNumber { return this.#div(other); }
  340. /**
  341. * Returns a new [[FixedNumber]] with the result of %%this%% divided
  342. * by %%other%%, ignoring underflow (precision loss). A
  343. * [[NumericFaultError]] is thrown if overflow occurs.
  344. */
  345. div(other: FixedNumber): FixedNumber { return this.#div(other, "div"); }
  346. /**
  347. * Returns a new [[FixedNumber]] with the result of %%this%% divided
  348. * by %%other%%. A [[NumericFaultError]] is thrown if underflow
  349. * (precision loss) occurs.
  350. */
  351. divSignal(other: FixedNumber): FixedNumber {
  352. assert(other.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
  353. operation: "div", fault: "divide-by-zero", value: this
  354. });
  355. this.#checkFormat(other);
  356. const value = (this.#val * this.#tens);
  357. assert((value % other.#val) === BN_0, "precision lost during signalling div", "NUMERIC_FAULT", {
  358. operation: "divSignal", fault: "underflow", value: this
  359. });
  360. return this.#checkValue(value / other.#val, "divSignal");
  361. }
  362. /**
  363. * Returns a comparison result between %%this%% and %%other%%.
  364. *
  365. * This is suitable for use in sorting, where ``-1`` implies %%this%%
  366. * is smaller, ``1`` implies %%this%% is larger and ``0`` implies
  367. * both are equal.
  368. */
  369. cmp(other: FixedNumber): number {
  370. let a = this.value, b = other.value;
  371. // Coerce a and b to the same magnitude
  372. const delta = this.decimals - other.decimals;
  373. if (delta > 0) {
  374. b *= getTens(delta);
  375. } else if (delta < 0) {
  376. a *= getTens(-delta);
  377. }
  378. // Comnpare
  379. if (a < b) { return -1; }
  380. if (a > b) { return 1; }
  381. return 0;
  382. }
  383. /**
  384. * Returns true if %%other%% is equal to %%this%%.
  385. */
  386. eq(other: FixedNumber): boolean { return this.cmp(other) === 0; }
  387. /**
  388. * Returns true if %%other%% is less than to %%this%%.
  389. */
  390. lt(other: FixedNumber): boolean { return this.cmp(other) < 0; }
  391. /**
  392. * Returns true if %%other%% is less than or equal to %%this%%.
  393. */
  394. lte(other: FixedNumber): boolean { return this.cmp(other) <= 0; }
  395. /**
  396. * Returns true if %%other%% is greater than to %%this%%.
  397. */
  398. gt(other: FixedNumber): boolean { return this.cmp(other) > 0; }
  399. /**
  400. * Returns true if %%other%% is greater than or equal to %%this%%.
  401. */
  402. gte(other: FixedNumber): boolean { return this.cmp(other) >= 0; }
  403. /**
  404. * Returns a new [[FixedNumber]] which is the largest **integer**
  405. * that is less than or equal to %%this%%.
  406. *
  407. * The decimal component of the result will always be ``0``.
  408. */
  409. floor(): FixedNumber {
  410. let val = this.#val;
  411. if (this.#val < BN_0) { val -= this.#tens - BN_1; }
  412. val = (this.#val / this.#tens) * this.#tens;
  413. return this.#checkValue(val, "floor");
  414. }
  415. /**
  416. * Returns a new [[FixedNumber]] which is the smallest **integer**
  417. * that is greater than or equal to %%this%%.
  418. *
  419. * The decimal component of the result will always be ``0``.
  420. */
  421. ceiling(): FixedNumber {
  422. let val = this.#val;
  423. if (this.#val > BN_0) { val += this.#tens - BN_1; }
  424. val = (this.#val / this.#tens) * this.#tens;
  425. return this.#checkValue(val, "ceiling");
  426. }
  427. /**
  428. * Returns a new [[FixedNumber]] with the decimal component
  429. * rounded up on ties at %%decimals%% places.
  430. */
  431. round(decimals?: number): FixedNumber {
  432. if (decimals == null) { decimals = 0; }
  433. // Not enough precision to not already be rounded
  434. if (decimals >= this.decimals) { return this; }
  435. const delta = this.decimals - decimals;
  436. const bump = BN_5 * getTens(delta - 1);
  437. let value = this.value + bump;
  438. const tens = getTens(delta);
  439. value = (value / tens) * tens;
  440. checkValue(value, this.#format, "round");
  441. return new FixedNumber(_guard, value, this.#format);
  442. }
  443. /**
  444. * Returns true if %%this%% is equal to ``0``.
  445. */
  446. isZero(): boolean { return (this.#val === BN_0); }
  447. /**
  448. * Returns true if %%this%% is less than ``0``.
  449. */
  450. isNegative(): boolean { return (this.#val < BN_0); }
  451. /**
  452. * Returns the string representation of %%this%%.
  453. */
  454. toString(): string { return this._value; }
  455. /**
  456. * Returns a float approximation.
  457. *
  458. * Due to IEEE 754 precission (or lack thereof), this function
  459. * can only return an approximation and most values will contain
  460. * rounding errors.
  461. */
  462. toUnsafeFloat(): number { return parseFloat(this.toString()); }
  463. /**
  464. * Return a new [[FixedNumber]] with the same value but has had
  465. * its field set to %%format%%.
  466. *
  467. * This will throw if the value cannot fit into %%format%%.
  468. */
  469. toFormat(format: FixedFormat): FixedNumber {
  470. return FixedNumber.fromString(this.toString(), format);
  471. }
  472. /**
  473. * Creates a new [[FixedNumber]] for %%value%% divided by
  474. * %%decimal%% places with %%format%%.
  475. *
  476. * This will throw a [[NumericFaultError]] if %%value%% (once adjusted
  477. * for %%decimals%%) cannot fit in %%format%%, either due to overflow
  478. * or underflow (precision loss).
  479. */
  480. static fromValue(_value: BigNumberish, _decimals?: Numeric, _format?: FixedFormat): FixedNumber {
  481. const decimals = (_decimals == null) ? 0: getNumber(_decimals);
  482. const format = getFormat(_format);
  483. let value = getBigInt(_value, "value");
  484. const delta = decimals - format.decimals;
  485. if (delta > 0) {
  486. const tens = getTens(delta);
  487. assert((value % tens) === BN_0, "value loses precision for format", "NUMERIC_FAULT", {
  488. operation: "fromValue", fault: "underflow", value: _value
  489. });
  490. value /= tens;
  491. } else if (delta < 0) {
  492. value *= getTens(-delta);
  493. }
  494. checkValue(value, format, "fromValue");
  495. return new FixedNumber(_guard, value, format);
  496. }
  497. /**
  498. * Creates a new [[FixedNumber]] for %%value%% with %%format%%.
  499. *
  500. * This will throw a [[NumericFaultError]] if %%value%% cannot fit
  501. * in %%format%%, either due to overflow or underflow (precision loss).
  502. */
  503. static fromString(_value: string, _format?: FixedFormat): FixedNumber {
  504. const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
  505. assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
  506. const format = getFormat(_format);
  507. let whole = (match[2] || "0"), decimal = (match[3] || "");
  508. // Pad out the decimals
  509. while (decimal.length < format.decimals) { decimal += Zeros; }
  510. // Check precision is safe
  511. assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
  512. operation: "fromString", fault: "underflow", value: _value
  513. });
  514. // Remove extra padding
  515. decimal = decimal.substring(0, format.decimals);
  516. const value = BigInt(match[1] + whole + decimal)
  517. checkValue(value, format, "fromString");
  518. return new FixedNumber(_guard, value, format);
  519. }
  520. /**
  521. * Creates a new [[FixedNumber]] with the big-endian representation
  522. * %%value%% with %%format%%.
  523. *
  524. * This will throw a [[NumericFaultError]] if %%value%% cannot fit
  525. * in %%format%% due to overflow.
  526. */
  527. static fromBytes(_value: BytesLike, _format?: FixedFormat): FixedNumber {
  528. let value = toBigInt(getBytes(_value, "value"));
  529. const format = getFormat(_format);
  530. if (format.signed) { value = fromTwos(value, format.width); }
  531. checkValue(value, format, "fromBytes");
  532. return new FixedNumber(_guard, value, format);
  533. }
  534. }
  535. //const f1 = FixedNumber.fromString("12.56", "fixed16x2");
  536. //const f2 = FixedNumber.fromString("0.3", "fixed16x2");
  537. //console.log(f1.divSignal(f2));
  538. //const BUMP = FixedNumber.from("0.5");