Skip to content

Commit bb82aea

Browse files
authored
ETH <> Gwei <> Wei - double -> decimal (#188)
1 parent 890e617 commit bb82aea

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

Assets/Thirdweb/Core/Scripts/Types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public class PolygonGasStationResult
612612
[System.Serializable]
613613
public class GasStationResult
614614
{
615-
public double maxPriorityFee;
616-
public double maxFee;
615+
public decimal maxPriorityFee;
616+
public decimal maxFee;
617617
}
618618
}

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)