get_user_balance
Retrieve token balances for a user on a specified blockchain chain using chain ID, token contract address, and user address.
Instructions
Get user token balance on a specific chain
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chainId | Yes | Chain ID to check balance on | |
| tokenAddress | Yes | Token contract address | |
| userAddress | Yes | User address to check balance for |
Input Schema (JSON Schema)
{
"additionalProperties": false,
"properties": {
"chainId": {
"description": "Chain ID to check balance on",
"oneOf": [
{
"type": "number"
},
{
"type": "string"
}
]
},
"tokenAddress": {
"description": "Token contract address",
"type": "string"
},
"userAddress": {
"description": "User address to check balance for",
"type": "string"
}
},
"required": [
"chainId",
"tokenAddress",
"userAddress"
],
"type": "object"
}
Implementation Reference
- src/index.ts:447-458 (handler)MCP tool handler for 'get_user_balance' that validates input with BalanceQuerySchema and delegates to BalanceService.getBalance()private async getUserBalance(args: any): Promise<MCPToolResult> { try { const request = BalanceQuerySchema.parse(args); const result = await this.balanceService.getBalance(request); return createSuccessResponse(result); } catch (error) { return createErrorResponse( error instanceof Error ? error.message : String(error), 'BALANCE_ERROR' ); } }
- src/types/index.ts:182-188 (schema)Zod schema defining the input parameters for get_user_balance: chainId, tokenAddress, userAddressexport const BalanceQuerySchema = z.object({ chainId: z.union([z.number(), z.string()]), tokenAddress: z.string(), userAddress: z.string(), }); export type BalanceQuery = z.infer<typeof BalanceQuerySchema>;
- src/index.ts:265-290 (registration)Tool registration in ListToolsRequestSchema handler, defining name, description, and inputSchema matching BalanceQuerySchema{ name: 'get_user_balance', description: 'Get user token balance on a specific chain', inputSchema: { type: 'object', properties: { chainId: { oneOf: [ { type: 'number' }, { type: 'string' }, ], description: 'Chain ID to check balance on', }, tokenAddress: { type: 'string', description: 'Token contract address', }, userAddress: { type: 'string', description: 'User address to check balance for', }, }, required: ['chainId', 'tokenAddress', 'userAddress'], additionalProperties: false, }, },
- src/services/balance.ts:55-74 (helper)Core helper method in BalanceService that dispatches balance queries to chain-specific implementations based on chain type (EVM, Solana, Sui, Cosmos)async getBalance(query: BalanceQuery): Promise<BalanceResponse> { const { chainId, tokenAddress, userAddress } = query; const chainIdTyped = chainId as SupportedChainId; try { if (isEVMChain(chainIdTyped)) { return await this.getEVMBalance(chainIdTyped, tokenAddress, userAddress); } else if (isSolanaChain(chainIdTyped)) { return await this.getSolanaBalance(chainIdTyped, tokenAddress, userAddress); } else if (isSuiChain(chainIdTyped)) { return await this.getSuiBalance(chainIdTyped, tokenAddress, userAddress); } else if (isCosmosChain(chainIdTyped)) { return await this.getCosmosBalance(chainIdTyped, tokenAddress, userAddress); } else { throw new Error(`Unsupported chain: ${chainId}`); } } catch (error) { throw new Error(`Failed to get balance: ${error instanceof Error ? error.message : String(error)}`); } }