provider-ankr.js 4.5 KB

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