2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
2020-10-07 20:10:50 -04:00
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
2020-07-14 02:33:30 -04:00
|
|
|
import { showThrottleMessage } from "./formatter";
|
2020-07-05 00:01:57 -04:00
|
|
|
import { WebSocketProvider } from "./websocket-provider";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { UrlJsonRpcProvider } from "./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
|
|
|
|
const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC";
|
2020-10-07 20:10:50 -04:00
|
|
|
export class AlchemyWebSocketProvider extends WebSocketProvider {
|
|
|
|
constructor(network, apiKey) {
|
2020-07-05 00:01:57 -04:00
|
|
|
const provider = new AlchemyProvider(network, apiKey);
|
|
|
|
const url = provider.connection.url.replace(/^http/i, "ws")
|
|
|
|
.replace(".alchemyapi.", ".ws.alchemyapi.");
|
2020-10-07 20:10:50 -04:00
|
|
|
super(url, provider.network);
|
|
|
|
defineReadOnly(this, "apiKey", provider.apiKey);
|
|
|
|
}
|
|
|
|
isCommunityResource() {
|
|
|
|
return (this.apiKey === defaultApiKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class AlchemyProvider extends UrlJsonRpcProvider {
|
|
|
|
static getWebSocketProvider(network, apiKey) {
|
|
|
|
return new AlchemyWebSocketProvider(network, apiKey);
|
2020-07-05 00:01:57 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
static getApiKey(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-08-25 02:39:20 -04:00
|
|
|
return apiKey;
|
|
|
|
}
|
|
|
|
static getUrl(network, apiKey) {
|
|
|
|
let host = null;
|
|
|
|
switch (network.name) {
|
|
|
|
case "homestead":
|
2020-07-05 00:01:57 -04:00
|
|
|
host = "eth-mainnet.alchemyapi.io/v2/";
|
2019-08-25 02:39:20 -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-08-25 02:39:20 -04:00
|
|
|
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;
|
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;
|
2019-08-25 02:39:20 -04:00
|
|
|
default:
|
|
|
|
logger.throwArgumentError("unsupported network", "network", arguments[0]);
|
|
|
|
}
|
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: (attempt, url) => {
|
|
|
|
if (apiKey === defaultApiKey) {
|
|
|
|
showThrottleMessage();
|
|
|
|
}
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-10-07 20:10:50 -04:00
|
|
|
isCommunityResource() {
|
|
|
|
return (this.apiKey === defaultApiKey);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=alchemy-provider.js.map
|