provider-ankr.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 {
  25. defineProperties, FetchRequest, assertArgument
  26. } from "../utils/index.js";
  27. import { AbstractProvider } from "./abstract-provider.js";
  28. import { showThrottleMessage } from "./community.js";
  29. import { Network } from "./network.js";
  30. import { JsonRpcProvider } from "./provider-jsonrpc.js";
  31. import type { CommunityResourcable } from "./community.js";
  32. import type { Networkish } from "./network.js";
  33. import type { JsonRpcError, JsonRpcPayload } from "./provider-jsonrpc.js";
  34. const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
  35. function getHost(name: string): string {
  36. switch (name) {
  37. case "mainnet":
  38. return "rpc.ankr.com/eth";
  39. case "goerli":
  40. return "rpc.ankr.com/eth_goerli";
  41. case "sepolia":
  42. return "rpc.ankr.com/eth_sepolia";
  43. case "arbitrum":
  44. return "rpc.ankr.com/arbitrum";
  45. case "base":
  46. return "rpc.ankr.com/base";
  47. case "base-goerli":
  48. return "rpc.ankr.com/base_goerli";
  49. case "base-sepolia":
  50. return "rpc.ankr.com/base_sepolia";
  51. case "bnb":
  52. return "rpc.ankr.com/bsc";
  53. case "bnbt":
  54. return "rpc.ankr.com/bsc_testnet_chapel";
  55. case "matic":
  56. return "rpc.ankr.com/polygon";
  57. case "matic-mumbai":
  58. return "rpc.ankr.com/polygon_mumbai";
  59. case "optimism":
  60. return "rpc.ankr.com/optimism";
  61. case "optimism-goerli":
  62. return "rpc.ankr.com/optimism_testnet";
  63. case "optimism-sepolia":
  64. return "rpc.ankr.com/optimism_sepolia";
  65. }
  66. assertArgument(false, "unsupported network", "network", name);
  67. }
  68. /**
  69. * The **AnkrProvider** connects to the [[link-ankr]]
  70. * JSON-RPC end-points.
  71. *
  72. * By default, a highly-throttled API key is used, which is
  73. * appropriate for quick prototypes and simple scripts. To
  74. * gain access to an increased rate-limit, it is highly
  75. * recommended to [sign up here](link-ankr-signup).
  76. */
  77. export class AnkrProvider extends JsonRpcProvider implements CommunityResourcable {
  78. /**
  79. * The API key for the Ankr connection.
  80. */
  81. readonly apiKey!: string;
  82. /**
  83. * Create a new **AnkrProvider**.
  84. *
  85. * By default connecting to ``mainnet`` with a highly throttled
  86. * API key.
  87. */
  88. constructor(_network?: Networkish, apiKey?: null | string) {
  89. if (_network == null) { _network = "mainnet"; }
  90. const network = Network.from(_network);
  91. if (apiKey == null) { apiKey = defaultApiKey; }
  92. // Ankr does not support filterId, so we force polling
  93. const options = { polling: true, staticNetwork: network };
  94. const request = AnkrProvider.getRequest(network, apiKey);
  95. super(request, network, options);
  96. defineProperties<AnkrProvider>(this, { apiKey });
  97. }
  98. _getProvider(chainId: number): AbstractProvider {
  99. try {
  100. return new AnkrProvider(chainId, this.apiKey);
  101. } catch (error) { }
  102. return super._getProvider(chainId);
  103. }
  104. /**
  105. * Returns a prepared request for connecting to %%network%% with
  106. * %%apiKey%%.
  107. */
  108. static getRequest(network: Network, apiKey?: null | string): FetchRequest {
  109. if (apiKey == null) { apiKey = defaultApiKey; }
  110. const request = new FetchRequest(`https:/\/${ getHost(network.name) }/${ apiKey }`);
  111. request.allowGzip = true;
  112. if (apiKey === defaultApiKey) {
  113. request.retryFunc = async (request, response, attempt) => {
  114. showThrottleMessage("AnkrProvider");
  115. return true;
  116. };
  117. }
  118. return request;
  119. }
  120. getRpcError(payload: JsonRpcPayload, error: JsonRpcError): Error {
  121. if (payload.method === "eth_sendRawTransaction") {
  122. if (error && error.error && error.error.message === "INTERNAL_ERROR: could not replace existing tx") {
  123. error.error.message = "replacement transaction underpriced";
  124. }
  125. }
  126. return super.getRpcError(payload, error);
  127. }
  128. isCommunityResource(): boolean {
  129. return (this.apiKey === defaultApiKey);
  130. }
  131. }