provider-pocket.js 3.5 KB

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