Skip to content

Commit

Permalink
Added CloudflareProvider (#587).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Aug 21, 2019
1 parent 724c32e commit 621313d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 121 deletions.
6 changes: 6 additions & 0 deletions packages/networks/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function ethDefaultProvider(network: string): (providers: any) => any {
} catch(error) { }
}

if (providers.CloudflareProvider) {
try {
providerList.push(new providers.CloudflareProvider(network));
} catch(error) { }
}

if (providerList.length === 0) { return null; }

if (providers.FallbackProvider) {
Expand Down
28 changes: 28 additions & 0 deletions packages/providers/src.ts/cloudflare-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

import { Network } from "@ethersproject/networks";
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

export class CloudflareProvider extends UrlJsonRpcProvider {

static getUrl(network: Network, apiKey?: string): string {
if (apiKey != null) {
logger.throwArgumentError("apiKey not supported for cloudflare", "apiKey", apiKey);
}

let host = null;
switch (network.name) {
case "homestead":
host = "https://cloudflare-eth.com/";
break;
default:
logger.throwArgumentError("unsupported network", "network", arguments[0]);
}

return host;
}
}
2 changes: 2 additions & 0 deletions packages/providers/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Network, Networkish } from "@ethersproject/networks";
import { BaseProvider } from "./base-provider";

import { AlchemyProvider } from "./alchemy-provider";
import { CloudflareProvider } from "./cloudflare-provider";
import { EtherscanProvider } from "./etherscan-provider";
import { FallbackProvider } from "./fallback-provider";
import { IpcProvider } from "./ipc-provider";
Expand Down Expand Up @@ -47,6 +48,7 @@ export {
FallbackProvider,

AlchemyProvider,
CloudflareProvider,
EtherscanProvider,
InfuraProvider,
JsonRpcProvider,
Expand Down
3 changes: 2 additions & 1 deletion packages/providers/src.ts/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export class JsonRpcProvider extends BaseProvider {
response: result,
provider: this
});

return result;
});
}
Expand Down Expand Up @@ -350,7 +351,7 @@ export class JsonRpcProvider extends BaseProvider {
} else if (params.blockHash) {
return this.send("eth_getBlockByHash", [ params.blockHash, !!params.includeTransactions ]);
}
return Promise.reject(new Error("invalid block tag or block hash"));
return logger.throwArgumentError("invalid block tag or block hash", "params", params);

case "getTransaction":
return this.send("eth_getTransactionByHash", [ params.transactionHash ]);
Expand Down
8 changes: 4 additions & 4 deletions packages/providers/src.ts/url-json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import { getNetwork, Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
import { defineReadOnly, getStatic } from "@ethersproject/properties";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
Expand All @@ -16,10 +16,10 @@ export class UrlJsonRpcProvider extends JsonRpcProvider {
logger.checkAbstract(new.target, UrlJsonRpcProvider);

// Normalize the Network and API Key
network = new.target.getNetwork(network);
apiKey = new.target.getApiKey(apiKey);
network = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
apiKey = getStatic<(apiKey: string) => string>(new.target, "getApiKey")(apiKey);

let url = new.target.getUrl(network, apiKey);
let url = getStatic<(network: Network, apiKey: string) => string>(new.target, "getUrl")(network, apiKey);

super(url, network);

Expand Down
Loading

0 comments on commit 621313d

Please sign in to comment.