This commit is contained in:
Fabio Berger
2018-07-13 12:42:01 +02:00
parent 1229c61ba4
commit 2e5ff53d72
20 changed files with 45 additions and 75 deletions

View File

@@ -1,7 +1,7 @@
import { assert } from '@0xproject/assert';
import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/types';
import 'isomorphic-fetch';
import { fetchAsync } from '@0xproject/utils';
import * as _ from 'lodash';
import * as queryString from 'query-string';
@@ -167,7 +167,7 @@ export class HttpClient implements Client {
const headers = new Headers({
'content-type': 'application/json',
});
const response = await fetch(url, {
const response = await fetchAsync(url, {
method: requestType,
body: JSON.stringify(payload),
headers,

View File

@@ -1,7 +1,11 @@
import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { EmptyWalletSubprovider, FakeGasEstimateSubprovider, GanacheSubprovider } from '@0xproject/subproviders';
import {
EmptyWalletSubprovider,
FakeGasEstimateSubprovider,
GanacheSubprovider,
RPCSubprovider,
} from '@0xproject/subproviders';
import * as fs from 'fs';
import * as _ from 'lodash';
@@ -49,11 +53,7 @@ export const web3Factory = {
}),
);
} else {
provider.addProvider(
new RpcSubprovider({
rpcUrl: config.rpcUrl || constants.RPC_URL,
}),
);
provider.addProvider(new RPCSubprovider(config.rpcUrl || constants.RPC_URL));
}
provider.start();
return provider;

View File

@@ -1,10 +1,9 @@
import { env, EnvVars } from '@0xproject/dev-utils';
import { GanacheSubprovider, prependSubprovider } from '@0xproject/subproviders';
import { GanacheSubprovider, prependSubprovider, RPCSubprovider } from '@0xproject/subproviders';
import { errorUtils, logUtils } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as fs from 'fs';
import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { config } from './config';
import { coverage } from './coverage';
@@ -45,7 +44,7 @@ if (testProvider === ProviderType.Ganache) {
}),
);
} else {
provider.addProvider(new RpcSubprovider({ rpcUrl: 'http://localhost:8501' }));
provider.addProvider(new RPCSubprovider('http://localhost:8501'));
}
provider.start();

View File

@@ -1,10 +1,9 @@
import { LedgerEthereumClient, LedgerSubprovider } from '@0xproject/subproviders';
import { LedgerEthereumClient, LedgerSubprovider, RPCSubprovider } from '@0xproject/subproviders';
import Eth from '@ledgerhq/hw-app-eth';
// tslint:disable:no-implicit-dependencies
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid';
import { Provider } from 'ethereum-types';
import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { constants } from './constants';
@@ -22,11 +21,7 @@ export const providerFactory = {
};
const ledgerSubprovider = new LedgerSubprovider(ledgerWalletConfigs);
provider.addProvider(ledgerSubprovider);
provider.addProvider(
new RpcSubprovider({
rpcUrl: constants.RPC_URL,
}),
);
provider.addProvider(new RPCSubprovider(constants.RPC_URL));
provider.start();
return provider;
},

View File

@@ -87,7 +87,6 @@
"ethereum-types": "^0.0.2",
"chalk": "^2.3.0",
"ethereumjs-util": "^5.1.1",
"isomorphic-fetch": "^2.2.1",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
"require-from-string": "^2.0.1",

View File

