Skip to content

Commit

Permalink
ETH <> Gwei <> Wei - double -> decimal (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored May 17, 2024
1 parent 890e617 commit bb82aea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Assets/Thirdweb/Core/Scripts/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public class PolygonGasStationResult
[System.Serializable]
public class GasStationResult
{
public double maxPriorityFee;
public double maxFee;
public decimal maxPriorityFee;
public decimal maxFee;
}
}
16 changes: 9 additions & 7 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -652,9 +654,9 @@ public async static Task<GasPriceParameters> 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)
Expand Down

0 comments on commit bb82aea

Please sign in to comment.