import { z } from 'zod';
import { DataiClient } from '../utils/api-client.js';
import { WalletAddressSchema } from '../utils/validation.js';
import { DataAIError, ValidationError, APIError, NetworkError, TimeoutError } from '../utils/error-handler.js';
/**
* Tool 6: Get User Overall Balance All Chains
*
* COMPREHENSIVE DESCRIPTION:
* This tool fetches the overall balance for a specific wallet address across ALL supported blockchain networks.
* It provides aggregated balance information in multiple currencies (USD, EUR, AUD, etc.) for the complete
* portfolio across all chains. Perfect for total portfolio valuation and cross-chain wealth assessment.
*
* USE CASES:
* - Complete portfolio valuation
* - Multi-currency balance reporting
* - Tax reporting and compliance
* - Wealth tracking across all chains
* - Portfolio performance monitoring
* - Cross-chain asset allocation analysis
*
* SUPPORTED CURRENCIES:
* - USD (US Dollar)
* - EUR (Euro)
* - AUD (Australian Dollar)
* - GBP (British Pound)
* - JPY (Japanese Yen)
* - CAD (Canadian Dollar)
* - CHF (Swiss Franc)
*
* RESPONSE FORMAT:
* Returns overall balance in multiple currencies:
* {
* "USD": 45420.50,
* "EUR": 41250.30,
* "AUD": 67890.20,
* "GBP": 35670.10,
* "JPY": 6789012.00,
* "CAD": 61230.40,
* "CHF": 42180.60
* }
*
* PERFORMANCE:
* - Typical response time: 3-6 seconds (cross-chain aggregation)
* - Comprehensive data across all supported chains
* - Real-time currency conversion
* - Cached exchange rates (updated every 5 minutes)
*/
export function createGetUserOverallBalanceAllChainsTool(client: DataiClient) {
return {
name: "get_overall_balance_all_c",
description: "Get overall balance for a wallet across ALL supported blockchain networks in multiple currencies. Provides complete portfolio valuation including USD, EUR, AUD, and other major currencies for comprehensive wealth tracking.",
parameters: z.object({
wallet: WalletAddressSchema.describe("Ethereum wallet address (42-character hex string starting with 0x) to get overall balance for across all chains")
}),
annotations: {
readOnlyHint: true,
destructiveHint: false,
openWorldHint: true,
idempotentHint: true,
streamingHint: false,
title: "Overall Balance All Chains",
category: "Portfolio Valuation",
tags: ["balance", "cross-chain", "multi-currency", "portfolio", "valuation", "comprehensive"]
},
execute: async (args: { wallet: string }, context: { log: any }) => {
const { log } = context;
const startTime = Date.now();
const executionId = crypto.randomUUID().slice(0, 8);
log.info("Starting get_overall_balance_all_c", {
executionId,
wallet: args.wallet
});
try {
// Validate wallet address
const validatedArgs = { wallet: WalletAddressSchema.parse(args.wallet) };
// Make API request
const response = await client.getUserOverallBalanceAllChains(validatedArgs.wallet);
const duration = Date.now() - startTime;
const currencyCount = typeof response.data === 'object' && response.data !== null ?
Object.keys(response.data).length : 0;
log.info("Overall balance all chains fetched successfully", {
executionId,
wallet: validatedArgs.wallet,
currencyCount,
duration: `${duration}ms`
});
return {
content: [{
type: "text" as const,
text: JSON.stringify(response.data, null, 2)
}]
};
} catch (error: any) {
const duration = Date.now() - startTime;
log.error("Failed to fetch overall balance across all chains", {
executionId,
wallet: args.wallet,
duration: `${duration}ms`,
error: error.message,
errorType: error.constructor.name,
errorCode: error.code || 'UNKNOWN',
success: false
});
// Transform error for user-friendly response
if (error instanceof ValidationError) {
throw new DataAIError(
`Invalid wallet address: ${args.wallet}. Please provide a valid Ethereum address (42 characters, starting with 0x).`,
'VALIDATION_ERROR'
);
} else if (error instanceof TimeoutError) {
throw new DataAIError(
`Request timed out after ${duration}ms. Cross-chain balance aggregation may take longer during high load. Please try again in a few moments.`,
'TIMEOUT_ERROR'
);
} else if (error instanceof NetworkError) {
throw new DataAIError(
'Network error occurred while fetching overall balance across all chains. Please check your connection and try again.',
'NETWORK_ERROR'
);
} else if (error instanceof APIError) {
if (error.message.includes('404')) {
throw new DataAIError(
`No balance data found for wallet ${args.wallet}. The wallet may be new or not have any assets across supported chains.`,
'NOT_FOUND'
);
} else if (error.message.includes('429')) {
throw new DataAIError(
'Rate limit exceeded. Cross-chain balance queries are resource-intensive. Please wait a moment before making another request.',
'RATE_LIMIT_EXCEEDED'
);
} else if (error.message.includes('401') || error.message.includes('403')) {
throw new DataAIError(
'Authentication failed. Please check your API credentials.',
'AUTHENTICATION_ERROR'
);
} else {
throw new DataAIError(`API error: ${error.message}`, 'API_ERROR');
}
} else {
throw new DataAIError(
`Failed to fetch overall balance across all chains: ${error.message}. Please try again or contact support if the issue persists.`,
'UNKNOWN_ERROR'
);
}
}
}
};
}