provider-chainstack.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 { defineProperties, FetchRequest, assertArgument } from "../utils/index.js";
  15. import { showThrottleMessage } from "./community.js";
  16. import { Network } from "./network.js";
  17. import { JsonRpcProvider } from "./provider-jsonrpc.js";
  18. function getApiKey(name) {
  19. switch (name) {
  20. case "mainnet": return "39f1d67cedf8b7831010a665328c9197";
  21. case "arbitrum": return "0550c209db33c3abf4cc927e1e18cea1";
  22. case "bnb": return "98b5a77e531614387366f6fc5da097f8";
  23. case "matic": return "cd9d4d70377471aa7c142ec4a4205249";
  24. }
  25. assertArgument(false, "unsupported network", "network", name);
  26. }
  27. function getHost(name) {
  28. switch (name) {
  29. case "mainnet":
  30. return "ethereum-mainnet.core.chainstack.com";
  31. case "arbitrum":
  32. return "arbitrum-mainnet.core.chainstack.com";
  33. case "bnb":
  34. return "bsc-mainnet.core.chainstack.com";
  35. case "matic":
  36. return "polygon-mainnet.core.chainstack.com";
  37. }
  38. assertArgument(false, "unsupported network", "network", name);
  39. }
  40. /**
  41. * The **ChainstackProvider** connects to the [[link-chainstack]]
  42. * JSON-RPC end-points.
  43. *
  44. * By default, a highly-throttled API key is used, which is
  45. * appropriate for quick prototypes and simple scripts. To
  46. * gain access to an increased rate-limit, it is highly
  47. * recommended to [sign up here](link-chainstack).
  48. */
  49. export class ChainstackProvider extends JsonRpcProvider {
  50. /**
  51. * The API key for the Chainstack connection.
  52. */
  53. apiKey;
  54. /**
  55. * Creates a new **ChainstackProvider**.
  56. */
  57. constructor(_network, apiKey) {
  58. if (_network == null) {
  59. _network = "mainnet";
  60. }
  61. const network = Network.from(_network);
  62. if (apiKey == null) {
  63. apiKey = getApiKey(network.name);
  64. }
  65. const request = ChainstackProvider.getRequest(network, apiKey);
  66. super(request, network, { staticNetwork: network });
  67. defineProperties(this, { apiKey });
  68. }
  69. _getProvider(chainId) {
  70. try {
  71. return new ChainstackProvider(chainId, this.apiKey);
  72. }
  73. catch (error) { }
  74. return super._getProvider(chainId);
  75. }
  76. isCommunityResource() {
  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, apiKey) {
  84. if (apiKey == null) {
  85. apiKey = getApiKey(network.name);
  86. }
  87. const request = new FetchRequest(`https:/\/${getHost(network.name)}/${apiKey}`);
  88. request.allowGzip = true;
  89. if (apiKey === getApiKey(network.name)) {
  90. request.retryFunc = async (request, response, attempt) => {
  91. showThrottleMessage("ChainstackProvider");
  92. return true;
  93. };
  94. }
  95. return request;
  96. }
  97. }
  98. //# sourceMappingURL=provider-chainstack.js.map