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

147 lines
6.1 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-07 18:24:04 -05:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-14 18:48:48 -04:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-07 18:24:04 -05:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-14 18:48:48 -04:00
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.InfuraProvider = exports.InfuraWebSocketProvider = void 0;
2020-10-07 20:10:50 -04:00
var properties_1 = require("@ethersproject/properties");
2020-05-03 17:53:58 -04:00
var websocket_provider_1 = require("./websocket-provider");
2020-07-14 02:33:30 -04:00
var formatter_1 = require("./formatter");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var url_json_rpc_provider_1 = require("./url-json-rpc-provider");
var defaultProjectId = "84842078b09946638c03157f83405213";
2020-10-07 20:10:50 -04:00
var InfuraWebSocketProvider = /** @class */ (function (_super) {
__extends(InfuraWebSocketProvider, _super);
function InfuraWebSocketProvider(network, apiKey) {
var _this = this;
2020-05-03 17:53:58 -04:00
var provider = new InfuraProvider(network, apiKey);
var connection = provider.connection;
if (connection.password) {
logger.throwError("INFURA WebSocket project secrets unsupported", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "InfuraProvider.getWebSocketProvider()"
});
}
var url = connection.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
2020-10-07 20:10:50 -04:00
_this = _super.call(this, url, network) || this;
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(_this, "apiKey", provider.projectId);
(0, properties_1.defineReadOnly)(_this, "projectId", provider.projectId);
(0, properties_1.defineReadOnly)(_this, "projectSecret", provider.projectSecret);
2020-10-07 20:10:50 -04:00
return _this;
}
InfuraWebSocketProvider.prototype.isCommunityResource = function () {
return (this.projectId === defaultProjectId);
};
return InfuraWebSocketProvider;
}(websocket_provider_1.WebSocketProvider));
exports.InfuraWebSocketProvider = InfuraWebSocketProvider;
var InfuraProvider = /** @class */ (function (_super) {
__extends(InfuraProvider, _super);
function InfuraProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
InfuraProvider.getWebSocketProvider = function (network, apiKey) {
return new InfuraWebSocketProvider(network, apiKey);
2020-05-03 17:53:58 -04:00
};
2019-05-14 18:48:48 -04:00
InfuraProvider.getApiKey = function (apiKey) {
2019-11-20 18:57:38 +09:00
var apiKeyObj = {
apiKey: defaultProjectId,
projectId: defaultProjectId,
projectSecret: null
};
2019-05-14 18:48:48 -04:00
if (apiKey == null) {
2019-11-20 18:57:38 +09:00
return apiKeyObj;
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
if (typeof (apiKey) === "string") {
apiKeyObj.projectId = apiKey;
}
else if (apiKey.projectSecret != null) {
2020-07-13 08:03:56 -04: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 18:57:38 +09:00
apiKeyObj.projectId = apiKey.projectId;
apiKeyObj.projectSecret = apiKey.projectSecret;
}
else if (apiKey.projectId) {
apiKeyObj.projectId = apiKey.projectId;
}
apiKeyObj.apiKey = apiKeyObj.projectId;
return apiKeyObj;
2019-05-14 18:48:48 -04:00
};
InfuraProvider.getUrl = function (network, apiKey) {
var host = null;
2020-07-13 08:03:56 -04:00
switch (network ? network.name : "unknown") {
2019-05-14 18:48:48 -04:00
case "homestead":
host = "mainnet.infura.io";
break;
case "goerli":
host = "goerli.infura.io";
break;
2022-09-13 21:28:52 -04:00
case "sepolia":
host = "sepolia.infura.io";
break;
2021-07-23 02:21:24 -04:00
case "matic":
host = "polygon-mainnet.infura.io";
break;
case "maticmum":
host = "polygon-mumbai.infura.io";
break;
2021-11-30 11:19:37 -05:00
case "optimism":
host = "optimism-mainnet.infura.io";
break;
2022-10-18 20:35:20 -04:00
case "optimism-goerli":
host = "optimism-goerli.infura.io";
2021-11-30 11:19:37 -05:00
break;
2024-02-23 22:30:55 -05:00
case "optimism-sepolia":
host = "optimism-sepolia.infura.io";
break;
2021-11-30 11:19:37 -05:00
case "arbitrum":
host = "arbitrum-mainnet.infura.io";
break;
2022-10-18 20:35:20 -04:00
case "arbitrum-goerli":
host = "arbitrum-goerli.infura.io";
2021-11-30 11:19:37 -05:00
break;
2024-02-23 22:30:55 -05:00
case "arbitrum-sepolia":
host = "arbitrum-sepolia.infura.io";
break;
2019-05-14 18:48:48 -04:00
default:
2019-08-02 02:10:58 -04:00
logger.throwError("unsupported network", logger_1.Logger.errors.INVALID_ARGUMENT, {
2019-05-14 18:48:48 -04:00
argument: "network",
value: network
});
}
2019-11-20 18:57:38 +09:00
var connection = {
2020-10-07 20:10:50 -04:00
allowGzip: true,
2020-07-14 02:33:30 -04:00
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId),
throttleCallback: function (attempt, url) {
if (apiKey.projectId === defaultProjectId) {
2021-10-16 02:29:27 -04:00
(0, formatter_1.showThrottleMessage)();
2020-07-14 02:33:30 -04:00
}
return Promise.resolve(true);
}
2019-11-20 18:57:38 +09:00
};
if (apiKey.projectSecret != null) {
connection.user = "";
connection.password = apiKey.projectSecret;
}
return connection;
2019-05-14 18:48:48 -04:00
};
2020-10-07 20:10:50 -04:00
InfuraProvider.prototype.isCommunityResource = function () {
return (this.projectId === defaultProjectId);
};
2019-05-14 18:48:48 -04:00
return InfuraProvider;
}(url_json_rpc_provider_1.UrlJsonRpcProvider));
exports.InfuraProvider = InfuraProvider;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=infura-provider.js.map