Get token balance
Displaying the player's in-game currency balance is essential for building an in-game marketplace. It allows players to see if they have enough funds to complete a purchase.
💡Who is this for?
Developers who want to build an in-game marketplace in Unity.
💡Alpha version
This module is experimental and may change in the future. We recommend using it for testing purposes only.
The Orderbook package enables you to display the player's in-game currency balance. Below is an example implementation:
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: LOGGED_IN_WALLET, // Replace with player's wallet
contractAddress: ERC20_CONTRACT_ADDRESS // Replace with in-game ERC20 contract address
);
return result.Quantity;
}
}
}
You can find a complete example in the Unity sample game.
Related content
Cancel Order
Further information about cancelling orders can be found in the Typescript documentation.
Learn more
Order Statuses
Understand the different order statuses and what they mean using this reference table.
Learn more
Unity Game Tutorial
For information on other aspects of building a game with Unity, check out our “Build a Game” tutorial.
Learn more