ethers.js/packages/providers/lib.esm/infura-provider.js

122 lines
4.6 KiB
JavaScript
Raw Normal View History

"use strict";
2020-10-08 03:10:50 +03:00
import { defineReadOnly } from "@ethersproject/properties";
2020-05-04 00:53:58 +03:00
import { WebSocketProvider } from "./websocket-provider";
2020-07-14 09:33:30 +03:00
import { showThrottleMessage } from "./formatter";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
const defaultProjectId = "84842078b09946638c03157f83405213";
2020-10-08 03:10:50 +03:00
export class InfuraWebSocketProvider extends WebSocketProvider {
constructor(network, apiKey) {
2020-05-04 00:53:58 +03:00
const provider = new InfuraProvider(network, apiKey);
const connection = provider.connection;
if (connection.password) {
logger.throwError("INFURA WebSocket project secrets unsupported", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "InfuraProvider.getWebSocketProvider()"
});
}
const url = connection.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
2020-10-08 03:10:50 +03:00
super(url, network);
defineReadOnly(this, "apiKey", provider.projectId);
defineReadOnly(this, "projectId", provider.projectId);
defineReadOnly(this, "projectSecret", provider.projectSecret);
}
isCommunityResource() {
return (this.projectId === defaultProjectId);
}
}
export class InfuraProvider extends UrlJsonRpcProvider {
static getWebSocketProvider(network, apiKey) {
return new InfuraWebSocketProvider(network, apiKey);
2020-05-04 00:53:58 +03:00
}
static getApiKey(apiKey) {
2019-11-20 12:57:38 +03:00
const apiKeyObj = {
apiKey: defaultProjectId,
projectId: defaultProjectId,
projectSecret: null
};
if (apiKey == null) {
2019-11-20 12:57:38 +03:00
return apiKeyObj;
}
2019-11-20 12:57:38 +03:00
if (typeof (apiKey) === "string") {
apiKeyObj.projectId = apiKey;
}
else if (apiKey.projectSecret != null) {
2020-07-13 15:03:56 +03:00
logger.assertArgument((typeof (apiKey.projectId) === "string"), "projectSecret requires a projectId", "projectId", apiKey.projectId);
logger.assertArgument((typeof (apiKey.projectSecret) === "string"), "invalid projectSecret", "projectSecret", "[REDACTED]");
2019-11-20 12:57:38 +03:00
apiKeyObj.projectId = apiKey.projectId;
apiKeyObj.projectSecret = apiKey.projectSecret;
}
else if (apiKey.projectId) {
apiKeyObj.projectId = apiKey.projectId;
}
apiKeyObj.apiKey = apiKeyObj.projectId;
return apiKeyObj;
}
static getUrl(network, apiKey) {
let host = null;
2020-07-13 15:03:56 +03:00
switch (network ? network.name : "unknown") {
case "homestead":
host = "mainnet.infura.io";
break;
case "ropsten":
host = "ropsten.infura.io";
break;
case "rinkeby":
host = "rinkeby.infura.io";
break;
case "kovan":
host = "kovan.infura.io";
break;
case "goerli":
host = "goerli.infura.io";
break;
2022-09-14 04:28:52 +03:00
case "sepolia":
host = "sepolia.infura.io";
break;
2021-07-23 09:21:24 +03:00
case "matic":
host = "polygon-mainnet.infura.io";
break;
case "maticmum":
host = "polygon-mumbai.infura.io";
break;
2021-11-30 19:19:37 +03:00
case "optimism":
host = "optimism-mainnet.infura.io";
break;
case "optimism-kovan":
host = "optimism-kovan.infura.io";
break;
case "arbitrum":
host = "arbitrum-mainnet.infura.io";
break;
case "arbitrum-rinkeby":
host = "arbitrum-rinkeby.infura.io";
break;
default:
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
argument: "network",
value: network
});
}
2019-11-20 12:57:38 +03:00
const connection = {
2020-10-08 03:10:50 +03:00
allowGzip: true,
2020-07-14 09:33:30 +03:00
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId),
throttleCallback: (attempt, url) => {
if (apiKey.projectId === defaultProjectId) {
showThrottleMessage();
}
return Promise.resolve(true);
}
2019-11-20 12:57:38 +03:00
};
if (apiKey.projectSecret != null) {
connection.user = "";
connection.password = apiKey.projectSecret;
}
return connection;
}
2020-10-08 03:10:50 +03:00
isCommunityResource() {
return (this.projectId === defaultProjectId);
}
}
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=infura-provider.js.map