Skip to content

Commit

Permalink
Merge pull request #212 from gentlementlegen/fix/failed-network
Browse files Browse the repository at this point in the history
fix: optimal RPC now removes faulty elements properly
  • Loading branch information
0x4007 authored Mar 26, 2024
2 parents cd49591 + 6f3d6bc commit 349a419
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ export async function getFastestRpcProvider(networkId: number) {

// Get all valid latencies from localStorage and find the fastest RPC
const sortedLatencies = validLatencies.sort((a, b) => a[1] - b[1]);
const optimalRPC = sortedLatencies[0][0].split("_").slice(0, -1).join("_"); // Remove the network ID from the key
const optimalRpc = sortedLatencies[0][0];
const optimalRpcName = optimalRpc.split("_").slice(0, -1).join("_"); // Remove the network ID from the key

try {
const rpcProvider = new ethers.providers.JsonRpcProvider(optimalRPC, {
name: optimalRPC,
const rpcProvider = new ethers.providers.JsonRpcProvider(optimalRpcName, {
name: optimalRpcName,
chainId: networkId,
});
// We check if the networks positively gives us a block when requested to ensure the network works
// because some of them appear to be constantly failing such as https://gnosis.api.onfinality.io/public
await rpcProvider.getBlock(1);
return rpcProvider;
} catch (e) {
console.warn(`Failed to get a block using network ${optimalRPC}, will try with another.`);
delete latencies[optimalRPC];
console.warn(`Failed to get a block using network ${optimalRpc}, will try with another.`);
delete latencies[optimalRpc];
}
}

Expand Down

0 comments on commit 349a419

Please sign in to comment.