Add Polygon RPC endpoints to the default provider (#3689).

This commit is contained in:
Richard Moore 2023-07-27 20:11:08 -04:00
parent 8f0a50921a
commit 23704a9c44

@ -36,15 +36,23 @@ export function getDefaultProvider(network: string | Networkish | WebSocketLike,
return new WebSocketProvider(network); return new WebSocketProvider(network);
} }
// Get the network name, if possible // Get the network and name, if possible
let name: null | string = null; let staticNetwork: null | Network = null;
try { try {
name = Network.from(network).name; staticNetwork = Network.from(network);
} catch (error) { } } catch (error) { }
const providers: Array<AbstractProvider> = [ ]; const providers: Array<AbstractProvider> = [ ];
if (options.publicPolygon !== "-" && staticNetwork) {
if (staticNetwork.name === "matic") {
providers.push(new JsonRpcProvider("https:/\/polygon-rpc.com/", staticNetwork, { staticNetwork }));
} else if (staticNetwork.name === "matic-mumbai") {
providers.push(new JsonRpcProvider("https:/\/rpc-mumbai.matic.today/", staticNetwork, { staticNetwork }));
}
}
if (options.alchemy !== "-") { if (options.alchemy !== "-") {
try { try {
providers.push(new AlchemyProvider(network, options.alchemy)); providers.push(new AlchemyProvider(network, options.alchemy));
@ -112,10 +120,11 @@ export function getDefaultProvider(network: string | Networkish | WebSocketLike,
// We use the floor because public third-party providers can be unreliable, // We use the floor because public third-party providers can be unreliable,
// so a low number of providers with a large quorum will fail too often // so a low number of providers with a large quorum will fail too often
let quorum = Math.floor(providers.length / 2); let quorum = Math.floor(providers.length / 2);
if (quorum > 2) { quorum = 2; }
// Testnets don't need as strong a security gaurantee and speed is // Testnets don't need as strong a security gaurantee and speed is
// more useful during testing // more useful during testing
if (name && Testnets.indexOf(name) !== -1) { quorum = 1; } if (staticNetwork && Testnets.indexOf(staticNetwork.name) !== -1) { quorum = 1; }
// Provided override qorum takes priority // Provided override qorum takes priority
if (options && options.quorum) { quorum = options.quorum; } if (options && options.quorum) { quorum = options.quorum; }