> loading_
# Before: A single large request that may now fail
# const proof = await provider.send('eth_getProof', [
# CONTRACT_ADDRESS,
# [key1, key2, key3, ... key1000], # This large array might exceed the limit
# 'latest'
# ]);
# After: Batching requests to respect the new limit
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
// 1. Set up your client and define constants
const client = createPublicClient({
chain: mainnet, // Replace with GOAT Network chain info
transport: http('https://rpc.goat.network')
});
const CONTRACT_ADDRESS = '0x...'; // Your target contract address
const ALL_STORAGE_KEYS = []; // Assume this is a large array with 1000s of keys
const MAX_KEYS_PER_REQUEST = 100; // Define a safe batch size
// 2. Create a function to handle batching
async function getBatchedProofs(address, keys, blockTag = 'latest') {
const allProofs = [];
console.log(`Fetching proofs for ${keys.length} keys in batches of ${MAX_KEYS_PER_REQUEST}...`);
for (let i = 0; i < keys.length; i += MAX_KEYS_PER_REQUEST) {
const batch = keys.slice(i, i + MAX_KEYS_PER_REQUEST);
console.log(`- Fetching batch ${i / MAX_KEYS_PER_REQUEST + 1}`);
try {
// 3. Call eth_getProof for each smaller chunk
const proof = await client.request({
method: 'eth_getProof',
params: [address, batch, blockTag]
});
// Note: You would need logic here to merge the proofs if required by your application
allProofs.push(proof);
} catch (error) {
console.error(`Error fetching batch starting at index ${i}:`, error);
// Implement retry logic or handle the error as needed
break;
}
}
console.log('Finished fetching all batches.');
return allProofs;
}
// 4. Execute the batched call
getBatchedProofs(CONTRACT_ADDRESS, ALL_STORAGE_KEYS);