ethers.js/packages/providers/src.ts/url-json-rpc-provider.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03:00
"use strict";
import { getNetwork, Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
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 { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
export class UrlJsonRpcProvider extends JsonRpcProvider {
readonly apiKey: string;
constructor(network?: Networkish, apiKey?: string) {
2019-08-02 01:04:06 +03:00
logger.checkAbstract(new.target, UrlJsonRpcProvider);
2019-05-15 01:25:46 +03:00
2019-05-24 01:24:31 +03:00
// Normalize the Network and API Key
network = new.target.getNetwork(network);
2019-05-15 01:25:46 +03:00
apiKey = new.target.getApiKey(apiKey);
let url = new.target.getUrl(network, apiKey);
super(url, network);
defineReadOnly(this, "apiKey", apiKey);
}
_startPending(): void {
2019-08-02 01:04:06 +03:00
logger.warn("WARNING: API provider does not support pending filters");
2019-05-15 01:25:46 +03:00
}
getSigner(address?: string): JsonRpcSigner {
2019-08-02 01:04:06 +03:00
logger.throwError(
2019-05-15 01:25:46 +03:00
"API provider does not support signing",
2019-08-02 01:04:06 +03:00
Logger.errors.UNSUPPORTED_OPERATION,
2019-05-15 01:25:46 +03:00
{ operation: "getSigner" }
);
return null;
}
listAccounts(): Promise<Array<string>> {
return Promise.resolve([]);
}
2019-05-24 01:24:31 +03:00
static getNetwork(network?: Networkish): Network {
return getNetwork((network == null) ? "homestead": network);
}
2019-05-15 01:25:46 +03:00
// Return a defaultApiKey if null, otherwise validate the API key
static getApiKey(apiKey: string): string {
return apiKey;
}
// Returns the url for the given network and API key
static getUrl(network: Network, apiKey: string): string {
2019-08-02 01:04:06 +03:00
return logger.throwError("not implemented; sub-classes must override getUrl", Logger.errors.NOT_IMPLEMENTED, {
2019-05-15 01:25:46 +03:00
operation: "getUrl"
});
}
}