provider-chainstack.js 3.5 KB

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