From 51cc2a9c0a0445f459ea5eb0ce7ab5f3b42df7b7 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Fri, 17 May 2024 20:46:52 +0300 Subject: [PATCH] ETH <> Gwei <> Wei - double -> decimal Avoid floating point errors --- Assets/Thirdweb/Core/Scripts/Types.cs | 4 ++-- Assets/Thirdweb/Core/Scripts/Utils.cs | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Assets/Thirdweb/Core/Scripts/Types.cs b/Assets/Thirdweb/Core/Scripts/Types.cs index 54d30053b..5a64b57ee 100644 --- a/Assets/Thirdweb/Core/Scripts/Types.cs +++ b/Assets/Thirdweb/Core/Scripts/Types.cs @@ -612,7 +612,7 @@ public class PolygonGasStationResult [System.Serializable] public class GasStationResult { - public double maxPriorityFee; - public double maxFee; + public decimal maxPriorityFee; + public decimal maxFee; } } diff --git a/Assets/Thirdweb/Core/Scripts/Utils.cs b/Assets/Thirdweb/Core/Scripts/Utils.cs index 0d808ad74..869ca51af 100644 --- a/Assets/Thirdweb/Core/Scripts/Utils.cs +++ b/Assets/Thirdweb/Core/Scripts/Utils.cs @@ -23,7 +23,7 @@ public static class Utils { public const string AddressZero = "0x0000000000000000000000000000000000000000"; public const string NativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; - public const double DECIMALS_18 = 1000000000000000000; + public const decimal DECIMALS_18 = 1000000000000000000; public static string[] ToJsonStringArray(params object[] args) { @@ -93,9 +93,9 @@ public static long UnixTimeNowMs() public static string ToWei(this string eth) { - if (!double.TryParse(eth, NumberStyles.Number, CultureInfo.InvariantCulture, out double ethDouble)) + if (!decimal.TryParse(eth, NumberStyles.Number, CultureInfo.InvariantCulture, out decimal ethDecimal)) throw new ArgumentException("Invalid eth value."); - BigInteger wei = (BigInteger)(ethDouble * DECIMALS_18); + BigInteger wei = (BigInteger)(ethDecimal * DECIMALS_18); return wei.ToString(); } @@ -106,15 +106,17 @@ public static string ToEth(this string wei, int decimalsToDisplay = 4, bool addC public static string FormatERC20(this string wei, int decimalsToDisplay = 4, int decimals = 18, bool addCommas = true) { - decimals = decimals == 0 ? 18 : decimals; if (!BigInteger.TryParse(wei, out BigInteger weiBigInt)) throw new ArgumentException("Invalid wei value."); - double eth = (double)weiBigInt / Math.Pow(10.0, decimals); + + decimal eth = (decimal)weiBigInt / (decimal)Math.Pow(10, decimals); string format = addCommas ? "#,0" : "#0"; + if (decimalsToDisplay > 0) format += "."; for (int i = 0; i < decimalsToDisplay; i++) format += "#"; + return eth.ToString(format); } @@ -652,9 +654,9 @@ public async static Task GetPolygonGasPriceParameters(int ch return new GasPriceParameters(GweiToWei(data.fast.maxFee), GweiToWei(data.fast.maxPriorityFee)); } - public static BigInteger GweiToWei(double gweiAmount) + public static BigInteger GweiToWei(decimal gweiAmount) { - return new BigInteger(gweiAmount * 1e9); + return new BigInteger(gweiAmount * 1_000_000_000m); // 1e9 in decimal } public static string BigIntToHex(this BigInteger number)