2022-09-09 06:21:08 +03:00
|
|
|
import {
|
2022-11-05 01:08:37 +03:00
|
|
|
defineProperties, FetchRequest, assert, assertArgument
|
2022-09-09 06:21:08 +03:00
|
|
|
} from "../utils/index.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
import { showThrottleMessage } from "./community.js";
|
|
|
|
import { Network } from "./network.js";
|
|
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
2022-09-30 04:53:25 +03:00
|
|
|
import { WebSocketProvider } from "./provider-websocket.js";
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
import type { AbstractProvider } from "./abstract-provider.js";
|
|
|
|
import type { CommunityResourcable } from "./community.js";
|
|
|
|
import type { Networkish } from "./network.js";
|
|
|
|
|
|
|
|
|
|
|
|
const defaultProjectId = "84842078b09946638c03157f83405213";
|
|
|
|
|
|
|
|
function getHost(name: string): string {
|
|
|
|
switch(name) {
|
2022-09-30 04:53:25 +03:00
|
|
|
case "mainnet":
|
2022-09-05 23:14:43 +03:00
|
|
|
return "mainnet.infura.io";
|
|
|
|
case "goerli":
|
|
|
|
return "goerli.infura.io";
|
2022-10-20 11:54:26 +03:00
|
|
|
case "sepolia":
|
|
|
|
return "sepolia.infura.io";
|
|
|
|
|
|
|
|
case "arbitrum":
|
|
|
|
return "arbitrum-mainnet.infura.io";
|
|
|
|
case "arbitrum-goerli":
|
|
|
|
return "arbitrum-goerli.infura.io";
|
2022-09-05 23:14:43 +03:00
|
|
|
case "matic":
|
|
|
|
return "polygon-mainnet.infura.io";
|
|
|
|
case "maticmum":
|
|
|
|
return "polygon-mumbai.infura.io";
|
|
|
|
case "optimism":
|
|
|
|
return "optimism-mainnet.infura.io";
|
2022-10-20 11:54:26 +03:00
|
|
|
case "optimism-goerli":
|
|
|
|
return "optimism-goerli.infura.io";
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-10-25 11:06:00 +03:00
|
|
|
assertArgument(false, "unsupported network", "network", name);
|
2022-09-05 23:14:43 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 04:53:25 +03:00
|
|
|
export class InfuraWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
|
|
|
|
readonly projectId!: string;
|
|
|
|
readonly projectSecret!: null | string;
|
|
|
|
|
|
|
|
constructor(network?: Networkish, apiKey?: any) {
|
|
|
|
const provider = new InfuraProvider(network, apiKey);
|
|
|
|
|
|
|
|
const req = provider._getConnection();
|
2022-11-05 01:08:37 +03:00
|
|
|
assert(!req.credentials, "INFURA WebSocket project secrets unsupported",
|
|
|
|
"UNSUPPORTED_OPERATION", { operation: "InfuraProvider.getWebSocketProvider()" });
|
2022-09-30 04:53:25 +03:00
|
|
|
|
|
|
|
const url = req.url.replace(/^http/i, "ws").replace("/v3/", "/ws/v3/");
|
|
|
|
super(url, network);
|
|
|
|
|
|
|
|
defineProperties<InfuraWebSocketProvider>(this, {
|
|
|
|
projectId: provider.projectId,
|
|
|
|
projectSecret: provider.projectSecret
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.projectId === defaultProjectId);
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 23:14:43 +03:00
|
|
|
|
|
|
|
export class InfuraProvider extends JsonRpcProvider implements CommunityResourcable {
|
|
|
|
readonly projectId!: string;
|
|
|
|
readonly projectSecret!: null | string;
|
|
|
|
|
2022-09-30 04:53:25 +03:00
|
|
|
constructor(_network: Networkish = "mainnet", projectId?: null | string, projectSecret?: null | string) {
|
2022-09-05 23:14:43 +03:00
|
|
|
const network = Network.from(_network);
|
|
|
|
if (projectId == null) { projectId = defaultProjectId; }
|
|
|
|
if (projectSecret == null) { projectSecret = null; }
|
|
|
|
|
|
|
|
const request = InfuraProvider.getRequest(network, projectId, projectSecret);
|
|
|
|
super(request, network, { staticNetwork: network });
|
|
|
|
|
|
|
|
defineProperties<InfuraProvider>(this, { projectId, projectSecret });
|
|
|
|
}
|
|
|
|
|
|
|
|
_getProvider(chainId: number): AbstractProvider {
|
|
|
|
try {
|
|
|
|
return new InfuraProvider(chainId, this.projectId, this.projectSecret);
|
|
|
|
} catch (error) { }
|
|
|
|
return super._getProvider(chainId);
|
|
|
|
}
|
|
|
|
|
2022-09-30 04:53:25 +03:00
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.projectId === defaultProjectId);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getWebSocketProvider(network?: Networkish, apiKey?: any): InfuraWebSocketProvider {
|
|
|
|
return new InfuraWebSocketProvider(network, apiKey);
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:14:43 +03:00
|
|
|
static getRequest(network: Network, projectId?: null | string, projectSecret?: null | string): FetchRequest {
|
|
|
|
if (projectId == null) { projectId = defaultProjectId; }
|
|
|
|
if (projectSecret == null) { projectSecret = null; }
|
|
|
|
|
|
|
|
const request = new FetchRequest(`https:/\/${ getHost(network.name) }/v3/${ projectId }`);
|
|
|
|
request.allowGzip = true;
|
|
|
|
if (projectSecret) { request.setCredentials("", projectSecret); }
|
|
|
|
|
|
|
|
if (projectId === defaultProjectId) {
|
|
|
|
request.retryFunc = async (request, response, attempt) => {
|
|
|
|
showThrottleMessage("InfuraProvider");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|