Skip to content

Commit

Permalink
docs: unity get token balance
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Nov 19, 2024
1 parent 0ff82c1 commit 22dc98a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 45 deletions.
30 changes: 30 additions & 0 deletions Assets/Shared/Scripts/Domain/GetTokenBalanceUseCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Cysharp.Threading.Tasks;
using Immutable.Orderbook.Api;
using Immutable.Orderbook.Client;

namespace HyperCasual.Runner
{
public class GetTokenBalanceUseCase
{
private static readonly Lazy<GetTokenBalanceUseCase> s_Instance = new(() => new GetTokenBalanceUseCase());

private readonly OrderbookApi m_OrderbookApi = new(new Configuration { BasePath = Config.BASE_URL });

private GetTokenBalanceUseCase() { }

public static GetTokenBalanceUseCase Instance => s_Instance.Value;

/// <summary>
/// Gets the user's ERC20 token balance
/// </summary>
public async UniTask<string> GetBalance()
{
var result = await m_OrderbookApi.TokenBalanceAsync(
walletAddress: SaveManager.Instance.WalletAddress,
contractAddress: Contract.TOKEN
);
return result.Quantity;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Shared/Scripts/Domain/GetTokenBalanceUseCase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions Assets/Shared/Scripts/UI/BalanceObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable enable
using System;
using System.Net.Http;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
Expand All @@ -27,24 +26,12 @@ public class BalanceObject : MonoBehaviour

try
{
using var client = new HttpClient();
var response = await client.GetAsync($"{Config.SERVER_URL}/balance?address={SaveManager.Instance.WalletAddress}");
var balance = await GetTokenBalanceUseCase.Instance.GetBalance();
m_ValueText.text = $"{balance} IMR";

if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Debug.Log($"Balance response: {responseBody}");
if (gameObject != null) gameObject.SetActive(true);

var balanceResponse = JsonUtility.FromJson<BalanceResponse>(responseBody);
if (balanceResponse?.quantity != null) m_ValueText.text = $"{balanceResponse.quantity} IMR";

if (gameObject != null) gameObject.SetActive(true);

return balanceResponse?.quantity == "0.0" ? "0" : balanceResponse?.quantity;
}

Debug.LogWarning("Failed to retrieve the balance.");
return null;
return balance == "0.0" ? "0" : balance;
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"com.unity.nuget.newtonsoft-json": "3.2.0",
"com.cysharp.unitask": "2.3.3"
},
"hash": "b136ff40a2b0486fa9057b49b022d4d51581b82c"
"hash": "9ed3136ecf878ee1cf229fdf8c7f064bdb6e6cda"
},
"com.nobi.roundedcorners": {
"version": "https://github.com/kirevdokimov/Unity-UI-Rounded-Corners.git",
Expand Down
3 changes: 1 addition & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ PlayerSettings:
16:9: 1
Others: 1
bundleVersion: 0.1.0
preloadedAssets:
- {fileID: 11400000, guid: 7abe46dede6624a60a115c533b19a2a8, type: 2}
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1
Expand Down
25 changes: 0 additions & 25 deletions mint-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,6 @@ router.post('/mint/skin', async (req: Request, res: Response) => {
},
);

// In-game ERC20 balance
router.get('/balance', async (req: Request, res: Response) => {
try {
if (tokenContractAddress) {
// Get the address
const address = req.query.address ?? null;

// Call balanceOf
const abi = ['function balanceOf(address account) view returns (uint256)'];
const contract = new Contract(tokenContractAddress, abi, zkEvmProvider);
const balance = await contract.balanceOf(address);

return res.status(200).json({
quantity: utils.formatUnits(balance, 18),
});
} else {
return res.status(500).json({});
}

} catch (error) {
console.log(error);
return res.status(400).json({ message: 'Failed to mint to user' });
}
},
);

// Packs
const galacticShieldId = 1;
Expand Down

0 comments on commit 22dc98a

Please sign in to comment.