ethers.js/lib.commonjs/providers/provider-alchemy.js

99 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlchemyProvider = void 0;
2022-09-16 05:58:45 +03:00
const index_js_1 = require("../utils/index.js");
2022-09-05 23:57:11 +03:00
const community_js_1 = require("./community.js");
const network_js_1 = require("./network.js");
const provider_jsonrpc_js_1 = require("./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 "ropsten":
return "eth-ropsten.alchemyapi.io";
case "rinkeby":
return "eth-rinkeby.alchemyapi.io";
case "goerli":
return "eth-goerli.alchemyapi.io";
case "kovan":
return "eth-kovan.alchemyapi.io";
case "matic":
return "polygon-mainnet.g.alchemy.com";
case "maticmum":
return "polygon-mumbai.g.alchemy.com";
case "arbitrum":
return "arb-mainnet.g.alchemy.com";
case "arbitrum-rinkeby":
return "arb-rinkeby.g.alchemy.com";
case "optimism":
return "opt-mainnet.g.alchemy.com";
case "optimism-kovan":
return "opt-kovan.g.alchemy.com";
}
2022-09-16 05:58:45 +03:00
return (0, index_js_1.throwArgumentError)("unsupported network", "network", name);
2022-09-05 23:57:11 +03:00
}
class AlchemyProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
apiKey;
2022-09-30 05:57:27 +03:00
constructor(_network = "mainnet", apiKey) {
2022-09-05 23:57:11 +03:00
const network = network_js_1.Network.from(_network);
if (apiKey == null) {
apiKey = defaultApiKey;
}
const request = AlchemyProvider.getRequest(network, apiKey);
super(request, network, { staticNetwork: network });
2022-09-16 05:58:45 +03:00
(0, index_js_1.defineProperties)(this, { apiKey });
2022-09-05 23:57:11 +03:00
}
_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") {
const trace = await this.send("trace_transaction", [req.hash]);
if (trace == null) {
return null;
}
let data;
let error = false;
try {
data = trace[0].result.output;
error = (trace[0].error === "Reverted");
}
catch (error) { }
if (data) {
if (error) {
2022-09-16 05:58:45 +03:00
(0, index_js_1.throwError)("an error occurred during transaction executions", "CALL_EXCEPTION", {
2022-09-05 23:57:11 +03:00
data
});
}
return data;
}
2022-09-16 05:58:45 +03:00
return (0, index_js_1.throwError)("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;
}
2022-09-16 05:58:45 +03:00
const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/v2/${apiKey}`);
2022-09-05 23:57:11 +03:00
request.allowGzip = true;
if (apiKey === defaultApiKey) {
request.retryFunc = async (request, response, attempt) => {
(0, community_js_1.showThrottleMessage)("alchemy");
return true;
};
}
return request;
}
}
exports.AlchemyProvider = AlchemyProvider;
//# sourceMappingURL=provider-alchemy.js.map