@@ -11,12 +11,15 @@ import {
1111 minPrice ,
1212 type Price ,
1313 type PriceDai ,
14+ type PriceEnsTokens ,
1415 type PriceEth ,
1516 type PriceUsdc ,
1617 parseDai ,
18+ parseEnsTokens ,
1719 parseEth ,
1820 parseUsdc ,
1921 priceDai ,
22+ priceEnsTokens ,
2023 priceEth ,
2124 priceUsdc ,
2225 scalePrice ,
@@ -31,6 +34,12 @@ describe("Currencies", () => {
3134 name : "ETH" ,
3235 decimals : 18 ,
3336 } satisfies CurrencyInfo ) ;
37+
38+ expect ( getCurrencyInfo ( CurrencyIds . ENSTokens ) ) . toStrictEqual ( {
39+ id : CurrencyIds . ENSTokens ,
40+ name : "$ENS Tokens" ,
41+ decimals : 18 ,
42+ } satisfies CurrencyInfo ) ;
3443 } ) ;
3544 } ) ;
3645
@@ -61,6 +70,15 @@ describe("Currencies", () => {
6170 } ) ;
6271 } ) ;
6372
73+ describe ( "priceEnsTokens" , ( ) => {
74+ it ( "returns correct Price object" , ( ) => {
75+ expect ( priceEnsTokens ( 1n ) ) . toStrictEqual ( {
76+ amount : 1n ,
77+ currency : CurrencyIds . ENSTokens ,
78+ } satisfies PriceEnsTokens ) ;
79+ } ) ;
80+ } ) ;
81+
6482 describe ( "isPriceCurrencyEqual" , ( ) => {
6583 it ( "returns true when two prices have the same currency" , ( ) => {
6684 expect ( isPriceCurrencyEqual ( priceEth ( 1n ) , priceEth ( 1n ) ) ) . toBe ( true ) ;
@@ -199,6 +217,13 @@ describe("Currencies", () => {
199217 expect ( result . amount ) . toBe ( 500000000000000000n ) ;
200218 } ) ;
201219
220+ it ( "should preserve ENSTokens currency type and scale correctly" , ( ) => {
221+ const price : PriceEnsTokens = priceEnsTokens ( 1000000000000000000n ) ;
222+ const result : PriceEnsTokens = scalePrice ( price , 0.5 ) ;
223+ expect ( result . currency ) . toBe ( "ENSTokens" ) ;
224+ expect ( result . amount ) . toBe ( 500000000000000000n ) ;
225+ } ) ;
226+
202227 it ( "should preserve currency when scaling by 1" , ( ) => {
203228 const price = priceUsdc ( 1000000n ) ;
204229 const result = scalePrice ( price , 1 ) ;
@@ -239,6 +264,12 @@ describe("Currencies", () => {
239264 const result : PriceDai = scalePrice ( price , 0.5 ) ;
240265 expect ( result . currency ) . toBe ( "DAI" ) ;
241266 } ) ;
267+
268+ it ( "should preserve PriceEnsTokens type" , ( ) => {
269+ const price : PriceEnsTokens = priceEnsTokens ( 1000000000000000000n ) ;
270+ const result : PriceEnsTokens = scalePrice ( price , 0.5 ) ;
271+ expect ( result . currency ) . toBe ( "ENSTokens" ) ;
272+ } ) ;
242273 } ) ;
243274
244275 describe ( "error handling" , ( ) => {
@@ -536,4 +567,100 @@ describe("Currencies", () => {
536567 } ) ;
537568 } ) ;
538569 } ) ;
570+
571+ describe ( "parseEnsTokens" , ( ) => {
572+ describe ( "correct format and decimals" , ( ) => {
573+ it ( "should return PriceEnsTokens type with correct currency" , ( ) => {
574+ const result = parseEnsTokens ( "1" ) ;
575+ expect ( result ) . toHaveProperty ( "currency" , CurrencyIds . ENSTokens ) ;
576+ expect ( result ) . toHaveProperty ( "amount" ) ;
577+ expect ( result . currency ) . toBe ( CurrencyIds . ENSTokens ) ;
578+ expect ( typeof result . amount ) . toBe ( "bigint" ) ;
579+ } ) ;
580+
581+ it ( "should use 18 decimals from getCurrencyInfo" , ( ) => {
582+ const currencyInfo = getCurrencyInfo ( CurrencyIds . ENSTokens ) ;
583+ expect ( currencyInfo . decimals ) . toBe ( 18 ) ;
584+
585+ // Test that it uses 18 decimals by parsing a value with 18 decimal places
586+ const result = parseEnsTokens ( "1.123456789012345678" ) ;
587+ expect ( result . amount ) . toBe ( 1123456789012345678n ) ;
588+ } ) ;
589+
590+ it ( "should parse integer ENSTokens values correctly" , ( ) => {
591+ expect ( parseEnsTokens ( "1" ) ) . toEqual ( {
592+ currency : CurrencyIds . ENSTokens ,
593+ amount : 1000000000000000000n , // 1 $ENS = 10^18 smallest units
594+ } satisfies PriceEnsTokens ) ;
595+
596+ expect ( parseEnsTokens ( "0" ) ) . toEqual ( {
597+ currency : CurrencyIds . ENSTokens ,
598+ amount : 0n ,
599+ } satisfies PriceEnsTokens ) ;
600+
601+ expect ( parseEnsTokens ( "123" ) ) . toEqual ( {
602+ currency : CurrencyIds . ENSTokens ,
603+ amount : 123000000000000000000n ,
604+ } satisfies PriceEnsTokens ) ;
605+ } ) ;
606+
607+ it ( "should parse decimal ENSTokens values correctly" , ( ) => {
608+ expect ( parseEnsTokens ( "123.456789012345678" ) ) . toEqual ( {
609+ currency : CurrencyIds . ENSTokens ,
610+ amount : 123456789012345678000n ,
611+ } satisfies PriceEnsTokens ) ;
612+
613+ expect ( parseEnsTokens ( "0.001" ) ) . toEqual ( {
614+ currency : CurrencyIds . ENSTokens ,
615+ amount : 1000000000000000n ,
616+ } satisfies PriceEnsTokens ) ;
617+
618+ expect ( parseEnsTokens ( "0.5" ) ) . toEqual ( {
619+ currency : CurrencyIds . ENSTokens ,
620+ amount : 500000000000000000n ,
621+ } satisfies PriceEnsTokens ) ;
622+ } ) ;
623+
624+ it ( "should handle small ENSTokens values (minimum unit)" , ( ) => {
625+ expect ( parseEnsTokens ( "0.000000000000000001" ) ) . toEqual ( {
626+ currency : CurrencyIds . ENSTokens ,
627+ amount : 1n , // 1 smallest unit
628+ } satisfies PriceEnsTokens ) ;
629+ } ) ;
630+
631+ it ( "should handle maximum precision (18 decimal places)" , ( ) => {
632+ expect ( parseEnsTokens ( "1.123456789012345678" ) ) . toEqual ( {
633+ currency : CurrencyIds . ENSTokens ,
634+ amount : 1123456789012345678n ,
635+ } satisfies PriceEnsTokens ) ;
636+ } ) ;
637+ } ) ;
638+
639+ describe ( "error handling" , ( ) => {
640+ it ( "should throw on invalid format" , ( ) => {
641+ expect ( ( ) => parseEnsTokens ( "abc" ) ) . toThrow ( ) ;
642+ expect ( ( ) => parseEnsTokens ( "1.2.3" ) ) . toThrow ( ) ;
643+ } ) ;
644+
645+ it ( "should throw on empty string" , ( ) => {
646+ expect ( ( ) => parseEnsTokens ( "" ) ) . toThrow ( "amount must be a non-negative decimal string" ) ;
647+ } ) ;
648+
649+ it ( "should throw on whitespace-only string" , ( ) => {
650+ expect ( ( ) => parseEnsTokens ( " " ) ) . toThrow ( "amount must be a non-negative decimal string" ) ;
651+ expect ( ( ) => parseEnsTokens ( "\t" ) ) . toThrow ( "amount must be a non-negative decimal string" ) ;
652+ expect ( ( ) => parseEnsTokens ( "\n" ) ) . toThrow ( "amount must be a non-negative decimal string" ) ;
653+ } ) ;
654+
655+ it ( "should throw on negative values" , ( ) => {
656+ expect ( ( ) => parseEnsTokens ( "-1" ) ) . toThrow ( "amount must be a non-negative decimal string" ) ;
657+ expect ( ( ) => parseEnsTokens ( "-0.5" ) ) . toThrow (
658+ "amount must be a non-negative decimal string" ,
659+ ) ;
660+ expect ( ( ) => parseEnsTokens ( "-123.456" ) ) . toThrow (
661+ "amount must be a non-negative decimal string" ,
662+ ) ;
663+ } ) ;
664+ } ) ;
665+ } ) ;
539666} ) ;
0 commit comments