Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETH <> Gwei <> Wei - double -> decimal #188

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading