aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-07-14 02:15:50 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-07-14 02:15:50 +0800
commitb70f5d1a1efcb3b07673aa3b4bc7bc2318f55462 (patch)
treed011565b1c95d4682381f8f573afadf98911e396 /packages/subproviders/src
parentc5fcdd06579b67b5b129749474acd9a4d1d4db65 (diff)
parent26363931ed8ba35a55073d00c4fe128c7e616acc (diff)
downloaddexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar.gz
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar.bz2
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar.lz
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar.xz
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.tar.zst
dexon-sol-tools-b70f5d1a1efcb3b07673aa3b4bc7bc2318f55462.zip
Merge branch 'v2-prototype' of https://github.com/0xProject/0x-monorepo into bug/website/txhash-error
Diffstat (limited to 'packages/subproviders/src')
-rw-r--r--packages/subproviders/src/index.ts2
-rw-r--r--packages/subproviders/src/subproviders/rpc_subprovider.ts90
-rw-r--r--packages/subproviders/src/subproviders/subprovider.ts24
-rw-r--r--packages/subproviders/src/utils/subprovider_utils.ts4
4 files changed, 106 insertions, 14 deletions
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index 71d643f14..9d4480e59 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -1,5 +1,6 @@
import Eth from '@ledgerhq/hw-app-eth';
import TransportU2F from '@ledgerhq/hw-transport-u2f';
+export import Web3ProviderEngine = require('web3-provider-engine');
export { ECSignature } from '@0xproject/types';
import { LedgerEthereumClient } from './types';
@@ -10,6 +11,7 @@ export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_sub
export { SignerSubprovider } from './subproviders/signer';
export { RedundantSubprovider } from './subproviders/redundant_subprovider';
export { LedgerSubprovider } from './subproviders/ledger';
+export { RPCSubprovider } from './subproviders/rpc_subprovider';
export { GanacheSubprovider } from './subproviders/ganache';
export { Subprovider } from './subproviders/subprovider';
export { NonceTrackerSubprovider } from './subproviders/nonce_tracker';
diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts
new file mode 100644
index 000000000..3ad0e5325
--- /dev/null
+++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts
@@ -0,0 +1,90 @@
+import { assert } from '@0xproject/assert';
+import { StatusCodes } from '@0xproject/types';
+import { fetchAsync } from '@0xproject/utils';
+import { JSONRPCRequestPayload } from 'ethereum-types';
+import JsonRpcError = require('json-rpc-error');
+
+import { Callback, ErrorCallback } from '../types';
+
+import { Subprovider } from './subprovider';
+
+/**
+ * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
+ * It forwards on JSON RPC requests to the supplied `rpcUrl` endpoint
+ */
+export class RPCSubprovider extends Subprovider {
+ private _rpcUrl: string;
+ private _requestTimeoutMs: number;
+ constructor(rpcUrl: string, requestTimeoutMs: number = 20000) {
+ super();
+ assert.isString('rpcUrl', rpcUrl);
+ assert.isNumber('requestTimeoutMs', requestTimeoutMs);
+ this._rpcUrl = rpcUrl;
+ this._requestTimeoutMs = requestTimeoutMs;
+ }
+ /**
+ * This method conforms to the web3-provider-engine interface.
+ * It is called internally by the ProviderEngine when it is this subproviders
+ * turn to handle a JSON RPC request.
+ * @param payload JSON RPC payload
+ * @param next Callback to call if this subprovider decides not to handle the request
+ * @param end Callback to call if subprovider handled the request and wants to pass back the request.
+ */
+ // tslint:disable-next-line:prefer-function-over-method async-suffix
+ public async handleRequest(payload: JSONRPCRequestPayload, _next: Callback, end: ErrorCallback): Promise<void> {
+ const finalPayload = Subprovider._createFinalPayload(payload);
+ const headers = new Headers({
+ Accept: 'application/json',
+ 'Content-Type': 'application/json',
+ });
+
+ let response;
+ try {
+ response = await fetchAsync(
+ this._rpcUrl,
+ {
+ method: 'POST',
+ headers,
+ body: JSON.stringify(finalPayload),
+ },
+ this._requestTimeoutMs,
+ );
+ } catch (err) {
+ end(new JsonRpcError.InternalError(err));
+ return;
+ }
+
+ const text = await response.text();
+ if (!response.ok) {
+ const statusCode = response.status;
+ switch (statusCode) {
+ case StatusCodes.MethodNotAllowed:
+ end(new JsonRpcError.MethodNotFound());
+ return;
+ case StatusCodes.GatewayTimeout:
+ const errMsg =
+ 'Gateway timeout. The request took too long to process. This can happen when querying logs over too wide a block range.';
+ const err = new Error(errMsg);
+ end(new JsonRpcError.InternalError(err));
+ return;
+ default:
+ end(new JsonRpcError.InternalError(text));
+ return;
+ }
+ }
+
+ let data;
+ try {
+ data = JSON.parse(text);
+ } catch (err) {
+ end(new JsonRpcError.InternalError(err));
+ return;
+ }
+
+ if (data.error) {
+ end(data.error);
+ return;
+ }
+ end(null, data.result);
+ }
+}
diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts
index ff8378c4e..5dc273569 100644
--- a/packages/subproviders/src/subproviders/subprovider.ts
+++ b/packages/subproviders/src/subproviders/subprovider.ts
@@ -9,18 +9,7 @@ import { Callback, ErrorCallback, JSONRPCRequestPayloadWithMethod } from '../typ
export abstract class Subprovider {
// tslint:disable-next-line:underscore-private-and-protected
private engine!: Provider;
- // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js
- private static _getRandomId(): number {
- const extraDigits = 3;
- const baseTen = 10;
- // 13 time digits
- const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits);
- // 3 random digits
- const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits));
- // 16 digits
- return datePart + extraPart;
- }
- private static _createFinalPayload(
+ protected static _createFinalPayload(
payload: Partial<JSONRPCRequestPayloadWithMethod>,
): Partial<JSONRPCRequestPayloadWithMethod> {
const finalPayload = {
@@ -32,6 +21,17 @@ export abstract class Subprovider {
};
return finalPayload;
}
+ // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js
+ private static _getRandomId(): number {
+ const extraDigits = 3;
+ const baseTen = 10;
+ // 13 time digits
+ const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits);
+ // 3 random digits
+ const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits));
+ // 16 digits
+ return datePart + extraPart;
+ }
// tslint:disable-next-line:async-suffix
public abstract async handleRequest(
payload: JSONRPCRequestPayload,
diff --git a/packages/subproviders/src/utils/subprovider_utils.ts b/packages/subproviders/src/utils/subprovider_utils.ts
index 24ebedd06..beda408ab 100644
--- a/packages/subproviders/src/utils/subprovider_utils.ts
+++ b/packages/subproviders/src/utils/subprovider_utils.ts
@@ -1,4 +1,4 @@
-import ProviderEngine = require('web3-provider-engine');
+import Web3ProviderEngine = require('web3-provider-engine');
import { Subprovider } from '../subproviders/subprovider';
@@ -7,7 +7,7 @@ import { Subprovider } from '../subproviders/subprovider';
* @param provider Given provider
* @param subprovider Subprovider to prepend
*/
-export function prependSubprovider(provider: ProviderEngine, subprovider: Subprovider): void {
+export function prependSubprovider(provider: Web3ProviderEngine, subprovider: Subprovider): void {
subprovider.setEngine(provider);
// HACK: We use implementation details of provider engine here
// https://github.com/MetaMask/provider-engine/blob/master/index.js#L68