provider-pocket.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.PocketProvider = void 0;
  4. /**
  5. * [[link-pocket]] provides a third-party service for connecting to
  6. * various blockchains over JSON-RPC.
  7. *
  8. * **Supported Networks**
  9. *
  10. * - Ethereum Mainnet (``mainnet``)
  11. * - Goerli Testnet (``goerli``)
  12. * - Polygon (``matic``)
  13. * - Arbitrum (``arbitrum``)
  14. *
  15. * @_subsection: api/providers/thirdparty:Pocket [providers-pocket]
  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. const defaultApplicationId = "62e1ad51b37b8e00394bda3b";
  22. function getHost(name) {
  23. switch (name) {
  24. case "mainnet":
  25. return "eth-mainnet.gateway.pokt.network";
  26. case "goerli":
  27. return "eth-goerli.gateway.pokt.network";
  28. case "matic":
  29. return "poly-mainnet.gateway.pokt.network";
  30. case "matic-mumbai":
  31. return "polygon-mumbai-rpc.gateway.pokt.network";
  32. }
  33. (0, index_js_1.assertArgument)(false, "unsupported network", "network", name);
  34. }
  35. /**
  36. * The **PocketProvider** connects to the [[link-pocket]]
  37. * JSON-RPC end-points.
  38. *
  39. * By default, a highly-throttled API key is used, which is
  40. * appropriate for quick prototypes and simple scripts. To
  41. * gain access to an increased rate-limit, it is highly
  42. * recommended to [sign up here](link-pocket-signup).
  43. */
  44. class PocketProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
  45. /**
  46. * The Application ID for the Pocket connection.
  47. */
  48. applicationId;
  49. /**
  50. * The Application Secret for making authenticated requests
  51. * to the Pocket connection.
  52. */
  53. applicationSecret;
  54. /**
  55. * Create a new **PocketProvider**.
  56. *
  57. * By default connecting to ``mainnet`` with a highly throttled
  58. * API key.
  59. */
  60. constructor(_network, applicationId, applicationSecret) {
  61. if (_network == null) {
  62. _network = "mainnet";
  63. }
  64. const network = network_js_1.Network.from(_network);
  65. if (applicationId == null) {
  66. applicationId = defaultApplicationId;
  67. }
  68. if (applicationSecret == null) {
  69. applicationSecret = null;
  70. }
  71. const options = { staticNetwork: network };
  72. const request = PocketProvider.getRequest(network, applicationId, applicationSecret);
  73. super(request, network, options);
  74. (0, index_js_1.defineProperties)(this, { applicationId, applicationSecret });
  75. }
  76. _getProvider(chainId) {
  77. try {
  78. return new PocketProvider(chainId, this.applicationId, this.applicationSecret);
  79. }
  80. catch (error) { }
  81. return super._getProvider(chainId);
  82. }
  83. /**
  84. * Returns a prepared request for connecting to %%network%% with
  85. * %%applicationId%%.
  86. */
  87. static getRequest(network, applicationId, applicationSecret) {
  88. if (applicationId == null) {
  89. applicationId = defaultApplicationId;
  90. }
  91. const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/v1/lb/${applicationId}`);
  92. request.allowGzip = true;
  93. if (applicationSecret) {
  94. request.setCredentials("", applicationSecret);
  95. }
  96. if (applicationId === defaultApplicationId) {
  97. request.retryFunc = async (request, response, attempt) => {
  98. (0, community_js_1.showThrottleMessage)("PocketProvider");
  99. return true;
  100. };
  101. }
  102. return request;
  103. }
  104. isCommunityResource() {
  105. return (this.applicationId === defaultApplicationId);
  106. }
  107. }
  108. exports.PocketProvider = PocketProvider;
  109. //# sourceMappingURL=provider-pocket.js.map