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

172 lines
5.9 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2022-09-30 05:57:27 +03:00
exports.InfuraProvider = exports.InfuraWebSocketProvider = void 0;
2022-11-30 23:44:23 +03:00
/**
2023-01-28 09:53:29 +03:00
* [[link-infura]] provides a third-party service for connecting to
* various blockchains over JSON-RPC.
2022-11-30 23:44:23 +03:00
*
2023-01-28 09:53:29 +03:00
* **Supported Networks**
*
* - Ethereum Mainnet (``mainnet``)
* - Goerli Testnet (``goerli``)
* - Sepolia Testnet (``sepolia``)
* - Arbitrum (``arbitrum``)
* - Arbitrum Goerli Testnet (``arbitrum-goerli``)
* - Optimism (``optimism``)
* - Optimism Goerli Testnet (``optimism-goerli``)
* - Polygon (``matic``)
* - Polygon Mumbai Testnet (``maticmum``)
*
* @_subsection: api/providers/thirdparty:INFURA [providers-infura]
2022-11-30 23:44:23 +03:00
*/
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");
2022-09-30 05:57:27 +03:00
const provider_websocket_js_1 = require("./provider-websocket.js");
2022-09-05 23:57:11 +03:00
const defaultProjectId = "84842078b09946638c03157f83405213";
function getHost(name) {
switch (name) {
2022-09-30 05:57:27 +03:00
case "mainnet":
2022-09-05 23:57:11 +03:00
return "mainnet.infura.io";
case "goerli":
return "goerli.infura.io";
2022-10-20 12:03:32 +03:00
case "sepolia":
return "sepolia.infura.io";
case "arbitrum":
return "arbitrum-mainnet.infura.io";
case "arbitrum-goerli":
return "arbitrum-goerli.infura.io";
2022-09-05 23:57:11 +03:00
case "matic":
return "polygon-mainnet.infura.io";
case "maticmum":
return "polygon-mumbai.infura.io";
case "optimism":
return "optimism-mainnet.infura.io";
2022-10-20 12:03:32 +03:00
case "optimism-goerli":
return "optimism-goerli.infura.io";
2022-09-05 23:57:11 +03:00
}
2022-11-09 10:57:02 +03:00
(0, index_js_1.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 **InfuraWebSocketProvider** connects to the [[link-infura]]
* WebSocket 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-infura-signup).
2022-11-30 23:44:23 +03:00
*/
2022-09-30 05:57:27 +03:00
class InfuraWebSocketProvider extends provider_websocket_js_1.WebSocketProvider {
2023-01-28 09:53:29 +03:00
/**
* The Project ID for the INFURA connection.
*/
2022-09-30 05:57:27 +03:00
projectId;
2023-01-28 09:53:29 +03:00
/**
* The Project Secret.
*
* If null, no authenticated requests are made. This should not
* be used outside of private contexts.
*/
2022-09-30 05:57:27 +03:00
projectSecret;
2023-01-28 09:53:29 +03:00
/**
* Creates a new **InfuraWebSocketProvider**.
*/
2022-11-30 23:44:23 +03:00
constructor(network, projectId) {
const provider = new InfuraProvider(network, projectId);
2022-09-30 05:57:27 +03:00
const req = provider._getConnection();
2022-11-09 10:57:02 +03:00
(0, index_js_1.assert)(!req.credentials, "INFURA WebSocket project secrets unsupported", "UNSUPPORTED_OPERATION", { operation: "InfuraProvider.getWebSocketProvider()" });
2022-09-30 05:57:27 +03:00
const url = req.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
super(url, network);
(0, index_js_1.defineProperties)(this, {
projectId: provider.projectId,
projectSecret: provider.projectSecret
});
}
isCommunityResource() {
return (this.projectId === defaultProjectId);
}
}
exports.InfuraWebSocketProvider = InfuraWebSocketProvider;
2022-11-30 23:44:23 +03:00
/**
2023-01-28 09:53:29 +03:00
* The **InfuraProvider** connects to the [[link-infura]]
* 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-infura-signup).
2022-11-30 23:44:23 +03:00
*/
2022-09-05 23:57:11 +03:00
class InfuraProvider extends provider_jsonrpc_js_1.JsonRpcProvider {
2023-01-28 09:53:29 +03:00
/**
* The Project ID for the INFURA connection.
*/
2022-09-05 23:57:11 +03:00
projectId;
2023-01-28 09:53:29 +03:00
/**
* The Project Secret.
*
* If null, no authenticated requests are made. This should not
* be used outside of private contexts.
*/
2022-09-05 23:57:11 +03:00
projectSecret;
2023-01-28 09:53:29 +03:00
/**
* Creates a new **InfuraProvider**.
*/
2022-11-30 23:44:23 +03:00
constructor(_network, projectId, projectSecret) {
if (_network == null) {
_network = "mainnet";
}
2022-09-05 23:57:11 +03:00
const network = network_js_1.Network.from(_network);
if (projectId == null) {
projectId = defaultProjectId;
}
if (projectSecret == null) {
projectSecret = null;
}
const request = InfuraProvider.getRequest(network, projectId, projectSecret);
super(request, network, { staticNetwork: network });
2022-09-16 05:58:45 +03:00
(0, index_js_1.defineProperties)(this, { projectId, projectSecret });
2022-09-05 23:57:11 +03:00
}
_getProvider(chainId) {
try {
return new InfuraProvider(chainId, this.projectId, this.projectSecret);
}
catch (error) { }
return super._getProvider(chainId);
}
2022-09-30 05:57:27 +03:00
isCommunityResource() {
return (this.projectId === defaultProjectId);
}
2023-01-28 09:53:29 +03:00
/**
* Creates a new **InfuraWebSocketProvider**.
*/
2022-11-30 23:44:23 +03:00
static getWebSocketProvider(network, projectId) {
return new InfuraWebSocketProvider(network, projectId);
2022-09-30 05:57:27 +03:00
}
2023-01-28 09:53:29 +03:00
/**
* Returns a prepared request for connecting to %%network%%
* with %%projectId%% and %%projectSecret%%.
*/
2022-09-05 23:57:11 +03:00
static getRequest(network, projectId, projectSecret) {
if (projectId == null) {
projectId = defaultProjectId;
}
if (projectSecret == null) {
projectSecret = null;
}
2022-09-16 05:58:45 +03:00
const request = new index_js_1.FetchRequest(`https:/\/${getHost(network.name)}/v3/${projectId}`);
2022-09-05 23:57:11 +03:00
request.allowGzip = true;
if (projectSecret) {
request.setCredentials("", projectSecret);
}
if (projectId === defaultProjectId) {
request.retryFunc = async (request, response, attempt) => {
(0, community_js_1.showThrottleMessage)("InfuraProvider");
return true;
};
}
return request;
}
}
exports.InfuraProvider = InfuraProvider;
//# sourceMappingURL=provider-infura.js.map