provider-alchemy.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. /**
  3. * [[link-alchemy]] provides a third-party service for connecting to
  4. * various blockchains over JSON-RPC.
  5. *
  6. * **Supported Networks**
  7. *
  8. * - Ethereum Mainnet (``mainnet``)
  9. * - Goerli Testnet (``goerli``)
  10. * - Sepolia Testnet (``sepolia``)
  11. * - Arbitrum (``arbitrum``)
  12. * - Arbitrum Goerli Testnet (``arbitrum-goerli``)
  13. * - Arbitrum Sepolia Testnet (``arbitrum-sepolia``)
  14. * - Base (``base``)
  15. * - Base Goerlia Testnet (``base-goerli``)
  16. * - Base Sepolia Testnet (``base-sepolia``)
  17. * - Optimism (``optimism``)
  18. * - Optimism Goerli Testnet (``optimism-goerli``)
  19. * - Optimism Sepolia Testnet (``optimism-sepolia``)
  20. * - Polygon (``matic``)
  21. * - Polygon Amoy Testnet (``matic-amoy``)
  22. * - Polygon Mumbai Testnet (``matic-mumbai``)
  23. *
  24. * @_subsection: api/providers/thirdparty:Alchemy [providers-alchemy]
  25. */
  26. Object.defineProperty(exports, "__esModule", { value: true });
  27. exports.AlchemyProvider = void 0;
  28. const index_js_1 = require("../utils/index.js");
  29. const community_js_1 = require("./community.js");
  30. const network_js_1 = require("./network.js");
  31. const provider_jsonrpc_js_1 = require("./provider-jsonrpc.js");
  32. const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC";
  33. function getHost(name) {
  34. switch (name) {
  35. case "mainnet":
  36. return "eth-mainnet.alchemyapi.io";
  37. case "goerli":
  38. return "eth-goerli.g.alchemy.com";
  39. case "sepolia":
  40. return "eth-sepolia.g.alchemy.com";
  41. case "arbitrum":
  42. return "arb-mainnet.g.alchemy.com";
  43. case "arbitrum-goerli":
  44. return "arb-goerli.g.alchemy.com";
  45. case "arbitrum-sepolia":
  46. return "arb-sepolia.g.alchemy.com";
  47. case "base":
  48. return "base-mainnet.g.alchemy.com";
  49. case "base-goerli":
  50. return "base-goerli.g.alchemy.com";
  51. case "base-sepolia":
  52. return "base-sepolia.g.alchemy.com";
  53. case "matic":
  54. return "polygon-mainnet.g.alchemy.com";
  55. case "matic-amoy":
  56. return "polygon-amoy.g.alchemy.com";
  57. case "matic-mumbai":
  58. return "polygon-mumbai.g.alchemy.com";
  59. case "optimism":
  60. return "opt-mainnet.g.alchemy.com";
  61. case "optimism-goerli":
  62. return "opt-goerli.g.alchemy.com";
  63. case "optimism-sepolia":
  64. return "opt-sepolia.g.alchemy.com";
  65. }
  66. (0, index_js_1.assertArgument)(false, "unsupported network", "network", name);
  67. }
  68. /**
  69. * The **AlchemyProvider** connects to the [[link-alchemy]]
  70. * JSON-RPC end-points.
  71. *
  72. * By default, a highly-throttled API key is used, which is
  73. * appropriate for quick prototypes and simple scripts. To
  74. * gain access to an increased rate-limit, it is highly
  75. * recommended to [sign up here](link-alchemy-signup).
  76. *
  77. * @_docloc: api/providers/thirdparty
  78. */
  79. class AlchemyProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
  80. apiKey;
  81. constructor(_network, apiKey) {
  82. if (_network == null) {
  83. _network = "mainnet";
  84. }
  85. const network = network_js_1.Network.from(_network);
  86. if (apiKey == null) {
  87. apiKey = defaultApiKey;
  88. }
  89. const request = AlchemyProvider.getRequest(network, apiKey);
  90. super(request, network, { staticNetwork: network });
  91. (0, index_js_1.defineProperties)(this, { apiKey });
  92. }
  93. _getProvider(chainId) {
  94. try {
  95. return new AlchemyProvider(chainId, this.apiKey);
  96. }
  97. catch (error) { }
  98. return super._getProvider(chainId);
  99. }
  100. async _perform(req) {
  101. // https://docs.alchemy.com/reference/trace-transaction
  102. if (req.method === "getTransactionResult") {
  103. const { trace, tx } = await (0, index_js_1.resolveProperties)({
  104. trace: this.send("trace_transaction", [req.hash]),
  105. tx: this.getTransaction(req.hash)
  106. });
  107. if (trace == null || tx == null) {
  108. return null;
  109. }
  110. let data;
  111. let error = false;
  112. try {
  113. data = trace[0].result.output;
  114. error = (trace[0].error === "Reverted");
  115. }
  116. catch (error) { }
  117. if (data) {
  118. (0, index_js_1.assert)(!error, "an error occurred during transaction executions", "CALL_EXCEPTION", {
  119. action: "getTransactionResult",
  120. data,
  121. reason: null,
  122. transaction: tx,
  123. invocation: null,
  124. revert: null // @TODO
  125. });
  126. return data;
  127. }
  128. (0, index_js_1.assert)(false, "could not parse trace result", "BAD_DATA", { value: trace });
  129. }
  130. return await super._perform(req);
  131. }
  132. isCommunityResource() {
  133. return (this.apiKey === defaultApiKey);
  134. }
  135. static getRequest(network, apiKey) {
  136. if (apiKey == null) {
  137. apiKey = defaultApiKey;
  138. }
  139. const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/v2/${apiKey}`);
  140. request.allowGzip = true;
  141. if (apiKey === defaultApiKey) {
  142. request.retryFunc = async (request, response, attempt) => {
  143. (0, community_js_1.showThrottleMessage)("alchemy");
  144. return true;
  145. };
  146. }
  147. return request;
  148. }
  149. }
  150. exports.AlchemyProvider = AlchemyProvider;
  151. //# sourceMappingURL=provider-alchemy.js.map