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

45 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03:00
"use strict";
import * as errors from "@ethersproject/errors";
import { Network } from "@ethersproject/networks";
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"
export class AlchemyProvider extends UrlJsonRpcProvider {
readonly apiKey: string;
static getApiKey(apiKey: string): string {
if (apiKey == null) { return defaultApiKey; }
return apiKey;
}
static getUrl(network: Network, apiKey: string): string {
let host = null;
switch (network.name) {
case "homestead":
host = "eth-mainnet.alchemyapi.io/jsonrpc/";
break;
case "ropsten":
host = "eth-ropsten.alchemyapi.io/jsonrpc/";
break;
case "rinkeby":
host = "eth-rinkeby.alchemyapi.io/jsonrpc/";
break;
case "kovan":
host = "eth-kovan.alchemyapi.io/jsonrpc/";
break;
default:
errors.throwArgumentError("unsupported network", "network", arguments[0]);
}
return ("https:/" + "/" + host + apiKey);
}
}