From 0859f84bac6a5b9c94530d6c257bce1b13870f33 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 10 Dec 2022 16:07:58 -0500 Subject: [PATCH] Small changes in maths on error. --- src.ts/_tests/test-utils-maths.ts | 8 ++++---- src.ts/utils/maths.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src.ts/_tests/test-utils-maths.ts b/src.ts/_tests/test-utils-maths.ts index 198fe4ee9..652472c81 100644 --- a/src.ts/_tests/test-utils-maths.ts +++ b/src.ts/_tests/test-utils-maths.ts @@ -146,7 +146,7 @@ describe("Tests Bad Math Values", function() { { name: "negative value", value: -4, - error: "cannot toBeHex negative value" + error: "unsigned value cannot be negative" }, { name: "width too short", @@ -162,7 +162,7 @@ describe("Tests Bad Math Values", function() { const result = toBeHex(value, width); console.log(result); }, (e: any) => { - return (isError(e, "INVALID_ARGUMENT") && + return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" && e.message.startsWith(error)); }); }); @@ -173,8 +173,8 @@ describe("Tests Bad Math Values", function() { const result = toBeArray(-4); console.log(result); }, (e: any) => { - return (isError(e, "INVALID_ARGUMENT") && - e.message.startsWith("cannot toBeArray negative value")); + return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" && + e.message.startsWith("unsigned value cannot be negative")); }); }); }); diff --git a/src.ts/utils/maths.ts b/src.ts/utils/maths.ts index 6c6653b0a..18bff7fbe 100644 --- a/src.ts/utils/maths.ts +++ b/src.ts/utils/maths.ts @@ -115,7 +115,7 @@ export function getBigInt(value: BigNumberish, name?: string): bigint { export function getUint(value: BigNumberish, name?: string): bigint { const result = getBigInt(value, name); - assert(result >= BN_0, "overflow", "NUMERIC_FAULT", { + assert(result >= BN_0, "unsigned value cannot be negative", "NUMERIC_FAULT", { fault: "overflow", operation: "getUint", value }); return result; @@ -187,7 +187,11 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string { if (result.length % 2) { result = "0" + result; } } else { const width = getNumber(_width, "width"); - assertArgument(width * 2 >= result.length, `value exceeds width`, "[ value, width ]", [ _value, _width ]); + assert(width * 2 >= result.length, `value exceeds width (${ width } bits)`, "NUMERIC_FAULT", { + operation: "toBeHex", + fault: "overflow", + value: _value + }); // Pad the value to the required width while (result.length < (width * 2)) { result = "0" + result; }