ethers.js/packages/providers/src.ts/infura-provider.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03:00
"use strict";
import { Network } from "@ethersproject/networks";
2019-08-02 01:04:06 +03:00
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-15 01:25:46 +03:00
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
const defaultProjectId = "84842078b09946638c03157f83405213"
export class InfuraProvider extends UrlJsonRpcProvider {
get projectId(): string { return this.apiKey; }
static getApiKey(apiKey: string): string {
if (apiKey == null) { return defaultProjectId; }
return apiKey;
}
static getUrl(network: Network, apiKey: string): string {
let host = null;
switch(network.name) {
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-02 01:04:06 +03:00
logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
2019-05-15 01:25:46 +03:00
argument: "network",
value: network
});
}
return "https:/" + "/" + host + "/v3/" + apiKey;
2019-05-15 01:25:46 +03:00
}
}