11import * as BigNumber from 'bignumber.js' ;
22
33export const NumberUtil = {
4+ /**
5+ * Creates a BigNumber instance from a given value.
6+ * If the value is a string, commas are removed before conversion.
7+ * @param value - The input value (string, number, or BigNumber) to convert to a BigNumber.
8+ * @returns A BigNumber instance.
9+ */
410 bigNumber ( value : BigNumber . BigNumber . Value ) {
511 if ( typeof value === 'string' ) {
612 return new BigNumber . BigNumber ( value . replace ( / , / g, '' ) ) ;
@@ -10,10 +16,11 @@ export const NumberUtil = {
1016 } ,
1117
1218 /**
13- * Multiply two numbers represented as strings with BigNumber to handle decimals correctly
14- * @param a string
15- * @param b string
16- * @returns
19+ * Multiplies two numbers using BigNumber for precision, especially with decimals.
20+ * Handles undefined inputs by returning BigNumber(0).
21+ * @param a - The first multiplicand (string, number, or BigNumber). Commas are removed if it's a string.
22+ * @param b - The second multiplicand (string, number, or BigNumber). Commas are removed if it's a string.
23+ * @returns The product as a BigNumber instance, or BigNumber(0) if either input is undefined.
1724 */
1825 multiply ( a : BigNumber . BigNumber . Value | undefined , b : BigNumber . BigNumber . Value | undefined ) {
1926 if ( a === undefined || b === undefined ) {
@@ -26,24 +33,37 @@ export const NumberUtil = {
2633 return aBigNumber . multipliedBy ( bBigNumber ) ;
2734 } ,
2835
36+ /**
37+ * Rounds a number to a specified number of decimal places if its string representation meets a certain length threshold.
38+ * @param number - The number to potentially round.
39+ * @param threshold - The minimum string length of the number to trigger rounding.
40+ * @param fixed - The number of decimal places to round to.
41+ * @returns The rounded number (as a string if rounded, otherwise the original number) or the original number.
42+ */
2943 roundNumber ( number : number , threshold : number , fixed : number ) {
3044 const roundedNumber =
3145 number . toString ( ) . length >= threshold ? Number ( number ) . toFixed ( fixed ) : number ;
3246
3347 return roundedNumber ;
3448 } ,
3549
50+ /**
51+ * Calculates the next multiple of ten greater than or equal to the given amount.
52+ * Defaults to 10 if no amount is provided or if the calculated multiple is less than 10.
53+ * @param amount - The number for which to find the next multiple of ten. Optional.
54+ * @returns The next multiple of ten, at least 10.
55+ */
3656 nextMultipleOfTen ( amount ?: number ) {
3757 if ( ! amount ) return 10 ;
3858
3959 return Math . max ( Math . ceil ( amount / 10 ) * 10 , 10 ) ;
4060 } ,
4161
4262 /**
43- * Format the given number or string to human readable numbers with the given number of decimals
44- * @param value - The value to format. It could be a number or string . If it's a string, it will be parsed to a float then formatted .
45- * @param decimals - number of decimals after dot
46- * @returns
63+ * Formats a number or string to a human- readable string with a specified number of decimal places, using US locale formatting.
64+ * @param value - The value to format (string, number, or undefined) . If undefined, returns '0.00' .
65+ * @param decimals - The number of decimal places to display. Defaults to 2.
66+ * @returns A locale-formatted string representation of the number.
4767 */
4868 formatNumberToLocalString ( value : string | number | undefined , decimals = 2 ) {
4969 if ( value === undefined ) {
@@ -62,10 +82,11 @@ export const NumberUtil = {
6282 minimumFractionDigits : decimals
6383 } ) ;
6484 } ,
85+
6586 /**
66- * Parse a formatted local string back to a number
67- * @param value - The formatted string to parse
68- * @returns
87+ * Parses a locale- formatted numeric string (e.g., with commas) back into a number.
88+ * @param value - The formatted string to parse. If undefined, returns 0.
89+ * @returns The parsed number, or 0 if the input is undefined.
6990 */
7091 parseLocalStringToNumber ( value : string | undefined ) {
7192 if ( value === undefined ) {
@@ -74,5 +95,27 @@ export const NumberUtil = {
7495
7596 // Remove any commas used as thousand separators and parse the float
7697 return parseFloat ( value . replace ( / , / gu, '' ) ) ;
98+ } ,
99+
100+ /**
101+ * Converts a numeric value (BigInt, number, or string representation of a number) to a 0x-prefixed hexadecimal string.
102+ * This is often required for Ethereum RPC parameters like value, gas, gasPrice.
103+ * @param value - The value to convert. Can be BigInt, number, or a string (decimal or hex).
104+ * @returns A 0x-prefixed hexadecimal string, or undefined if the input is undefined or null.
105+ * @throws Error if the value cannot be converted to BigInt.
106+ */
107+ convertNumericToHexString : ( value : any ) : string | undefined => {
108+ if ( value === undefined || value === null ) {
109+ return undefined ;
110+ }
111+ try {
112+ // This handles BigInt, number, or string representation of a number (decimal or hex)
113+ const bigIntValue = BigInt ( value ) ;
114+ // Ethereum RPC spec requires "0x0" for zero, and other values to be 0x-prefixed hex.
115+
116+ return '0x' + bigIntValue . toString ( 16 ) ;
117+ } catch ( error ) {
118+ throw new Error ( `NumberUtil: Invalid parameter, cannot convert to hex: ${ value } ` ) ;
119+ }
77120 }
78121} ;
0 commit comments