Added CloudflareProvider (#587).
This commit is contained in:
parent
724c32e8c0
commit
621313d2a6
@ -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) {
|
||||
|
28
packages/providers/src.ts/cloudflare-provider.ts
Normal file
28
packages/providers/src.ts/cloudflare-provider.ts
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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";
|
||||
@ -47,6 +48,7 @@ export {
|
||||
FallbackProvider,
|
||||
|
||||
AlchemyProvider,
|
||||
CloudflareProvider,
|
||||
EtherscanProvider,
|
||||
InfuraProvider,
|
||||
JsonRpcProvider,
|
||||
|
@ -301,6 +301,7 @@ export class JsonRpcProvider extends BaseProvider {
|
||||
response: result,
|
||||
provider: this
|
||||
});
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
@ -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 ]);
|
||||
|
@ -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";
|
||||
@ -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);
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
import assert from "assert";
|
||||
|
||||
@ -13,39 +13,39 @@ type Dictionary = { [ key: string ]: any };
|
||||
const blockchainData: any = {
|
||||
homestead: {
|
||||
balance: {
|
||||
address: '0xAC1639CF97a3A46D431e6d1216f576622894cBB5',
|
||||
balance: bnify('4918774100000000')
|
||||
address: "0xAC1639CF97a3A46D431e6d1216f576622894cBB5",
|
||||
balance: bnify("4918774100000000")
|
||||
},
|
||||
block3: {
|
||||
hash: '0x3d6122660cc824376f11ee842f83addc3525e2dd6756b9bcf0affa6aa88cf741',
|
||||
parentHash: '0xb495a1d7e6663152ae92708da4843337b958146015a2802f4193a410044698c9',
|
||||
hash: "0x3d6122660cc824376f11ee842f83addc3525e2dd6756b9bcf0affa6aa88cf741",
|
||||
parentHash: "0xb495a1d7e6663152ae92708da4843337b958146015a2802f4193a410044698c9",
|
||||
number: 3,
|
||||
timestamp: 1438270048,
|
||||
nonce: '0x2e9344e0cbde83ce',
|
||||
nonce: "0x2e9344e0cbde83ce",
|
||||
difficulty: 17154715646,
|
||||
gasLimit: bnify('0x1388'),
|
||||
gasUsed: bnify('0'),
|
||||
miner: '0x5088D623ba0fcf0131E0897a91734A4D83596AA0',
|
||||
extraData: '0x476574682f76312e302e302d66633739643332642f6c696e75782f676f312e34',
|
||||
gasLimit: bnify("0x1388"),
|
||||
gasUsed: bnify("0"),
|
||||
miner: "0x5088D623ba0fcf0131E0897a91734A4D83596AA0",
|
||||
extraData: "0x476574682f76312e302e302d66633739643332642f6c696e75782f676f312e34",
|
||||
transactions: []
|
||||
},
|
||||
transaction: {
|
||||
hash: '0xccc90ab97a74c952fb3376c4a3efb566a58a10df62eb4d44a61e106fcf10ec61',
|
||||
blockHash: '0x9653f180a5720f3634816eb945a6d722adee52cc47526f6357ac10adaf368135',
|
||||
hash: "0xccc90ab97a74c952fb3376c4a3efb566a58a10df62eb4d44a61e106fcf10ec61",
|
||||
blockHash: "0x9653f180a5720f3634816eb945a6d722adee52cc47526f6357ac10adaf368135",
|
||||
blockNumber: 4097745,
|
||||
transactionIndex: 18,
|
||||
from: '0x32DEF047DeFd076DB21A2D759aff2A591c972248',
|
||||
gasPrice: bnify('0x4a817c800'),
|
||||
gasLimit: bnify('0x3d090'),
|
||||
to: '0x6fC21092DA55B392b045eD78F4732bff3C580e2c',
|
||||
value: bnify('0x186cc6acd4b0000'),
|
||||
from: "0x32DEF047DeFd076DB21A2D759aff2A591c972248",
|
||||
gasPrice: bnify("0x4a817c800"),
|
||||
gasLimit: bnify("0x3d090"),
|
||||
to: "0x6fC21092DA55B392b045eD78F4732bff3C580e2c",
|
||||
value: bnify("0x186cc6acd4b0000"),
|
||||
nonce: 0,
|
||||
data: '0xf2c298be000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067269636d6f6f0000000000000000000000000000000000000000000000000000',
|
||||
r: '0x1e5605197a03e3f0a168f14749168dfeefc44c9228312dacbffdcbbb13263265',
|
||||
s: '0x269c3e5b3558267ad91b0a887d51f9f10098771c67b82ea6cb74f29638754f54',
|
||||
data: "0xf2c298be000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067269636d6f6f0000000000000000000000000000000000000000000000000000",
|
||||
r: "0x1e5605197a03e3f0a168f14749168dfeefc44c9228312dacbffdcbbb13263265",
|
||||
s: "0x269c3e5b3558267ad91b0a887d51f9f10098771c67b82ea6cb74f29638754f54",
|
||||
v: 38,
|
||||
creates: null,
|
||||
raw: '0xf8d2808504a817c8008303d090946fc21092da55b392b045ed78f4732bff3c580e2c880186cc6acd4b0000b864f2c298be000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067269636d6f6f000000000000000000000000000000000000000000000000000026a01e5605197a03e3f0a168f14749168dfeefc44c9228312dacbffdcbbb13263265a0269c3e5b3558267ad91b0a887d51f9f10098771c67b82ea6cb74f29638754f54',
|
||||
raw: "0xf8d2808504a817c8008303d090946fc21092da55b392b045ed78f4732bff3c580e2c880186cc6acd4b0000b864f2c298be000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067269636d6f6f000000000000000000000000000000000000000000000000000026a01e5605197a03e3f0a168f14749168dfeefc44c9228312dacbffdcbbb13263265a0269c3e5b3558267ad91b0a887d51f9f10098771c67b82ea6cb74f29638754f54",
|
||||
networkId: 1
|
||||
},
|
||||
transactionReceipt: {
|
||||
@ -116,57 +116,57 @@ const blockchainData: any = {
|
||||
},
|
||||
kovan: {
|
||||
balance: {
|
||||
address: '0x09c967A0385eE3B3717779738cA0B9D116e0EcE7',
|
||||
balance: bnify('997787946734641021')
|
||||
address: "0x09c967A0385eE3B3717779738cA0B9D116e0EcE7",
|
||||
balance: bnify("997787946734641021")
|
||||
},
|
||||
block3: {
|
||||
hash: '0xf0ec9bf41b99a6bd1f6cd29f91302f71a1a82d14634d2e207edea4b7962f3676',
|
||||
parentHash: '0xf110ecd84454f116e2222378e7bca81ac3e59be0dac96d7ec56d5ef1c3bc1d64',
|
||||
hash: "0xf0ec9bf41b99a6bd1f6cd29f91302f71a1a82d14634d2e207edea4b7962f3676",
|
||||
parentHash: "0xf110ecd84454f116e2222378e7bca81ac3e59be0dac96d7ec56d5ef1c3bc1d64",
|
||||
number: 3,
|
||||
timestamp: 1488459452,
|
||||
difficulty: 131072,
|
||||
gasLimit: bnify('0x5b48ec'),
|
||||
gasUsed: bnify('0'),
|
||||
miner: '0x00A0A24b9f0E5EC7Aa4c7389b8302fd0123194dE',
|
||||
extraData: '0xd5830105048650617269747986312e31352e31826c69',
|
||||
gasLimit: bnify("0x5b48ec"),
|
||||
gasUsed: bnify("0"),
|
||||
miner: "0x00A0A24b9f0E5EC7Aa4c7389b8302fd0123194dE",
|
||||
extraData: "0xd5830105048650617269747986312e31352e31826c69",
|
||||
transactions: []
|
||||
},
|
||||
},
|
||||
rinkeby: {
|
||||
balance: {
|
||||
address: '0xd09a624630a656a7dbb122cb05e41c12c7cd8c0e',
|
||||
balance: bnify('3000000000000000000')
|
||||
address: "0xd09a624630a656a7dbb122cb05e41c12c7cd8c0e",
|
||||
balance: bnify("3000000000000000000")
|
||||
},
|
||||
block3: {
|
||||
hash: '0x9eb9db9c3ec72918c7db73ae44e520139e95319c421ed6f9fc11fa8dd0cddc56',
|
||||
parentHash: '0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9',
|
||||
hash: "0x9eb9db9c3ec72918c7db73ae44e520139e95319c421ed6f9fc11fa8dd0cddc56",
|
||||
parentHash: "0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9",
|
||||
number: 3,
|
||||
timestamp: 1492010489,
|
||||
nonce: '0x0000000000000000',
|
||||
nonce: "0x0000000000000000",
|
||||
difficulty: 2,
|
||||
gasLimit: bnify('0x47e7c4'),
|
||||
gasLimit: bnify("0x47e7c4"),
|
||||
gasUsed: bnify(0),
|
||||
miner: '0x0000000000000000000000000000000000000000',
|
||||
extraData: '0xd783010600846765746887676f312e372e33856c696e757800000000000000004e10f96536e45ceca7e34cc1bdda71db3f3bb029eb69afd28b57eb0202c0ec0859d383a99f63503c4df9ab6c1dc63bf6b9db77be952f47d86d2d7b208e77397301',
|
||||
miner: "0x0000000000000000000000000000000000000000",
|
||||
extraData: "0xd783010600846765746887676f312e372e33856c696e757800000000000000004e10f96536e45ceca7e34cc1bdda71db3f3bb029eb69afd28b57eb0202c0ec0859d383a99f63503c4df9ab6c1dc63bf6b9db77be952f47d86d2d7b208e77397301",
|
||||
transactions: []
|
||||
},
|
||||
},
|
||||
ropsten: {
|
||||
balance: {
|
||||
address: '0x03a6F7a5ce5866d9A0CCC1D4C980b8d523f80480',
|
||||
balance: bnify('15861113897828552666')
|
||||
address: "0x03a6F7a5ce5866d9A0CCC1D4C980b8d523f80480",
|
||||
balance: bnify("15861113897828552666")
|
||||
},
|
||||
block3: {
|
||||
hash: '0xaf2f2d55e6514389bcc388ccaf40c6ebf7b3814a199a214f1203fb674076e6df',
|
||||
parentHash: '0x88e8bc1dd383672e96d77ee247e7524622ff3b15c337bd33ef602f15ba82d920',
|
||||
hash: "0xaf2f2d55e6514389bcc388ccaf40c6ebf7b3814a199a214f1203fb674076e6df",
|
||||
parentHash: "0x88e8bc1dd383672e96d77ee247e7524622ff3b15c337bd33ef602f15ba82d920",
|
||||
number: 3,
|
||||
timestamp: 1479642588,
|
||||
nonce: '0x04668f72247a130c',
|
||||
nonce: "0x04668f72247a130c",
|
||||
difficulty: 996427,
|
||||
gasLimit: bnify('0xff4033'),
|
||||
gasUsed: bnify('0'),
|
||||
miner: '0xD1aEb42885A43b72B518182Ef893125814811048',
|
||||
extraData: '0xd883010503846765746887676f312e372e318664617277696e',
|
||||
gasLimit: bnify("0xff4033"),
|
||||
gasUsed: bnify("0"),
|
||||
miner: "0xD1aEb42885A43b72B518182Ef893125814811048",
|
||||
extraData: "0xd883010503846765746887676f312e372e318664617277696e",
|
||||
transactions: []
|
||||
},
|
||||
transactionReceipt: {
|
||||
@ -229,28 +229,28 @@ const blockchainData: any = {
|
||||
balance: bnify("314159000000000000")
|
||||
},
|
||||
block3: {
|
||||
hash: '0xd5daa825732729bb0d2fd187a1b888e6bfc890f1fc5333984740d9052afb2920',
|
||||
parentHash: '0xe675f1362d82cdd1ec260b16fb046c17f61d8a84808150f5d715ccce775f575e',
|
||||
hash: "0xd5daa825732729bb0d2fd187a1b888e6bfc890f1fc5333984740d9052afb2920",
|
||||
parentHash: "0xe675f1362d82cdd1ec260b16fb046c17f61d8a84808150f5d715ccce775f575e",
|
||||
number: 3,
|
||||
timestamp: 1548947483,
|
||||
difficulty: 2,
|
||||
gasLimit: bnify('10455073'),
|
||||
gasUsed: bnify('0'),
|
||||
miner: '0x0000000000000000000000000000000000000000',
|
||||
extraData: '0x506172697479205465636820417574686f7269747900000000000000000000002822e1b202411c38084d96c84302b8361ec4840a51cd2fad9cb4bd9921cad7e64bc2e5dc7b41f3f75b33358be3aec718cf4d4317ace940e01b3581a95c9259ac01',
|
||||
gasLimit: bnify("10455073"),
|
||||
gasUsed: bnify("0"),
|
||||
miner: "0x0000000000000000000000000000000000000000",
|
||||
extraData: "0x506172697479205465636820417574686f7269747900000000000000000000002822e1b202411c38084d96c84302b8361ec4840a51cd2fad9cb4bd9921cad7e64bc2e5dc7b41f3f75b33358be3aec718cf4d4317ace940e01b3581a95c9259ac01",
|
||||
transactions: []
|
||||
},
|
||||
transactionReceipt: {
|
||||
blockHash: '0x2384e8e8bdcf6eb87ec7c138fa503ac34adb32cac817e4b35f14d4339eaa1993',
|
||||
blockHash: "0x2384e8e8bdcf6eb87ec7c138fa503ac34adb32cac817e4b35f14d4339eaa1993",
|
||||
blockNumber: 47464,
|
||||
byzantium: true,
|
||||
contractAddress: null,
|
||||
cumulativeGasUsed: bnify(21000),
|
||||
from: '0x8c1e1e5b47980D214965f3bd8ea34C413E120ae4',
|
||||
from: "0x8c1e1e5b47980D214965f3bd8ea34C413E120ae4",
|
||||
gasUsed: bnify(21000),
|
||||
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
|
||||
to: '0x58Bb4221245461E1d4cf886f18a01E3Df40Bd359',
|
||||
transactionHash: '0xec8b1ac5d787f36c738cc7793fec606283b41f1efa69df4ae6b2a014dcd12797',
|
||||
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
||||
to: "0x58Bb4221245461E1d4cf886f18a01E3Df40Bd359",
|
||||
transactionHash: "0xec8b1ac5d787f36c738cc7793fec606283b41f1efa69df4ae6b2a014dcd12797",
|
||||
transactionIndex: 0,
|
||||
logs: [],
|
||||
status: 1
|
||||
@ -258,23 +258,23 @@ const blockchainData: any = {
|
||||
}
|
||||
}
|
||||
|
||||
blockchainData['default'] = blockchainData.homestead;
|
||||
blockchainData["default"] = blockchainData.homestead;
|
||||
|
||||
function equals(name: string, actual: any, expected: any): void {
|
||||
if (expected && expected.eq) {
|
||||
if (actual == null) { assert.ok(false, name + ' - actual big number null'); }
|
||||
assert.ok(expected.eq(actual), name + ' matches');
|
||||
if (actual == null) { assert.ok(false, name + " - actual big number null"); }
|
||||
assert.ok(expected.eq(actual), name + " matches");
|
||||
|
||||
} else if (Array.isArray(expected)) {
|
||||
if (actual == null) { assert.ok(false, name + ' - actual array null'); }
|
||||
assert.equal(actual.length, expected.length, name + ' array lengths match');
|
||||
if (actual == null) { assert.ok(false, name + " - actual array null"); }
|
||||
assert.equal(actual.length, expected.length, name + " array lengths match");
|
||||
for (let i = 0; i < expected.length; i++) {
|
||||
equals('(' + name + ' - item ' + i + ')', actual[i], expected[i]);
|
||||
equals("(" + name + " - item " + i + ")", actual[i], expected[i]);
|
||||
}
|
||||
} else if (typeof(expected) === 'object') {
|
||||
} else if (typeof(expected) === "object") {
|
||||
if (actual == null) {
|
||||
if (expected === actual) { return; }
|
||||
assert.ok(false, name + ' - actual object null');
|
||||
assert.ok(false, name + " - actual object null");
|
||||
}
|
||||
|
||||
let keys: { [ key: string ]: boolean } = {};
|
||||
@ -282,31 +282,31 @@ function equals(name: string, actual: any, expected: any): void {
|
||||
Object.keys(actual).forEach((key) => { keys[key] = true; });
|
||||
|
||||
Object.keys(keys).forEach((key) => {
|
||||
equals('(' + name + ' - key + ' + key + ')', actual[key], expected[key]);
|
||||
equals("(" + name + " - key + " + key + ")", actual[key], expected[key]);
|
||||
});
|
||||
|
||||
} else {
|
||||
if (actual == null) { assert.ok(false, name + ' - actual null'); }
|
||||
assert.equal(actual, expected, name + ' matches');
|
||||
if (actual == null) { assert.ok(false, name + " - actual null"); }
|
||||
assert.equal(actual, expected, name + " matches");
|
||||
}
|
||||
}
|
||||
|
||||
function testProvider(providerName: string, networkName: string) {
|
||||
describe(('Read-Only ' + providerName + ' (' + networkName + ')'), function() {
|
||||
describe(("Read-Only " + providerName + " (" + networkName + ")"), function() {
|
||||
let provider: ethers.providers.Provider = null;
|
||||
if (networkName === 'default') {
|
||||
if (providerName === 'getDefaultProvider') {
|
||||
if (networkName === "default") {
|
||||
if (providerName === "getDefaultProvider") {
|
||||
provider = ethers.getDefaultProvider();
|
||||
} else if (providerName === 'Web3Provider') {
|
||||
} else if (providerName === "Web3Provider") {
|
||||
let infuraUrl = (new ethers.providers.InfuraProvider()).connection.url;
|
||||
provider = new ethers.providers.Web3Provider(new Web3HttpProvider(infuraUrl));
|
||||
} else {
|
||||
provider = new (<any>(ethers.providers))[providerName]();
|
||||
}
|
||||
} else {
|
||||
if (providerName === 'getDefaultProvider') {
|
||||
if (providerName === "getDefaultProvider") {
|
||||
provider = ethers.getDefaultProvider(networkName);
|
||||
} else if (providerName === 'Web3Provider') {
|
||||
} else if (providerName === "Web3Provider") {
|
||||
let infuraUrl = (new ethers.providers.InfuraProvider(networkName)).connection.url;
|
||||
provider = new ethers.providers.Web3Provider(new Web3HttpProvider(infuraUrl), networkName);
|
||||
} else {
|
||||
@ -314,17 +314,20 @@ function testProvider(providerName: string, networkName: string) {
|
||||
}
|
||||
}
|
||||
|
||||
it('fetches block #3', function() {
|
||||
it("fetches block #3", function() {
|
||||
this.timeout(20000);
|
||||
let test = blockchainData[networkName].block3;
|
||||
return provider.getBlock(3).then(function(block) {
|
||||
return provider.getBlock(3).then((block) => {
|
||||
for (let key in test) {
|
||||
equals('Block ' + key, (<any>block)[key], test[key]);
|
||||
equals("Block " + key, (<any>block)[key], test[key]);
|
||||
}
|
||||
}, (error) => {
|
||||
console.log("Error", error);
|
||||
assert.ok(false);
|
||||
});
|
||||
});
|
||||
|
||||
it('fetches address balance', function() {
|
||||
it("fetches address balance", function() {
|
||||
// @TODO: These tests could be fiddled with if someone sends ether to our address
|
||||
// We should set up a contract on each network like:
|
||||
//
|
||||
@ -337,48 +340,48 @@ function testProvider(providerName: string, networkName: string) {
|
||||
this.timeout(100000);
|
||||
let test = blockchainData[networkName].balance;
|
||||
return provider.getBalance(test.address).then(function(balance) {
|
||||
equals('Balance', test.balance, balance);
|
||||
equals("Balance", test.balance, balance);
|
||||
});
|
||||
});
|
||||
|
||||
function testTransactionReceipt(expected: Dictionary): Promise<void> {
|
||||
let title = ('Receipt ' + expected.transactionHash.substring(0, 10) + ' - ');
|
||||
let title = ("Receipt " + expected.transactionHash.substring(0, 10) + " - ");
|
||||
return provider.getTransactionReceipt(expected.transactionHash).then(function(receipt) {
|
||||
|
||||
// This changes with every block
|
||||
assert.equal(typeof(receipt.confirmations), 'number', 'confirmations is a number');
|
||||
assert.equal(typeof(receipt.confirmations), "number", "confirmations is a number");
|
||||
delete receipt.confirmations;
|
||||
|
||||
for (let key in receipt) {
|
||||
equals((title + key), (<any>receipt)[key], expected[key]);
|
||||
}
|
||||
//equals(('Receipt ' + expected.transactionHash.substring(0, 10)), receipt, expected);
|
||||
//equals(("Receipt " + expected.transactionHash.substring(0, 10)), receipt, expected);
|
||||
});
|
||||
}
|
||||
|
||||
if (blockchainData[networkName].transactionReceipt) {
|
||||
it('fetches pre-Byzantium transaction receipt', function() {
|
||||
it("fetches pre-Byzantium transaction receipt", function() {
|
||||
this.timeout(100000);
|
||||
return testTransactionReceipt(blockchainData[networkName].transactionReceipt);
|
||||
});
|
||||
}
|
||||
|
||||
if (blockchainData[networkName].transactionReceiptByzantium) {
|
||||
it('fetches Byzantium transaction receipt', function() {
|
||||
it("fetches Byzantium transaction receipt", function() {
|
||||
this.timeout(20000);
|
||||
return testTransactionReceipt(blockchainData[networkName].transactionReceiptByzantium);
|
||||
});
|
||||
}
|
||||
|
||||
function testTransaction(expected: Dictionary): Promise<void> {
|
||||
let title = ('Transaction ' + expected.hash.substring(0, 10) + ' - ');
|
||||
let title = ("Transaction " + expected.hash.substring(0, 10) + " - ");
|
||||
return provider.getTransaction(expected.hash).then((tx) => {
|
||||
|
||||
// This changes with every block
|
||||
assert.equal(typeof(tx.confirmations), 'number', 'confirmations is a number');
|
||||
assert.equal(typeof(tx.confirmations), "number", "confirmations is a number");
|
||||
delete tx.confirmations;
|
||||
|
||||
assert.equal(typeof(tx.wait), 'function', 'wait is a function');
|
||||
assert.equal(typeof(tx.wait), "function", "wait is a function");
|
||||
delete tx.wait
|
||||
|
||||
for (let key in tx) {
|
||||
@ -388,7 +391,7 @@ function testProvider(providerName: string, networkName: string) {
|
||||
}
|
||||
|
||||
if (blockchainData[networkName].transaction) {
|
||||
it('fetches transaction', function() {
|
||||
it("fetches transaction", function() {
|
||||
this.timeout(20000);
|
||||
return testTransaction(blockchainData[networkName].transaction);
|
||||
});
|
||||
@ -413,9 +416,10 @@ function testProvider(providerName: string, networkName: string) {
|
||||
});
|
||||
}
|
||||
|
||||
['default', 'homestead', 'ropsten', 'rinkeby', 'kovan', 'goerli'].forEach(function(networkName) {
|
||||
['getDefaultProvider', 'AlchemyProvider', 'InfuraProvider', 'EtherscanProvider', 'NodesmithProvider', 'Web3Provider'].forEach(function(providerName) {
|
||||
["default", "homestead", "ropsten", "rinkeby", "kovan", "goerli"].forEach(function(networkName) {
|
||||
["getDefaultProvider", "AlchemyProvider", "CloudflareProvider", "InfuraProvider", "EtherscanProvider", "NodesmithProvider", "Web3Provider"].forEach(function(providerName) {
|
||||
if (networkName === "goerli" && providerName === "AlchemyProvider") { return; }
|
||||
if ((networkName !== "homestead" && networkName !== "default") && providerName === "CloudflareProvider") { return; }
|
||||
testProvider(providerName, networkName);
|
||||
});
|
||||
});
|
||||
@ -426,7 +430,7 @@ function getDefaults(network, extra) {
|
||||
chainId: network.chainId,
|
||||
ensAddress: (network.ensAddress ? getAddress(network.ensAddress): null),
|
||||
name: network.name,
|
||||
testnet: (network.name !== 'homestead'),
|
||||
testnet: (network.name !== "homestead"),
|
||||
};
|
||||
for (let key in extra) {
|
||||
result[key] = extra[key];
|
||||
@ -435,25 +439,25 @@ function getDefaults(network, extra) {
|
||||
}
|
||||
*/
|
||||
/*
|
||||
describe('Test extra Etherscan operations', function() {
|
||||
describe("Test extra Etherscan operations", function() {
|
||||
let provider = new providers.EtherscanProvider();
|
||||
it('fethces the current price of ether', function() {
|
||||
it("fethces the current price of ether", function() {
|
||||
this.timeout(20000);
|
||||
return provider.getEtherPrice().then(function(price) {
|
||||
assert.ok(typeof(price) === 'number', 'Etherscan price returns a number');
|
||||
assert.ok(price > 0.0, 'Etherscan price returns non-zero');
|
||||
assert.ok(typeof(price) === "number", "Etherscan price returns a number");
|
||||
assert.ok(price > 0.0, "Etherscan price returns non-zero");
|
||||
});
|
||||
});
|
||||
it('fetches the history', function() {
|
||||
it("fetches the history", function() {
|
||||
this.timeout(100000);
|
||||
return provider.getHistory('ricmoo.firefly.eth').then(function(history) {
|
||||
assert.ok(history.length > 40, 'Etherscan history returns results');
|
||||
assert.equal(history[0].hash, '0xd25f550cfdff90c086a6496a84dbb2c4577df15b1416e5b3319a3e4ebb5b25d8', 'Etherscan history returns correct transaction');
|
||||
return provider.getHistory("ricmoo.firefly.eth").then(function(history) {
|
||||
assert.ok(history.length > 40, "Etherscan history returns results");
|
||||
assert.equal(history[0].hash, "0xd25f550cfdff90c086a6496a84dbb2c4577df15b1416e5b3319a3e4ebb5b25d8", "Etherscan history returns correct transaction");
|
||||
});
|
||||
});
|
||||
});
|
||||
*/
|
||||
describe('Test Basic Authentication', function() {
|
||||
describe("Test Basic Authentication", function() {
|
||||
// https://stackoverflow.com/questions/6509278/authentication-test-servers#16756383
|
||||
|
||||
type TestCase = {
|
||||
@ -464,42 +468,42 @@ describe('Test Basic Authentication', function() {
|
||||
};
|
||||
|
||||
function test(name: string, url: TestCase): void {
|
||||
it('tests ' + name, function() {
|
||||
it("tests " + name, function() {
|
||||
this.timeout(20000);
|
||||
return ethers.utils.fetchJson(url).then((data) => {
|
||||
assert.equal(data.authenticated, true, 'authenticates user');
|
||||
assert.equal(data.authenticated, true, "authenticates user");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let secure: TestCase = {
|
||||
url: 'https://httpbin.org/basic-auth/user/passwd',
|
||||
user: 'user',
|
||||
password: 'passwd'
|
||||
url: "https://httpbin.org/basic-auth/user/passwd",
|
||||
user: "user",
|
||||
password: "passwd"
|
||||
};
|
||||
|
||||
let insecure: TestCase = {
|
||||
url: 'http://httpbin.org/basic-auth/user/passwd',
|
||||
user: 'user',
|
||||
password: 'passwd'
|
||||
url: "http://httpbin.org/basic-auth/user/passwd",
|
||||
user: "user",
|
||||
password: "passwd"
|
||||
};
|
||||
|
||||
let insecureForced: TestCase = {
|
||||
url: 'http://httpbin.org/basic-auth/user/passwd',
|
||||
user: 'user',
|
||||
password: 'passwd',
|
||||
url: "http://httpbin.org/basic-auth/user/passwd",
|
||||
user: "user",
|
||||
password: "passwd",
|
||||
allowInsecureAuthentication: true
|
||||
};
|
||||
|
||||
test('secure url', secure);
|
||||
test('insecure url', insecureForced);
|
||||
test("secure url", secure);
|
||||
test("insecure url", insecureForced);
|
||||
|
||||
it('tests insecure connections fail', function() {
|
||||
it("tests insecure connections fail", function() {
|
||||
this.timeout(20000);
|
||||
assert.throws(() => {
|
||||
return ethers.utils.fetchJson(insecure);
|
||||
}, (error: Error) => {
|
||||
return ((<any>error).reason === 'basic authentication requires a secure https url');
|
||||
}, 'throws an exception for insecure connections');
|
||||
return ((<any>error).reason === "basic authentication requires a secure https url");
|
||||
}, "throws an exception for insecure connections");
|
||||
})
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user