/
ikratkiy
/
Hyperliquid
Обзор
Документация
Войти
/
ikratkiy
/
Hyperliquid
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/shared/hyperliquid/decimalMath.test.ts
55 строк
2 KB
ikratkiy
fix(spot): validate canonical IOC preconditions
14 июл 2026, 22:42
14 июл 2026, 22:42
689be06
Код
Авторство
О чём код?
import { describe, expect, test } from "vitest"; import { addDecimals, compareDecimals, divideDecimals, formatPlainDecimal, formatRatio, multiplyDecimals, parsePlainDecimal, subtractDecimals, } from "./decimalMath"; describe("exact decimal math", () => { test.each(["1e-3", "NaN", "Infinity", "+1", " 1", "1 ", ".1", "1.", "", "-"])( "rejects non-plain decimal %s", (value) => { expect(() => parsePlainDecimal(value)).toThrow("Invalid plain decimal"); }, ); test.each([ ["00012.34000", "12.34"], ["-000.0100", "-0.01"], ["0.00000000", "0"], ["1000000000000000000000000.000", "1000000000000000000000000"], ])("canonicalizes %s without exponent or trailing zeroes", (input, expected) => { expect(formatPlainDecimal(parsePlainDecimal(input))).toBe(expected); }); test("compares, adds and subtracts different scales exactly", () => { const left = parsePlainDecimal("0.00030"); const right = parsePlainDecimal("0.00001"); expect(compareDecimals(left, parsePlainDecimal("0.0003"))).toBe(0); expect(formatPlainDecimal(subtractDecimals(left, right))).toBe("0.00029"); expect(formatPlainDecimal(addDecimals(right, parsePlainDecimal("0.00029")))).toBe("0.0003"); }); test("multiplies large and small values without Number precision loss", () => { const product = multiplyDecimals( parsePlainDecimal("12345678901234567890.12345"), parsePlainDecimal("0.00001"), ); expect(formatPlainDecimal(product)).toBe("123456789012345.6789012345"); }); test("retains a non-terminating quotient as an exact reduced ratio", () => { const ratio = divideDecimals(parsePlainDecimal("1"), parsePlainDecimal("3")); expect(ratio).toEqual({ numerator: 1n, denominator: 3n }); expect(formatRatio(ratio, 8)).toBe("0.33333333"); expect(formatRatio(ratio, 8, "ceil")).toBe("0.33333334"); }); });