Skip to content

Instantly share code, notes, and snippets.

@thecompez
Created November 14, 2025 17:33
Show Gist options
  • Select an option

  • Save thecompez/fe77d9625a5266f1c314bcdbb7de9a8b to your computer and use it in GitHub Desktop.

Select an option

Save thecompez/fe77d9625a5266f1c314bcdbb7de9a8b to your computer and use it in GitHub Desktop.
interface TokenBalanceResponse {
status: string;
message: string;
result: string;
}
interface WalletBalance {
balance: string;
formattedBalance: string;
success: boolean;
error?: string;
}
/**
* Get GENY token balance for a wallet address
* @param walletAddress - The wallet address to check balance for
* @param baseScanApiKey - Your BaseScan API key
* @param tokenContractAddress - GENY token contract address (defaults to GENY)
* @returns Promise with wallet balance information
*/
async function getGENYTokenBalance(
walletAddress: string,
baseScanApiKey: string,
tokenContractAddress: string = "0x2826272e099b12aa3f661bba0adc5130d3630382" // Replace with actual GENY contract address
): Promise<WalletBalance> {
const baseUrl = "https://api.etherscan.io/v2/api";
// Validate inputs
if (!walletAddress || !baseScanApiKey) {
return {
balance: "0",
formattedBalance: "0",
success: false,
error: "Wallet address and API key are required"
};
}
try {
// Construct the API URL
const url = new URL(baseUrl);
url.searchParams.append("chainId", "8453"); // Base chain
url.searchParams.append("module", "account");
url.searchParams.append("action", "tokenbalance");
url.searchParams.append("contractaddress", tokenContractAddress);
url.searchParams.append("address", walletAddress);
url.searchParams.append("tag", "latest");
url.searchParams.append("apikey", baseScanApiKey);
const response = await fetch(url.toString());
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: TokenBalanceResponse = await response.json();
if (data.status === "1" && data.message === "OK") {
// Convert from wei to tokens (assuming 18 decimals like most ERC-20 tokens)
const balanceWei = data.result;
const balance = (parseInt(balanceWei) / 1e18).toString();
return {
balance: balanceWei,
formattedBalance: balance,
success: true
};
} else {
return {
balance: "0",
formattedBalance: "0",
success: false,
error: data.message || "Unknown API error"
};
}
} catch (error) {
console.error("Error fetching GENY token balance:", error);
return {
balance: "0",
formattedBalance: "0",
success: false,
error: error instanceof Error ? error.message : "Unknown error occurred"
};
}
}
// Example usage:
async function example() {
const walletAddress = "0xUserWalletAddress";
const apiKey = "YourBaseScanApiKey";
const genyContractAddress = "0x2826272e099b12aa3f661bba0adc5130d3630382"; // Actual GENY contract address
const result = await getGENYTokenBalance(walletAddress, apiKey, genyContractAddress);
if (result.success) {
console.log(`GENY Balance: ${result.formattedBalance} GENY`);
console.log(`Raw Balance: ${result.balance} wei`);
} else {
console.error(`Error: ${result.error}`);
}
}
// Export for use in other modules
export { getGENYTokenBalance };
export type { WalletBalance, TokenBalanceResponse };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment