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

98 lines
4.1 KiB
JavaScript
Raw Normal View History

"use strict";
2021-06-26 08:55:19 +03:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
2020-10-08 03:10:50 +03:00
import { defineReadOnly } from "@ethersproject/properties";
2020-07-14 09:33:30 +03:00
import { showThrottleMessage } from "./formatter";
2020-07-05 07:01:57 +03:00
import { WebSocketProvider } from "./websocket-provider";
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-08 03:10:50 +03:00
export class AlchemyWebSocketProvider extends WebSocketProvider {
constructor(network, apiKey) {
2020-07-05 07:01:57 +03:00
const provider = new AlchemyProvider(network, apiKey);
const url = provider.connection.url.replace(/^http/i, "ws")
.replace(".alchemyapi.", ".ws.alchemyapi.");
2020-10-08 03:10:50 +03: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 07:01:57 +03:00
}
static getApiKey(apiKey) {
if (apiKey == null) {
return defaultApiKey;
}
2019-11-20 12:57:38 +03:00
if (apiKey && typeof (apiKey) !== "string") {
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
}
return apiKey;
}
static getUrl(network, apiKey) {
let host = null;
switch (network.name) {
case "homestead":
2020-07-05 07:01:57 +03:00
host = "eth-mainnet.alchemyapi.io/v2/";
break;
case "ropsten":
2020-07-05 07:01:57 +03:00
host = "eth-ropsten.alchemyapi.io/v2/";
break;
case "rinkeby":
2020-07-05 07:01:57 +03:00
host = "eth-rinkeby.alchemyapi.io/v2/";
break;
2020-05-05 06:01:04 +03:00
case "goerli":
2020-07-05 07:01:57 +03:00
host = "eth-goerli.alchemyapi.io/v2/";
2020-05-05 06:01:04 +03:00
break;
case "kovan":
2020-07-05 07:01:57 +03:00
host = "eth-kovan.alchemyapi.io/v2/";
break;
default:
logger.throwArgumentError("unsupported network", "network", arguments[0]);
}
2020-07-14 09:33:30 +03:00
return {
2020-10-08 03:10:50 +03:00
allowGzip: true,
2020-07-14 09:33:30 +03:00
url: ("https:/" + "/" + host + apiKey),
throttleCallback: (attempt, url) => {
if (apiKey === defaultApiKey) {
showThrottleMessage();
}
return Promise.resolve(true);
}
};
}
2021-06-26 08:55:19 +03:00
perform(method, params) {
const _super = Object.create(null, {
perform: { get: () => super.perform }
});
return __awaiter(this, void 0, void 0, function* () {
if ((method === "estimateGas" && params.transaction.type === 2) || (method === "sendTransaction" && params.signedTransaction.substring(0, 4) === "0x02")) {
logger.throwError("AlchemyProvider does not currently support EIP-1559", Logger.errors.UNSUPPORTED_OPERATION, {
operation: method,
transaction: params.transaction
});
}
return _super.perform.call(this, method, params);
});
}
2020-10-08 03:10:50 +03:00
isCommunityResource() {
return (this.apiKey === defaultApiKey);
}
}
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=alchemy-provider.js.map