Skip to content

Commit

Permalink
fix: imx balance decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Dec 16, 2024
1 parent 98881ea commit 9138bd8
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Assets/Shared/Scripts/Domain/GetTokenBalanceUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Immutable.Orderbook.Api;
using Immutable.Orderbook.Client;
using Immutable.Passport;
using UnityEngine;

namespace HyperCasual.Runner
{
Expand Down Expand Up @@ -38,16 +39,14 @@ public async UniTask<string> GetImxBalance()
{
var balanceHex = await Passport.Instance.ZkEvmGetBalance(SaveManager.Instance.WalletAddress);

// Convert hex to dec
var balanceDec = BigInteger.Parse(balanceHex.Replace("0x", ""), NumberStyles.HexNumber);

// Convert smallest unit to main unit
var balance = BigInteger.Divide(balanceDec, BigInteger.Pow(10, 18));

// Round to two decimal places
var decimalValue = (decimal)balance;
var balance = BigInteger.Parse(balanceHex.Replace("0x", ""), NumberStyles.HexNumber);

// Convert from smallest unit to main unit
var decimalValue = (decimal)balance / 1_000_000_000_000_000_000m;

// Round to 2 decimal places
var roundedValue = Math.Round(decimalValue, 2);

return roundedValue.ToString("F2");
}
}
Expand Down

0 comments on commit 9138bd8

Please sign in to comment.