provider-pocket.ts 3.8 KB

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