/**
 * Web3 utilities with memoization and retry logic
 */
class Web3Cache {
    #cache = new Map();
    #ttl = 5 * 60 * 1000; // 5 minutes

    async getWithCache(key, fetchFn, maxRetries = 3) {
        if (this.#cache.has(key)) {
            const { value, timestamp } = this.#cache.get(key);
            if (Date.now() - timestamp < this.#ttl) {
                return value;
            }
            this.#cache.delete(key);
        }

        let lastError;
        for (let i = 0; i < maxRetries; i++) {
            try {
                const value = await fetchFn();
                this.#cache.set(key, {
                    value,
                    timestamp: Date.now()
                });
                return value;
            } catch (error) {
                lastError = error;
                await new Promise(resolve => 
                    setTimeout(resolve, Math.pow(2, i) * 1000)
                );
            }
        }
        
        throw new Error(`Failed after ${maxRetries} retries: ${lastError.message}`);
    }

    clearCache() {
        this.#cache.clear();
    }
}

export { Web3Cache };