Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimal RPC now removes faulty elements properly #212

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading