2023-01-28 09:53:29 +03:00
|
|
|
/**
|
2024-01-16 07:07:48 +03:00
|
|
|
* [[link-alchemy]] provides a third-party service for connecting to
|
|
|
|
* various blockchains over JSON-RPC.
|
|
|
|
*
|
|
|
|
* **Supported Networks**
|
|
|
|
*
|
|
|
|
* - Ethereum Mainnet (``mainnet``)
|
|
|
|
* - Goerli Testnet (``goerli``)
|
|
|
|
* - Sepolia Testnet (``sepolia``)
|
|
|
|
* - Arbitrum (``arbitrum``)
|
|
|
|
* - Arbitrum Goerli Testnet (``arbitrum-goerli``)
|
|
|
|
* - Arbitrum Sepolia Testnet (``arbitrum-sepolia``)
|
|
|
|
* - Base (``base``)
|
|
|
|
* - Base Goerlia Testnet (``base-goerli``)
|
|
|
|
* - Base Sepolia Testnet (``base-sepolia``)
|
|
|
|
* - Optimism (``optimism``)
|
|
|
|
* - Optimism Goerli Testnet (``optimism-goerli``)
|
|
|
|
* - Optimism Sepolia Testnet (``optimism-sepolia``)
|
|
|
|
* - Polygon (``matic``)
|
|
|
|
* - Polygon Mumbai Testnet (``matic-mumbai``)
|
2023-01-28 09:53:29 +03:00
|
|
|
*
|
|
|
|
* @_subsection: api/providers/thirdparty:Alchemy [providers-alchemy]
|
|
|
|
*/
|
2022-11-09 10:57:02 +03:00
|
|
|
import { defineProperties, resolveProperties, assert, assertArgument, FetchRequest } from "../utils/index.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
import { showThrottleMessage } from "./community.js";
|
|
|
|
import { Network } from "./network.js";
|
|
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
|
|
|
const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC";
|
|
|
|
function getHost(name) {
|
|
|
|
switch (name) {
|
2022-09-30 05:57:27 +03:00
|
|
|
case "mainnet":
|
2022-09-05 23:57:11 +03:00
|
|
|
return "eth-mainnet.alchemyapi.io";
|
|
|
|
case "goerli":
|
2022-10-20 12:03:32 +03:00
|
|
|
return "eth-goerli.g.alchemy.com";
|
2023-03-07 10:11:03 +03:00
|
|
|
case "sepolia":
|
|
|
|
return "eth-sepolia.g.alchemy.com";
|
2022-10-20 12:03:32 +03:00
|
|
|
case "arbitrum":
|
|
|
|
return "arb-mainnet.g.alchemy.com";
|
|
|
|
case "arbitrum-goerli":
|
|
|
|
return "arb-goerli.g.alchemy.com";
|
2024-01-16 07:07:48 +03:00
|
|
|
case "arbitrum-sepolia":
|
|
|
|
return "arb-sepolia.g.alchemy.com";
|
2023-11-27 14:11:49 +03:00
|
|
|
case "base":
|
|
|
|
return "base-mainnet.g.alchemy.com";
|
|
|
|
case "base-goerli":
|
|
|
|
return "base-goerli.g.alchemy.com";
|
2024-01-16 07:07:48 +03:00
|
|
|
case "base-sepolia":
|
|
|
|
return "base-sepolia.g.alchemy.com";
|
2022-09-05 23:57:11 +03:00
|
|
|
case "matic":
|
|
|
|
return "polygon-mainnet.g.alchemy.com";
|
2023-02-23 14:31:09 +03:00
|
|
|
case "matic-mumbai":
|
2022-09-05 23:57:11 +03:00
|
|
|
return "polygon-mumbai.g.alchemy.com";
|
|
|
|
case "optimism":
|
|
|
|
return "opt-mainnet.g.alchemy.com";
|
2022-10-20 12:03:32 +03:00
|
|
|
case "optimism-goerli":
|
|
|
|
return "opt-goerli.g.alchemy.com";
|
2024-01-16 07:07:48 +03:00
|
|
|
case "optimism-sepolia":
|
|
|
|
return "opt-sepolia.g.alchemy.com";
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
assertArgument(false, "unsupported network", "network", name);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
/**
|
2023-01-28 09:53:29 +03:00
|
|
|
* The **AlchemyProvider** connects to the [[link-alchemy]]
|
|
|
|
* JSON-RPC end-points.
|
|
|
|
*
|
|
|
|
* By default, a highly-throttled API key is used, which is
|
|
|
|
* appropriate for quick prototypes and simple scripts. To
|
|
|
|
* gain access to an increased rate-limit, it is highly
|
|
|
|
* recommended to [sign up here](link-alchemy-signup).
|
2022-11-30 23:44:23 +03:00
|
|
|
*
|
|
|
|
* @_docloc: api/providers/thirdparty
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
export class AlchemyProvider extends JsonRpcProvider {
|
|
|
|
apiKey;
|
2022-11-30 23:44:23 +03:00
|
|
|
constructor(_network, apiKey) {
|
|
|
|
if (_network == null) {
|
|
|
|
_network = "mainnet";
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
const network = Network.from(_network);
|
|
|
|
if (apiKey == null) {
|
|
|
|
apiKey = defaultApiKey;
|
|
|
|
}
|
|
|
|
const request = AlchemyProvider.getRequest(network, apiKey);
|
|
|
|
super(request, network, { staticNetwork: network });
|
|
|
|
defineProperties(this, { apiKey });
|
|
|
|
}
|
|
|
|
_getProvider(chainId) {
|
|
|
|
try {
|
|
|
|
return new AlchemyProvider(chainId, this.apiKey);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return super._getProvider(chainId);
|
|
|
|
}
|
|
|
|
async _perform(req) {
|
|
|
|
// https://docs.alchemy.com/reference/trace-transaction
|
|
|
|
if (req.method === "getTransactionResult") {
|
2022-10-20 12:03:32 +03:00
|
|
|
const { trace, tx } = await resolveProperties({
|
|
|
|
trace: this.send("trace_transaction", [req.hash]),
|
|
|
|
tx: this.getTransaction(req.hash)
|
|
|
|
});
|
|
|
|
if (trace == null || tx == null) {
|
2022-09-05 23:57:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let data;
|
|
|
|
let error = false;
|
|
|
|
try {
|
|
|
|
data = trace[0].result.output;
|
|
|
|
error = (trace[0].error === "Reverted");
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
if (data) {
|
2022-11-09 10:57:02 +03:00
|
|
|
assert(!error, "an error occurred during transaction executions", "CALL_EXCEPTION", {
|
|
|
|
action: "getTransactionResult",
|
|
|
|
data,
|
|
|
|
reason: null,
|
|
|
|
transaction: tx,
|
|
|
|
invocation: null,
|
|
|
|
revert: null // @TODO
|
|
|
|
});
|
2022-09-05 23:57:11 +03:00
|
|
|
return data;
|
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
assert(false, "could not parse trace result", "BAD_DATA", { value: trace });
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
return await super._perform(req);
|
|
|
|
}
|
|
|
|
isCommunityResource() {
|
|
|
|
return (this.apiKey === defaultApiKey);
|
|
|
|
}
|
|
|
|
static getRequest(network, apiKey) {
|
|
|
|
if (apiKey == null) {
|
|
|
|
apiKey = defaultApiKey;
|
|
|
|
}
|
|
|
|
const request = new FetchRequest(`https:/\/${getHost(network.name)}/v2/${apiKey}`);
|
|
|
|
request.allowGzip = true;
|
|
|
|
if (apiKey === defaultApiKey) {
|
|
|
|
request.retryFunc = async (request, response, attempt) => {
|
|
|
|
showThrottleMessage("alchemy");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=provider-alchemy.js.map
|