provider-chainstack.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * [[link-chainstack]] provides a third-party service for connecting to
  3. * various blockchains over JSON-RPC.
  4. *
  5. * **Supported Networks**
  6. *
  7. * - Ethereum Mainnet (``mainnet``)
  8. * - Arbitrum (``arbitrum``)
  9. * - BNB Smart Chain Mainnet (``bnb``)
  10. * - Polygon (``matic``)
  11. *
  12. * @_subsection: api/providers/thirdparty:Chainstack [providers-chainstack]
  13. */
  14. import {
  15. defineProperties, FetchRequest, assertArgument
  16. } from "../utils/index.js";
  17. import { showThrottleMessage } from "./community.js";
  18. import { Network } from "./network.js";
  19. import { JsonRpcProvider } from "./provider-jsonrpc.js";
  20. import type { AbstractProvider } from "./abstract-provider.js";
  21. import type { CommunityResourcable } from "./community.js";
  22. import type { Networkish } from "./network.js";
  23. function getApiKey(name: string): string {
  24. switch (name) {
  25. case "mainnet": return "39f1d67cedf8b7831010a665328c9197";
  26. case "arbitrum": return "0550c209db33c3abf4cc927e1e18cea1"
  27. case "bnb": return "98b5a77e531614387366f6fc5da097f8";
  28. case "matic": return "cd9d4d70377471aa7c142ec4a4205249";
  29. }
  30. assertArgument(false, "unsupported network", "network", name);
  31. }
  32. function getHost(name: string): string {
  33. switch(name) {
  34. case "mainnet":
  35. return "ethereum-mainnet.core.chainstack.com";
  36. case "arbitrum":
  37. return "arbitrum-mainnet.core.chainstack.com";
  38. case "bnb":
  39. return "bsc-mainnet.core.chainstack.com";
  40. case "matic":
  41. return "polygon-mainnet.core.chainstack.com";
  42. }
  43. assertArgument(false, "unsupported network", "network", name);
  44. }
  45. /**
  46. * The **ChainstackProvider** connects to the [[link-chainstack]]
  47. * JSON-RPC end-points.
  48. *
  49. * By default, a highly-throttled API key is used, which is
  50. * appropriate for quick prototypes and simple scripts. To
  51. * gain access to an increased rate-limit, it is highly
  52. * recommended to [sign up here](link-chainstack).
  53. */
  54. export class ChainstackProvider extends JsonRpcProvider implements CommunityResourcable {
  55. /**
  56. * The API key for the Chainstack connection.
  57. */
  58. readonly apiKey!: string;
  59. /**
  60. * Creates a new **ChainstackProvider**.
  61. */
  62. constructor(_network?: Networkish, apiKey?: null | string) {
  63. if (_network == null) { _network = "mainnet"; }
  64. const network = Network.from(_network);
  65. if (apiKey == null) { apiKey = getApiKey(network.name); }
  66. const request = ChainstackProvider.getRequest(network, apiKey);
  67. super(request, network, { staticNetwork: network });
  68. defineProperties<ChainstackProvider>(this, { apiKey });
  69. }
  70. _getProvider(chainId: number): AbstractProvider {
  71. try {
  72. return new ChainstackProvider(chainId, this.apiKey);
  73. } catch (error) { }
  74. return super._getProvider(chainId);
  75. }
  76. isCommunityResource(): boolean {
  77. return (this.apiKey === getApiKey(this._network.name));
  78. }
  79. /**
  80. * Returns a prepared request for connecting to %%network%%
  81. * with %%apiKey%% and %%projectSecret%%.
  82. */
  83. static getRequest(network: Network, apiKey?: null | string): FetchRequest {
  84. if (apiKey == null) { apiKey = getApiKey(network.name); }
  85. const request = new FetchRequest(`https:/\/${ getHost(network.name) }/${ apiKey }`);
  86. request.allowGzip = true;
  87. if (apiKey === getApiKey(network.name)) {
  88. request.retryFunc = async (request, response, attempt) => {
  89. showThrottleMessage("ChainstackProvider");
  90. return true;
  91. };
  92. }
  93. return request;
  94. }
  95. }