web3-error-helper
Version:
> 🛠️ Turn confusing Web3 errors into clear, human-friendly messages for developers and users alike.
128 lines (127 loc) • 3.66 kB
JavaScript
/**
* Chain statistics and aggregation
*
* This module provides comprehensive statistical analysis and reporting capabilities
* for blockchain networks. It aggregates data from both built-in and custom chains,
* generates usage summaries, and provides insights into chain distribution and
* configuration patterns. Focuses on data aggregation and reporting without side effects.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getChainStats = getChainStats;
exports.getChainDistribution = getChainDistribution;
exports.getChainUsageSummary = getChainUsageSummary;
const built_in_chain_manager_1 = require("./built-in-chain-manager");
const chain_registry_1 = require("./chain-registry");
/**
* Get comprehensive chain statistics
*
* @returns Object with detailed chain statistics
*
* @example
* ```typescript
* const stats = getChainStats();
* console.log(stats);
* // {
* // total: 10,
* // builtIn: 8,
* // custom: 2,
* // chains: ['ethereum', 'polygon', 'my-custom-chain', ...]
* // }
* ```
*/
function getChainStats() {
const builtInStats = (0, built_in_chain_manager_1.getBuiltInChainStats)();
const customChains = chain_registry_1.customChainRegistry
.getAll()
.map(config => config.chainId);
const allChains = [...customChains, ...builtInStats.chains];
return {
total: allChains.length,
builtIn: builtInStats.count,
custom: customChains.length,
chains: allChains,
};
}
/**
* Get chain distribution statistics
*
* @returns Object with chain type distribution
*
* @example
* ```typescript
* const distribution = getChainDistribution();
* console.log(distribution);
* // {
* // builtInPercentage: 80,
* // customPercentage: 20,
* // ratio: '4:1'
* // }
* ```
*/
function getChainDistribution() {
const stats = getChainStats();
if (stats.total === 0) {
return {
builtInPercentage: 0,
customPercentage: 0,
ratio: '0:0',
};
}
const builtInPercentage = Math.round((stats.builtIn / stats.total) * 100);
const customPercentage = Math.round((stats.custom / stats.total) * 100);
// Calculate ratio
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
const divisor = gcd(stats.builtIn, stats.custom);
const ratio = `${stats.builtIn / divisor}:${stats.custom / divisor}`;
return {
builtInPercentage,
customPercentage,
ratio,
};
}
/**
* Get chain usage summary
*
* @returns Object with chain usage information
*
* @example
* ```typescript
* const summary = getChainUsageSummary();
* console.log(summary);
* // {
* // mostUsed: 'ethereum',
* // leastUsed: 'fantom',
* // averageUsage: 12.5
* // }
* ```
*/
function getChainUsageSummary() {
const stats = getChainStats();
if (stats.chains.length === 0) {
return {
mostUsed: '',
leastUsed: '',
averageUsage: 0,
};
}
// Use a simple heuristic based on chain order
// This could be enhanced to track actual usage metrics in the future
const builtInChains = stats.chains.filter(chain => [
'ethereum',
'polygon',
'arbitrum',
'optimism',
'bsc',
'avalanche',
'fantom',
'base',
].includes(chain));
return {
mostUsed: builtInChains[0] || stats.chains[0] || '',
leastUsed: builtInChains[builtInChains.length - 1] ||
stats.chains[stats.chains.length - 1] ||
'',
averageUsage: stats.total / 2, // Placeholder calculation
};
}
;