2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
2020-05-03 14:20:33 -04:00
|
|
|
import { Network, Networkish } from "@ethersproject/networks";
|
2020-10-07 17:28:18 -04:00
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
2019-11-19 18:00:05 +09:00
|
|
|
import { ConnectionInfo } from "@ethersproject/web";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-03 14:20:33 -04:00
|
|
|
import { WebSocketProvider } from "./websocket-provider";
|
2020-10-07 17:28:18 -04:00
|
|
|
import { CommunityResourcable, showThrottleMessage } from "./formatter";
|
2020-05-03 14:20:33 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
|
|
|
|
|
|
|
|
|
|
|
const defaultProjectId = "84842078b09946638c03157f83405213"
|
|
|
|
|
2020-10-07 17:28:18 -04:00
|
|
|
export class InfuraWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
|
|
|
|
readonly apiKey: string;
|
2019-11-19 18:00:05 +09:00
|
|
|
readonly projectId: string;
|
|
|
|
readonly projectSecret: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-10-07 17:28:18 -04:00
|
|
|
constructor(network?: Networkish, apiKey?: any) {
|
2020-05-03 14:20:33 -04: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-07 17:28:18 -04:00
|
|
|
super(url, network);
|
|
|
|
|
|
|
|
defineReadOnly(this, "apiKey", provider.projectId);
|
|
|
|
defineReadOnly(this, "projectId", provider.projectId);
|
|
|
|
defineReadOnly(this, "projectSecret", provider.projectSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.projectId === defaultProjectId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InfuraProvider extends UrlJsonRpcProvider {
|
|
|
|
readonly projectId: string;
|
|
|
|
readonly projectSecret: string;
|
|
|
|
|
|
|
|
static getWebSocketProvider(network?: Networkish, apiKey?: any): InfuraWebSocketProvider {
|
|
|
|
return new InfuraWebSocketProvider(network, apiKey);
|
2020-05-03 14:20:33 -04:00
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
static getApiKey(apiKey: any): any {
|
|
|
|
const apiKeyObj: { apiKey: string, projectId: string, projectSecret: string } = {
|
2019-11-20 18:28:13 +09:00
|
|
|
apiKey: defaultProjectId,
|
2019-11-19 18:00:05 +09:00
|
|
|
projectId: defaultProjectId,
|
|
|
|
projectSecret: null
|
|
|
|
};
|
|
|
|
|
2019-11-20 18:28:13 +09:00
|
|
|
if (apiKey == null) { return apiKeyObj; }
|
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
if (typeof(apiKey) === "string") {
|
|
|
|
apiKeyObj.projectId = apiKey;
|
|
|
|
|
|
|
|
} else if (apiKey.projectSecret != null) {
|
2020-07-13 07:26:40 -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-19 18:00:05 +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:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-07-14 02:26:45 -04:00
|
|
|
static getUrl(network: Network, apiKey: any): ConnectionInfo {
|
2019-11-19 18:00:05 +09:00
|
|
|
let host: string = null;
|
2020-07-13 07:26:40 -04:00
|
|
|
switch(network ? network.name: "unknown") {
|
2019-05-14 18:25:46 -04:00
|
|
|
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;
|
|
|
|
default:
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
2019-05-14 18:25:46 -04:00
|
|
|
argument: "network",
|
|
|
|
value: network
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
const connection: ConnectionInfo = {
|
2020-10-07 19:43:20 -04:00
|
|
|
allowGzip: true,
|
|
|
|
url: ("https:/" + "/" + host + "/v3/" + apiKey.projectId),
|
|
|
|
throttleCallback: (attempt: number, url: string) => {
|
|
|
|
if (apiKey.projectId === defaultProjectId) {
|
|
|
|
showThrottleMessage();
|
|
|
|
}
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
2019-11-19 18:00:05 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
if (apiKey.projectSecret != null) {
|
|
|
|
connection.user = "";
|
|
|
|
connection.password = apiKey.projectSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-10-07 17:28:18 -04:00
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.projectId === defaultProjectId);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|