provider-ankr.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AnkrProvider = void 0;
  4. /**
  5. * [[link-ankr]] provides a third-party service for connecting to
  6. * various blockchains over JSON-RPC.
  7. *
  8. * **Supported Networks**
  9. *
  10. * - Ethereum Mainnet (``mainnet``)
  11. * - Goerli Testnet (``goerli``)
  12. * - Sepolia Testnet (``sepolia``)
  13. * - Arbitrum (``arbitrum``)
  14. * - Base (``base``)
  15. * - Base Goerlia Testnet (``base-goerli``)
  16. * - Base Sepolia Testnet (``base-sepolia``)
  17. * - BNB (``bnb``)
  18. * - BNB Testnet (``bnbt``)
  19. * - Optimism (``optimism``)
  20. * - Optimism Goerli Testnet (``optimism-goerli``)
  21. * - Optimism Sepolia Testnet (``optimism-sepolia``)
  22. * - Polygon (``matic``)
  23. * - Polygon Mumbai Testnet (``matic-mumbai``)
  24. *
  25. * @_subsection: api/providers/thirdparty:Ankr [providers-ankr]
  26. */
  27. const index_js_1 = require("../utils/index.js");
  28. const community_js_1 = require("./community.js");
  29. const network_js_1 = require("./network.js");
  30. const provider_jsonrpc_js_1 = require("./provider-jsonrpc.js");
  31. const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
  32. function getHost(name) {
  33. switch (name) {
  34. case "mainnet":
  35. return "rpc.ankr.com/eth";
  36. case "goerli":
  37. return "rpc.ankr.com/eth_goerli";
  38. case "sepolia":
  39. return "rpc.ankr.com/eth_sepolia";
  40. case "arbitrum":
  41. return "rpc.ankr.com/arbitrum";
  42. case "base":
  43. return "rpc.ankr.com/base";
  44. case "base-goerli":
  45. return "rpc.ankr.com/base_goerli";
  46. case "base-sepolia":
  47. return "rpc.ankr.com/base_sepolia";
  48. case "bnb":
  49. return "rpc.ankr.com/bsc";
  50. case "bnbt":
  51. return "rpc.ankr.com/bsc_testnet_chapel";
  52. case "matic":
  53. return "rpc.ankr.com/polygon";
  54. case "matic-mumbai":
  55. return "rpc.ankr.com/polygon_mumbai";
  56. case "optimism":
  57. return "rpc.ankr.com/optimism";
  58. case "optimism-goerli":
  59. return "rpc.ankr.com/optimism_testnet";
  60. case "optimism-sepolia":
  61. return "rpc.ankr.com/optimism_sepolia";
  62. }
  63. (0, index_js_1.assertArgument)(false, "unsupported network", "network", name);
  64. }
  65. /**
  66. * The **AnkrProvider** connects to the [[link-ankr]]
  67. * JSON-RPC end-points.
  68. *
  69. * By default, a highly-throttled API key is used, which is
  70. * appropriate for quick prototypes and simple scripts. To
  71. * gain access to an increased rate-limit, it is highly
  72. * recommended to [sign up here](link-ankr-signup).
  73. */
  74. class AnkrProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
  75. /**
  76. * The API key for the Ankr connection.
  77. */
  78. apiKey;
  79. /**
  80. * Create a new **AnkrProvider**.
  81. *
  82. * By default connecting to ``mainnet`` with a highly throttled
  83. * API key.
  84. */
  85. constructor(_network, apiKey) {
  86. if (_network == null) {
  87. _network = "mainnet";
  88. }
  89. const network = network_js_1.Network.from(_network);
  90. if (apiKey == null) {
  91. apiKey = defaultApiKey;
  92. }
  93. // Ankr does not support filterId, so we force polling
  94. const options = { polling: true, staticNetwork: network };
  95. const request = AnkrProvider.getRequest(network, apiKey);
  96. super(request, network, options);
  97. (0, index_js_1.defineProperties)(this, { apiKey });
  98. }
  99. _getProvider(chainId) {
  100. try {
  101. return new AnkrProvider(chainId, this.apiKey);
  102. }
  103. catch (error) { }
  104. return super._getProvider(chainId);
  105. }
  106. /**
  107. * Returns a prepared request for connecting to %%network%% with
  108. * %%apiKey%%.
  109. */
  110. static getRequest(network, apiKey) {
  111. if (apiKey == null) {
  112. apiKey = defaultApiKey;
  113. }
  114. const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/${apiKey}`);
  115. request.allowGzip = true;
  116. if (apiKey === defaultApiKey) {
  117. request.retryFunc = async (request, response, attempt) => {
  118. (0, community_js_1.showThrottleMessage)("AnkrProvider");
  119. return true;
  120. };
  121. }
  122. return request;
  123. }
  124. getRpcError(payload, error) {
  125. if (payload.method === "eth_sendRawTransaction") {
  126. if (error && error.error && error.error.message === "INTERNAL_ERROR: could not replace existing tx") {
  127. error.error.message = "replacement transaction underpriced";
  128. }
  129. }
  130. return super.getRpcError(payload, error);
  131. }
  132. isCommunityResource() {
  133. return (this.apiKey === defaultApiKey);
  134. }
  135. }
  136. exports.AnkrProvider = AnkrProvider;
  137. //# sourceMappingURL=provider-ankr.js.map