@@ -23,7 +23,7 @@ public static class Utils
2323 {
2424 public const string AddressZero = "0x0000000000000000000000000000000000000000" ;
2525 public const string NativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" ;
26- public const double DECIMALS_18 = 1000000000000000000 ;
26+ public const decimal DECIMALS_18 = 1000000000000000000 ;
2727
2828 public static string [ ] ToJsonStringArray ( params object [ ] args )
2929 {
@@ -93,9 +93,9 @@ public static long UnixTimeNowMs()
9393
9494 public static string ToWei ( this string eth )
9595 {
96- if ( ! double . TryParse ( eth , NumberStyles . Number , CultureInfo . InvariantCulture , out double ethDouble ) )
96+ if ( ! decimal . TryParse ( eth , NumberStyles . Number , CultureInfo . InvariantCulture , out decimal ethDecimal ) )
9797 throw new ArgumentException ( "Invalid eth value." ) ;
98- BigInteger wei = ( BigInteger ) ( ethDouble * DECIMALS_18 ) ;
98+ BigInteger wei = ( BigInteger ) ( ethDecimal * DECIMALS_18 ) ;
9999 return wei . ToString ( ) ;
100100 }
101101
@@ -106,15 +106,17 @@ public static string ToEth(this string wei, int decimalsToDisplay = 4, bool addC
106106
107107 public static string FormatERC20 ( this string wei , int decimalsToDisplay = 4 , int decimals = 18 , bool addCommas = true )
108108 {
109- decimals = decimals == 0 ? 18 : decimals ;
110109 if ( ! BigInteger . TryParse ( wei , out BigInteger weiBigInt ) )
111110 throw new ArgumentException ( "Invalid wei value." ) ;
112- double eth = ( double ) weiBigInt / Math . Pow ( 10.0 , decimals ) ;
111+
112+ decimal eth = ( decimal ) weiBigInt / ( decimal ) Math . Pow ( 10 , decimals ) ;
113113 string format = addCommas ? "#,0" : "#0" ;
114+
114115 if ( decimalsToDisplay > 0 )
115116 format += "." ;
116117 for ( int i = 0 ; i < decimalsToDisplay ; i ++ )
117118 format += "#" ;
119+
118120 return eth . ToString ( format ) ;
119121 }
120122
@@ -652,9 +654,9 @@ public async static Task<GasPriceParameters> GetPolygonGasPriceParameters(int ch
652654 return new GasPriceParameters ( GweiToWei ( data . fast . maxFee ) , GweiToWei ( data . fast . maxPriorityFee ) ) ;
653655 }
654656
655- public static BigInteger GweiToWei ( double gweiAmount )
657+ public static BigInteger GweiToWei ( decimal gweiAmount )
656658 {
657- return new BigInteger ( gweiAmount * 1e9 ) ;
659+ return new BigInteger ( gweiAmount * 1_000_000_000m ) ; // 1e9 in decimal
658660 }
659661
660662 public static string BigIntToHex ( this BigInteger number )
0 commit comments