Balance
This guide explains how to check token balances using the Cross Unity SDK.
1. Retrieve native coin from blockchain node
// Update the latest balance information from the Cross API.
await CrossSdk.UpdateBalance();
// Get the currently connected account information.
var account = await CrossSdk.GetAccountAsync();
// Retrieve the native token balance of the account.
var balance = await CrossSdk.Evm.GetBalanceAsync(account.Address);
// Display the balance as a notification.
Notification.ShowMessage($"Balance: {Web3.Convert.FromWei(balance)} CROSS");
2. Retrieve all native coin and tokens from Cross API
// Update the latest balance information from the Cross API.
await CrossSdk.UpdateBalance();
// Retrieve all token information (native tokens, ERC20, etc.).
var tokens = CrossSdk.GetTokens();
string message = "Tokens:\n";
foreach (var token in tokens)
{
string symbol = token.Symbol; // Token symbol (e.g., ETH, USDT)
string numeric = token.Quantity.numeric; // Token quantity (string)
int decimals = int.Parse(token.Quantity.decimals); // Decimal places
// Convert token balance to readable decimal format
var balance = Web3.Convert.FromWei(BigInteger.Parse(numeric), decimals);
message += $"{symbol}: {balance}\n";
}
// Display all token balances as a notification.
Notification.ShowMessage(message);
Updated 5 days ago