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

121 lines
5.0 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.AlchemyProvider = exports.AlchemyWebSocketProvider = void 0;
2020-10-07 20:10:50 -04:00
var properties_1 = require("@ethersproject/properties");
2020-07-14 02:33:30 -04:00
var formatter_1 = require("./formatter");
2020-07-05 00:01:57 -04:00
var websocket_provider_1 = require("./websocket-provider");
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");
// This key was provided to ethers.js by Alchemy to be used by the
// default provider, but it is recommended that for your own
// production environments, that you acquire your own API key at:
// https://dashboard.alchemyapi.io
var defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC";
2020-10-07 20:10:50 -04:00
var AlchemyWebSocketProvider = /** @class */ (function (_super) {
__extends(AlchemyWebSocketProvider, _super);
function AlchemyWebSocketProvider(network, apiKey) {
var _this = this;
var provider = new AlchemyProvider(network, apiKey);
var url = provider.connection.url.replace(/^http/i, "ws")
.replace(".alchemyapi.", ".ws.alchemyapi.");
_this = _super.call(this, url, provider.network) || this;
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(_this, "apiKey", provider.apiKey);
2020-10-07 20:10:50 -04:00
return _this;
}
AlchemyWebSocketProvider.prototype.isCommunityResource = function () {
return (this.apiKey === defaultApiKey);
};
return AlchemyWebSocketProvider;
}(websocket_provider_1.WebSocketProvider));
exports.AlchemyWebSocketProvider = AlchemyWebSocketProvider;
2019-05-14 18:48:48 -04:00
var AlchemyProvider = /** @class */ (function (_super) {
__extends(AlchemyProvider, _super);
function AlchemyProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
2020-07-05 00:01:57 -04:00
AlchemyProvider.getWebSocketProvider = function (network, apiKey) {
2020-10-07 20:10:50 -04:00
return new AlchemyWebSocketProvider(network, apiKey);
2020-07-05 00:01:57 -04:00
};
2019-05-14 18:48:48 -04:00
AlchemyProvider.getApiKey = function (apiKey) {
if (apiKey == null) {
return defaultApiKey;
}
2019-11-20 18:57:38 +09:00
if (apiKey && typeof (apiKey) !== "string") {
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
}
2019-05-14 18:48:48 -04:00
return apiKey;
};
AlchemyProvider.getUrl = function (network, apiKey) {
var host = null;
switch (network.name) {
case "homestead":
2020-07-05 00:01:57 -04:00
host = "eth-mainnet.alchemyapi.io/v2/";
2019-05-14 18:48:48 -04:00
break;
2020-05-04 23:01:04 -04:00
case "goerli":
2022-10-18 23:41:44 -04:00
host = "eth-goerli.g.alchemy.com/v2/";
2019-05-14 18:48:48 -04:00
break;
2024-02-23 22:30:55 -05:00
case "sepolia":
host = "eth-sepolia.g.alchemy.com/v2/";
break;
2021-07-23 02:21:24 -04:00
case "matic":
host = "polygon-mainnet.g.alchemy.com/v2/";
break;
case "maticmum":
host = "polygon-mumbai.g.alchemy.com/v2/";
break;
2021-11-30 11:19:37 -05:00
case "arbitrum":
host = "arb-mainnet.g.alchemy.com/v2/";
break;
2022-08-18 14:48:39 -04:00
case "arbitrum-goerli":
host = "arb-goerli.g.alchemy.com/v2/";
break;
2024-02-23 22:30:55 -05:00
case "arbitrum-sepolia":
host = "arb-sepolia.g.alchemy.com/v2/";
break;
2021-11-30 11:19:37 -05:00
case "optimism":
host = "opt-mainnet.g.alchemy.com/v2/";
break;
2022-08-18 14:48:39 -04:00
case "optimism-goerli":
host = "opt-goerli.g.alchemy.com/v2/";
break;
2024-02-23 22:30:55 -05:00
case "optimism-sepolia":
host = "opt-sepolia.g.alchemy.com/v2/";
break;
2019-05-14 18:48:48 -04:00
default:
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("unsupported network", "network", arguments[0]);
2019-05-14 18:48:48 -04:00
}
2020-07-14 02:33:30 -04:00
return {
2020-10-07 20:10:50 -04:00
allowGzip: true,
2020-07-14 02:33:30 -04:00
url: ("https:/" + "/" + host + apiKey),
throttleCallback: function (attempt, url) {
if (apiKey === defaultApiKey) {
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-05-14 18:48:48 -04:00
};
2020-10-07 20:10:50 -04:00
AlchemyProvider.prototype.isCommunityResource = function () {
return (this.apiKey === defaultApiKey);
};
2019-05-14 18:48:48 -04:00
return AlchemyProvider;
}(url_json_rpc_provider_1.UrlJsonRpcProvider));
exports.AlchemyProvider = AlchemyProvider;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=alchemy-provider.js.map