55 toHexString ,
66 formatTokenAmount ,
77 DEFAULT_TOKEN_DECIMALS ,
8+ U256_MAX ,
9+ U256OverflowError ,
810} from "./codec" ;
911
1012describe ( "parseU256" , ( ) => {
@@ -20,6 +22,19 @@ describe("parseU256", () => {
2022 expect ( BigInt ( r . low as never ) ) . toBe ( 7n ) ;
2123 expect ( BigInt ( r . high as never ) ) . toBe ( 1n ) ;
2224 } ) ;
25+
26+ it ( "accepts the maximum representable value" , ( ) => {
27+ expect ( parseU256 ( U256_MAX . toString ( ) ) ) . toBeTruthy ( ) ;
28+ } ) ;
29+
30+ it ( "throws a distinct error for values outside the u256 range" , ( ) => {
31+ expect ( ( ) => parseU256 ( ( U256_MAX + 1n ) . toString ( ) ) ) . toThrow ( U256OverflowError ) ;
32+ expect ( ( ) => parseU256 ( "-1" ) ) . toThrow ( U256OverflowError ) ;
33+ } ) ;
34+
35+ it ( "keeps the existing native error path for malformed values" , ( ) => {
36+ expect ( ( ) => parseU256 ( "not-a-number" ) ) . toThrow ( SyntaxError ) ;
37+ } ) ;
2338} ) ;
2439
2540describe ( "u256ToString" , ( ) => {
@@ -103,7 +118,7 @@ describe("formatTokenAmount", () => {
103118 it ( "formats the full u256 max without precision loss" , ( ) => {
104119 const u256Max = ( 1n << 256n ) - 1n ;
105120 expect ( formatTokenAmount ( u256Max ) ) . toBe (
106- "115792089237316195423570985008687907853269984665640564039457584007913129.639935"
121+ "115792089237316195423570985008687907853269984665640564039457584007913129.639935" ,
107122 ) ;
108123 } ) ;
109124
@@ -124,7 +139,7 @@ describe("Fuzz testing round-trip (encode/decode)", () => {
124139 const MAX_U256 = ( 1n << 256n ) - 1n ;
125140 const MAX_U128 = ( 1n << 128n ) - 1n ;
126141 const iterations = 10000 ;
127-
142+
128143 for ( let i = 0 ; i < iterations ; i ++ ) {
129144 let randomBigInt = 0n ;
130145 // Generate random up to 256 bits by appending 32 random bits at a time
@@ -137,11 +152,11 @@ describe("Fuzz testing round-trip (encode/decode)", () => {
137152 if ( i === 1 ) randomBigInt = MAX_U256 ;
138153 if ( i === 2 ) randomBigInt = MAX_U128 ;
139154 if ( i === 3 ) randomBigInt = 1n << 128n ; // min value in high part
140-
155+
141156 const str = randomBigInt . toString ( ) ;
142157 const parsed = parseU256 ( str ) ;
143158 const back = u256ToString ( parsed ) ;
144-
159+
145160 expect ( back ) . toBe ( str ) ;
146161 }
147162 } ) ;
0 commit comments