default-provider.js 6.8 KB

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