Skip to main content

getSplTokenBalance

Queries the balance of a specific SPL Token for a given wallet. The function automatically finds the wallet's Associated Token Account (ATA) for that token and returns its balance.

Important: This function returns the raw integer balance. To get a human-readable value, you must divide the raw balance by 10 raised to the power of the token's decimals. For example, if a token has 6 decimals, you would divide the raw balance by $10^6$. You can fetch the correct number of decimals using the getTokenDecimals function.

Parameters

ParameterTypeDescription
walletPubkeyBase58const String&The Base58 address of the wallet whose token balance is being checked.
tokenMintBase58const String&The Base58 address of the SPL Token's mint (i.e., the token's contract address).
outBalanceuint64_t&An output parameter that will be populated with the raw, integer token balance.

Return Value

  • bool: Returns true if the balance was fetched successfully. Returns false if the wallet does not have a token account for this mint or if an error occurs.

Example Code (main.cpp)

#include <Arduino.h>
#include <WiFi.h>
#include "Infratic-lib.h"

// --- WiFi Settings ---
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// --- Solana Settings ---
const String SOLANA_RPC_URL = "[https://api.devnet.solana.com](https://api.devnet.solana.com)";
const String MY_PUBLIC_KEY_BASE58 = "YOUR_PUBLIC_KEY_BASE58"; // Enter your wallet address
// As an example, we use the Devnet USDC mint address.
const String TOKEN_MINT_BASE58 = "Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr";
Infratic solana(SOLANA_RPC_URL);

void setup() {
Serial.begin(115200);
delay(1000);

// Connect to WiFi
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected!");

Serial.println("\n=== Get SPL Token Balance Example ===");
Serial.println("Checking USDC balance for: " + MY_PUBLIC_KEY_BASE58);

uint64_t rawBalance = 0;
if (solana.getSplTokenBalance(MY_PUBLIC_KEY_BASE58, TOKEN_MINT_BASE58, rawBalance)) {
Serial.print("✅ Raw Token Balance: ");
Serial.println((unsigned long)rawBalance);

// To make it readable, we must get the token's decimals
uint8_t decimals = 0;
if (solana.getTokenDecimals(TOKEN_MINT_BASE58, decimals)) {
double readableBalance = (double)rawBalance / pow(10, decimals);
Serial.print(" -> Token has ");
Serial.print(decimals);
Serial.println(" decimals.");
Serial.print(" -> Readable Balance: ");
Serial.println(readableBalance, decimals);
}
} else {
Serial.println("❌ Failed to get token balance. The wallet may not have an account for this token.");
}
}

void loop() {
// This example runs once in setup.
}