default-provider.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDefaultProvider = void 0;
  4. const index_js_1 = require("../utils/index.js");
  5. const provider_ankr_js_1 = require("./provider-ankr.js");
  6. const provider_alchemy_js_1 = require("./provider-alchemy.js");
  7. const provider_chainstack_js_1 = require("./provider-chainstack.js");
  8. const provider_cloudflare_js_1 = require("./provider-cloudflare.js");
  9. const provider_etherscan_js_1 = require("./provider-etherscan.js");
  10. const provider_infura_js_1 = require("./provider-infura.js");
  11. //import { PocketProvider } from "./provider-pocket.js";
  12. const provider_quicknode_js_1 = require("./provider-quicknode.js");
  13. const provider_fallback_js_1 = require("./provider-fallback.js");
  14. const provider_jsonrpc_js_1 = require("./provider-jsonrpc.js");
  15. const network_js_1 = require("./network.js");
  16. const provider_websocket_js_1 = require("./provider-websocket.js");
  17. function isWebSocketLike(value) {
  18. return (value && typeof (value.send) === "function" &&
  19. typeof (value.close) === "function");
  20. }
  21. const Testnets = "goerli kovan sepolia classicKotti optimism-goerli arbitrum-goerli matic-mumbai bnbt".split(" ");
  22. /**
  23. * Returns a default provider for %%network%%.
  24. *
  25. * If %%network%% is a [[WebSocketLike]] or string that begins with
  26. * ``"ws:"`` or ``"wss:"``, a [[WebSocketProvider]] is returned backed
  27. * by that WebSocket or URL.
  28. *
  29. * If %%network%% is a string that begins with ``"HTTP:"`` or ``"HTTPS:"``,
  30. * a [[JsonRpcProvider]] is returned connected to that URL.
  31. *
  32. * Otherwise, a default provider is created backed by well-known public
  33. * Web3 backends (such as [[link-infura]]) using community-provided API
  34. * keys.
  35. *
  36. * The %%options%% allows specifying custom API keys per backend (setting
  37. * an API key to ``"-"`` will omit that provider) and ``options.exclusive``
  38. * can be set to either a backend name or and array of backend names, which
  39. * will whitelist **only** those backends.
  40. *
  41. * Current backend strings supported are:
  42. * - ``"alchemy"``
  43. * - ``"ankr"``
  44. * - ``"cloudflare"``
  45. * - ``"chainstack"``
  46. * - ``"etherscan"``
  47. * - ``"infura"``
  48. * - ``"publicPolygon"``
  49. * - ``"quicknode"``
  50. *
  51. * @example:
  52. * // Connect to a local Geth node
  53. * provider = getDefaultProvider("http://localhost:8545/");
  54. *
  55. * // Connect to Ethereum mainnet with any current and future
  56. * // third-party services available
  57. * provider = getDefaultProvider("mainnet");
  58. *
  59. * // Connect to Polygon, but only allow Etherscan and
  60. * // INFURA and use "MY_API_KEY" in calls to Etherscan.
  61. * provider = getDefaultProvider("matic", {
  62. * etherscan: "MY_API_KEY",
  63. * exclusive: [ "etherscan", "infura" ]
  64. * });
  65. */
  66. function getDefaultProvider(network, options) {
  67. if (options == null) {
  68. options = {};
  69. }
  70. const allowService = (name) => {
  71. if (options[name] === "-") {
  72. return false;
  73. }
  74. if (typeof (options.exclusive) === "string") {
  75. return (name === options.exclusive);
  76. }
  77. if (Array.isArray(options.exclusive)) {
  78. return (options.exclusive.indexOf(name) !== -1);
  79. }
  80. return true;
  81. };
  82. if (typeof (network) === "string" && network.match(/^https?:/)) {
  83. return new provider_jsonrpc_js_1.JsonRpcProvider(network);
  84. }
  85. if (typeof (network) === "string" && network.match(/^wss?:/) || isWebSocketLike(network)) {
  86. return new provider_websocket_js_1.WebSocketProvider(network);
  87. }
  88. // Get the network and name, if possible
  89. let staticNetwork = null;
  90. try {
  91. staticNetwork = network_js_1.Network.from(network);
  92. }
  93. catch (error) { }
  94. const providers = [];
  95. if (allowService("publicPolygon") && staticNetwork) {
  96. if (staticNetwork.name === "matic") {
  97. providers.push(new provider_jsonrpc_js_1.JsonRpcProvider("https:/\/polygon-rpc.com/", staticNetwork, { staticNetwork }));
  98. }
  99. else if (staticNetwork.name === "matic-amoy") {
  100. providers.push(new provider_jsonrpc_js_1.JsonRpcProvider("https:/\/rpc-amoy.polygon.technology/", staticNetwork, { staticNetwork }));
  101. }
  102. }
  103. if (allowService("alchemy")) {
  104. try {
  105. providers.push(new provider_alchemy_js_1.AlchemyProvider(network, options.alchemy));
  106. }
  107. catch (error) { }
  108. }
  109. if (allowService("ankr") && options.ankr != null) {
  110. try {
  111. providers.push(new provider_ankr_js_1.AnkrProvider(network, options.ankr));
  112. }
  113. catch (error) { }
  114. }
  115. if (allowService("chainstack")) {
  116. try {
  117. providers.push(new provider_chainstack_js_1.ChainstackProvider(network, options.chainstack));
  118. }
  119. catch (error) { }
  120. }
  121. if (allowService("cloudflare")) {
  122. try {
  123. providers.push(new provider_cloudflare_js_1.CloudflareProvider(network));
  124. }
  125. catch (error) { }
  126. }
  127. if (allowService("etherscan")) {
  128. try {
  129. providers.push(new provider_etherscan_js_1.EtherscanProvider(network, options.etherscan));
  130. }
  131. catch (error) { }
  132. }
  133. if (allowService("infura")) {
  134. try {
  135. let projectId = options.infura;
  136. let projectSecret = undefined;
  137. if (typeof (projectId) === "object") {
  138. projectSecret = projectId.projectSecret;
  139. projectId = projectId.projectId;
  140. }
  141. providers.push(new provider_infura_js_1.InfuraProvider(network, projectId, projectSecret));
  142. }
  143. catch (error) { }
  144. }
  145. /*
  146. if (options.pocket !== "-") {
  147. try {
  148. let appId = options.pocket;
  149. let secretKey: undefined | string = undefined;
  150. let loadBalancer: undefined | boolean = undefined;
  151. if (typeof(appId) === "object") {
  152. loadBalancer = !!appId.loadBalancer;
  153. secretKey = appId.secretKey;
  154. appId = appId.appId;
  155. }
  156. providers.push(new PocketProvider(network, appId, secretKey, loadBalancer));
  157. } catch (error) { console.log(error); }
  158. }
  159. */
  160. if (allowService("quicknode")) {
  161. try {
  162. let token = options.quicknode;
  163. providers.push(new provider_quicknode_js_1.QuickNodeProvider(network, token));
  164. }
  165. catch (error) { }
  166. }
  167. (0, index_js_1.assert)(providers.length, "unsupported default network", "UNSUPPORTED_OPERATION", {
  168. operation: "getDefaultProvider"
  169. });
  170. // No need for a FallbackProvider
  171. if (providers.length === 1) {
  172. return providers[0];
  173. }
  174. // We use the floor because public third-party providers can be unreliable,
  175. // so a low number of providers with a large quorum will fail too often
  176. let quorum = Math.floor(providers.length / 2);
  177. if (quorum > 2) {
  178. quorum = 2;
  179. }
  180. // Testnets don't need as strong a security gaurantee and speed is
  181. // more useful during testing
  182. if (staticNetwork && Testnets.indexOf(staticNetwork.name) !== -1) {
  183. quorum = 1;
  184. }
  185. // Provided override qorum takes priority
  186. if (options && options.quorum) {
  187. quorum = options.quorum;
  188. }
  189. return new provider_fallback_js_1.FallbackProvider(providers, undefined, { quorum });
  190. }
  191. exports.getDefaultProvider = getDefaultProvider;
  192. //# sourceMappingURL=default-provider.js.map