@@ -8,11 +8,10 @@ import {
Resolver,
URLResolver,
} from '@0xproject/sol-resolver';
import { logUtils } from '@0xproject/utils';
import { fetchAsync, logUtils } from '@0xproject/utils';
import chalk from 'chalk';
import * as ethUtil from 'ethereumjs-util';
import * as fs from 'fs';
import 'isomorphic-fetch';
import * as _ from 'lodash';
import * as path from 'path';
import * as requireFromString from 'require-from-string';
@@ -149,7 +148,7 @@ export class Compiler {
} else {
logUtils.log(`Downloading ${fullSolcVersion}...`);
const url = `${constants.BASE_COMPILER_URL}${fullSolcVersion}`;
const response = await fetch(url);
const response = await fetchAsync(url);
const SUCCESS_STATUS = 200;
if (response.status !== SUCCESS_STATUS) {
throw new Error(`Failed to load ${fullSolcVersion}`);

View File

@@ -8,9 +8,8 @@ import * as chai from 'chai';
import { JSONRPCResponsePayload } from 'ethereum-types';
import * as ethUtils from 'ethereumjs-util';
import Web3ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { LedgerSubprovider } from '../../src';
import { LedgerSubprovider, RPCSubprovider } from '../../src';
import { LedgerEthereumClient } from '../../src/types';
import { chaiSetup } from '../chai_setup';
import { fixtureData } from '../utils/fixture_data';
@@ -86,9 +85,7 @@ describe('LedgerSubprovider', () => {
before(() => {
ledgerProvider = new Web3ProviderEngine();
ledgerProvider.addProvider(ledgerSubprovider);
const httpProvider = new RpcSubprovider({
rpcUrl: 'http://localhost:8545',
});
const httpProvider = new RPCSubprovider('http://localhost:8545');
ledgerProvider.addProvider(httpProvider);
ledgerProvider.start();

View File

@@ -3,9 +3,8 @@ import * as chai from 'chai';
import { JSONRPCResponsePayload } from 'ethereum-types';
import * as Sinon from 'sinon';
import Web3ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { RedundantSubprovider } from '../../src';
import { RedundantSubprovider, RPCSubprovider } from '../../src';
import { Subprovider } from '../../src/subproviders/subprovider';
import { chaiSetup } from '../chai_setup';
import { ganacheSubprovider } from '../utils/ganache_subprovider';
@@ -39,9 +38,7 @@ describe('RedundantSubprovider', () => {
});
it('succeeds when supplied at least one healthy endpoint', (done: DoneCallback) => {
provider = new Web3ProviderEngine();
const nonExistentSubprovider = new RpcSubprovider({
rpcUrl: 'http://does-not-exist:3000',
});
const nonExistentSubprovider = new RPCSubprovider('http://does-not-exist:3000');
const handleRequestStub = Sinon.stub(nonExistentSubprovider, 'handleRequest').throws(
new Error('REQUEST_FAILED'),
);

View File

@@ -5,9 +5,8 @@ import { Provider } from 'ethereum-types';
import * as express from 'express';
import * as _ from 'lodash';
import { NonceTrackerSubprovider, PrivateKeyWalletSubprovider } from '@0xproject/subproviders';
import { NonceTrackerSubprovider, PrivateKeyWalletSubprovider, RPCSubprovider } from '@0xproject/subproviders';
import ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import { configs } from './configs';
import { constants } from './constants';
@@ -42,11 +41,7 @@ export class Handler {
const engine = new ProviderEngine();
engine.addProvider(new NonceTrackerSubprovider());
engine.addProvider(new PrivateKeyWalletSubprovider(configs.DISPENSER_PRIVATE_KEY));
engine.addProvider(
new RpcSubprovider({
rpcUrl,
}),
);
engine.addProvider(new RPCSubprovider(rpcUrl));
engine.start();
return engine;
}

View File

@@ -229,7 +229,3 @@ export enum StatusCodes {
MethodNotAllowed = 405,
GatewayTimeout = 504,
}
export interface FetchRequest extends RequestInit {
timeout?: number;
}

View File

@@ -1,29 +1,31 @@
import { FetchRequest } from '@0xproject/types';
import 'isomorphic-fetch';
export const fetchAsync = async (
endpoint: string,
options: FetchRequest,
options: RequestInit = {},
timeoutMs: number = 20000,
): Promise<Response> => {
let finalOptions;
let optionsWithAbortParam;
if ((process as any).browser === true) {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => {
controller.abort();
}, timeoutMs);
finalOptions = {
optionsWithAbortParam = {
signal,
...options,
};
} else {
finalOptions = {
// HACK: the `timeout` param only exists in `node-fetch`, and not on the `isomorphic-fetch`
// `RequestInit` type. Since `isomorphic-fetch` conditionally wraps `node-fetch` when the
// execution environment is `Node.js`, we need to cast it to `any` in that scenario.
optionsWithAbortParam = {
timeout: timeoutMs,
...options,
};
} as any;
}
const response = await fetch(endpoint, finalOptions);
const response = await fetch(endpoint, optionsWithAbortParam);
return response;
};

View File

@@ -9,4 +9,3 @@ export { abiUtils } from './abi_utils';
export { NULL_BYTES } from './constants';
export { errorUtils } from './error_utils';
export { fetchAsync } from './fetchAsync';
export { FetchRequest } from '@0xproject/types';

View File

@@ -63,7 +63,6 @@
"thenby": "^1.2.3",
"truffle-contract": "2.0.1",
"web3-provider-engine": "14.0.6",
"whatwg-fetch": "^2.0.3",
"xml-js": "^1.6.4"
},
"devDependencies": {

View File

@@ -15,6 +15,7 @@ import {
ledgerEthereumBrowserClientFactoryAsync,
LedgerSubprovider,
RedundantSubprovider,
RPCSubprovider,
SignerSubprovider,
Subprovider,
} from '@0xproject/subproviders';
@@ -62,7 +63,6 @@ import { errorReporter } from 'ts/utils/error_reporter';
import { utils } from 'ts/utils/utils';
import ProviderEngine = require('web3-provider-engine');
import FilterSubprovider = require('web3-provider-engine/subproviders/filters');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
import * as MintableArtifacts from '../contracts/Mintable.json';
@@ -157,9 +157,7 @@ export class Blockchain {
provider.addProvider(ledgerSubprovider);
provider.addProvider(new FilterSubprovider());
const rpcSubproviders = _.map(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkIdIfExists], publicNodeUrl => {
return new RpcSubprovider({
rpcUrl: publicNodeUrl,
});
return new RPCSubprovider(publicNodeUrl);
});
provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[]));
provider.start();
@@ -171,9 +169,7 @@ export class Blockchain {
provider.addProvider(new SignerSubprovider(injectedWeb3.currentProvider));
provider.addProvider(new FilterSubprovider());
const rpcSubproviders = _.map(publicNodeUrlsIfExistsForNetworkId, publicNodeUrl => {
return new RpcSubprovider({
rpcUrl: publicNodeUrl,
});
return new RPCSubprovider(publicNodeUrl);
});
provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[]));
provider.start();
@@ -189,9 +185,7 @@ export class Blockchain {
provider.addProvider(new FilterSubprovider());
const networkId = constants.NETWORK_ID_MAINNET;
const rpcSubproviders = _.map(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId], publicNodeUrl => {
return new RpcSubprovider({
rpcUrl: publicNodeUrl,
});
return new RPCSubprovider(publicNodeUrl);
});
provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[]));
provider.start();

View File

@@ -1,5 +1,5 @@
import { ECSignature } from '@0xproject/types';
import { BigNumber, logUtils } from '@0xproject/utils';
import { BigNumber, fetchAsync, logUtils } from '@0xproject/utils';
import * as _ from 'lodash';
import Paper from 'material-ui/Paper';
import TextField from 'material-ui/TextField';
@@ -148,7 +148,7 @@ You can see and fill it here: ${this.state.shareLink}`);
const bitlyRequestUrl = `${constants.URL_BITLY_API}/v3/shorten?access_token=${
configs.BITLY_ACCESS_TOKEN
}&longUrl=${longUrl}`;
const response = await fetch(bitlyRequestUrl);
const response = await fetchAsync(bitlyRequestUrl);
const responseBody = await response.text();
const bodyObj = JSON.parse(responseBody);
if (response.status !== 200 || bodyObj.status_code !== 200) {

View File

@@ -5,7 +5,7 @@ import {
Styles,
utils as sharedUtils,
} from '@0xproject/react-shared';
import { BigNumber, errorUtils, logUtils } from '@0xproject/utils';
import { BigNumber, errorUtils, fetchAsync, logUtils } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
import Dialog from 'material-ui/Dialog';
@@ -548,7 +548,7 @@ export class TokenBalances extends React.Component<TokenBalancesProps, TokenBala
await utils.sleepAsync(ARTIFICIAL_FAUCET_REQUEST_DELAY);
const segment = isEtherRequest ? 'ether' : 'zrx';
const response = await fetch(
const response = await fetchAsync(
`${constants.URL_TESTNET_FAUCET}/${segment}/${this.props.userAddress}?networkId=${this.props.networkId}`,
);
const responseBody = await response.text();

View File

@@ -20,7 +20,6 @@ import { analytics } from 'ts/utils/analytics';
import { muiTheme } from 'ts/utils/mui_theme';
import { utils } from 'ts/utils/utils';
// Polyfills
import 'whatwg-fetch';
injectTapEventPlugin();
// Check if we've introduced an update that requires us to clear the tradeHistory local storage entries

View File

@@ -1,5 +1,5 @@
import { DoxityDocObj, TypeDocNode } from '@0xproject/react-docs';
import { logUtils } from '@0xproject/utils';
import { fetchAsync, logUtils } from '@0xproject/utils';
import findVersions = require('find-versions');
import * as _ from 'lodash';
import { S3FileObject, VersionToFilePath } from 'ts/types';
@@ -16,7 +16,7 @@ export const docUtils = {
return versionToFilePath;
},
async getVersionFileNamesAsync(s3DocJsonRoot: string, folderName: string): Promise<string[]> {
const response = await fetch(s3DocJsonRoot);
const response = await fetchAsync(s3DocJsonRoot);
if (response.status !== 200) {
// TODO: Show the user an error message when the docs fail to load
const errMsg = await response.text();
@@ -73,7 +73,7 @@ export const docUtils = {
},
async getJSONDocFileAsync(filePath: string, s3DocJsonRoot: string): Promise<TypeDocNode | DoxityDocObj> {
const endpoint = `${s3DocJsonRoot}/${filePath}`;
const response = await fetch(endpoint);
const response = await fetchAsync(endpoint);
if (response.status !== 200) {
// TODO: Show the user an error message when the docs fail to load
const errMsg = await response.text();

View File

@@ -1,4 +1,4 @@
import { logUtils } from '@0xproject/utils';
import { fetchAsync, logUtils } from '@0xproject/utils';
import * as _ from 'lodash';
import * as queryString from 'query-string';
@@ -19,14 +19,14 @@ export const fetchUtils = {
async requestAsync(baseUrl: string, path: string, queryParams?: object): Promise<any> {
const query = queryStringFromQueryParams(queryParams);
const url = `${baseUrl}${path}${query}`;
const response = await fetch(url);
const response = await fetchAsync(url);
logErrorIfPresent(response, url);
const result = await response.json();
return result;
},
async postAsync(baseUrl: string, path: string, body: object): Promise<Response> {
const url = `${baseUrl}${path}`;
const response = await fetch(url, {
const response = await fetchAsync(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View File

@@ -13270,7 +13270,7 @@ websocket@^1.0.24, websocket@^1.0.25:
typedarray-to-buffer "^3.1.2"
yaeti "^0.0.6"
whatwg-fetch@2.0.3, whatwg-fetch@>=0.10.0, whatwg-fetch@^2.0.3:
whatwg-fetch@2.0.3, whatwg-fetch@>=0.10.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"