From 49f1a6933cc22d1e703d631d5b861b8601ca2231 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 12 Jul 2018 23:13:47 +0200 Subject: Add fetchAsync util and RPCSubprovider --- packages/subproviders/src/globals.d.ts | 2 + packages/subproviders/src/index.ts | 1 + .../src/subproviders/rpc_subprovider.ts | 88 ++++++++++++++++++++++ .../subproviders/src/subproviders/subprovider.ts | 26 +++---- 4 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 packages/subproviders/src/subproviders/rpc_subprovider.ts (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 94e63a32d..6af4c7980 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -4,3 +4,5 @@ declare module '*.json' { export default json; /* tslint:enable */ } + +declare module 'json-rpc-error'; diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 71d643f14..9a5597a06 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -10,6 +10,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..87c522271 --- /dev/null +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -0,0 +1,88 @@ +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; + constructor(rpcUrl: string) { + super(); + assert.isString('rpcUrl', rpcUrl); + this._rpcUrl = rpcUrl; + } + /** + * 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 { + const finalPayload = Subprovider.createFinalPayload(payload); + const headers = new Headers({ + Accept: 'application/json', + 'Content-Type': 'application/json', + }); + const timeoutMs = 1000; + + let response; + try { + response = await fetchAsync( + this._rpcUrl, + { + method: 'POST', + headers, + body: JSON.stringify(finalPayload), + }, + timeoutMs, + ); + } 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..6f4a8f99e 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( + public static createFinalPayload( payload: Partial, ): Partial { 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, @@ -47,7 +47,7 @@ export abstract class Subprovider { * @returns JSON RPC response payload */ public async emitPayloadAsync(payload: Partial): Promise { - const finalPayload = Subprovider._createFinalPayload(payload); + const finalPayload = Subprovider.createFinalPayload(payload); const response = await promisify(this.engine.sendAsync, this.engine)(finalPayload); return response; } -- cgit From c2a1317c3813b13353eb84e4255284fe99ff8e6c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 12 Jul 2018 23:17:35 +0200 Subject: Adjust timeout --- packages/subproviders/src/subproviders/rpc_subprovider.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts index 87c522271..0fb99f87a 100644 --- a/packages/subproviders/src/subproviders/rpc_subprovider.ts +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -34,8 +34,10 @@ export class RPCSubprovider extends Subprovider { Accept: 'application/json', 'Content-Type': 'application/json', }); - const timeoutMs = 1000; + // Since Ethereum nodes have a proclivity for accepting requests and never + // returning a response, we set this quite low (10 seconds). + const timeoutMs = 10000; let response; try { response = await fetchAsync( -- cgit From 1229c61ba420b280708b39c04cc21f31e43ed3cf Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 12:05:02 +0200 Subject: Make timeout configurable on RPCSubprovider --- packages/subproviders/src/subproviders/rpc_subprovider.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts index 0fb99f87a..5c1c3272c 100644 --- a/packages/subproviders/src/subproviders/rpc_subprovider.ts +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -14,10 +14,12 @@ import { Subprovider } from './subprovider'; */ export class RPCSubprovider extends Subprovider { private _rpcUrl: string; - constructor(rpcUrl: string) { + private _requestTimeoutMs: number; + constructor(rpcUrl: string, requestTimeoutMs: number = 20000) { super(); assert.isString('rpcUrl', rpcUrl); this._rpcUrl = rpcUrl; + this._requestTimeoutMs = requestTimeoutMs; } /** * This method conforms to the web3-provider-engine interface. @@ -35,9 +37,6 @@ export class RPCSubprovider extends Subprovider { 'Content-Type': 'application/json', }); - // Since Ethereum nodes have a proclivity for accepting requests and never - // returning a response, we set this quite low (10 seconds). - const timeoutMs = 10000; let response; try { response = await fetchAsync( @@ -47,7 +46,7 @@ export class RPCSubprovider extends Subprovider { headers, body: JSON.stringify(finalPayload), }, - timeoutMs, + this._requestTimeoutMs, ); } catch (err) { end(new JsonRpcError.InternalError(err)); -- cgit From 701ea5c46bb1d25ffc2e6962042ba3e0c0277454 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 13:05:21 +0200 Subject: Add typedoc for json-rpc-error package --- packages/subproviders/src/globals.d.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 6af4c7980..837ed287d 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -5,4 +5,12 @@ declare module '*.json' { /* tslint:enable */ } -declare module 'json-rpc-error'; +// TODO: Move this to `typescript-typings` once it is more fleshed out +declare module 'json-rpc-error' { + export class InternalError extends Error { + constructor(err: Error | string); + } + export class MethodNotFound extends Error { + constructor(); + } +} -- cgit From 512502ca0873a290accb53777ca9d93f8d28e84b Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 14:26:02 +0200 Subject: Export Web3ProviderEngine from subproviders package --- packages/subproviders/src/index.ts | 1 + packages/subproviders/src/utils/subprovider_utils.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 9a5597a06..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'; 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 -- cgit From 70c3515c94e731f060aa8c532eb52d845f07b20f Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 15:04:17 +0200 Subject: Make createFinalPayload protected --- packages/subproviders/src/subproviders/rpc_subprovider.ts | 2 +- packages/subproviders/src/subproviders/subprovider.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts index 5c1c3272c..cb08e729e 100644 --- a/packages/subproviders/src/subproviders/rpc_subprovider.ts +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -31,7 +31,7 @@ export class RPCSubprovider extends Subprovider { */ // tslint:disable-next-line:prefer-function-over-method async-suffix public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback): Promise { - const finalPayload = Subprovider.createFinalPayload(payload); + const finalPayload = Subprovider._createFinalPayload(payload); const headers = new Headers({ Accept: 'application/json', 'Content-Type': 'application/json', diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts index 6f4a8f99e..5dc273569 100644 --- a/packages/subproviders/src/subproviders/subprovider.ts +++ b/packages/subproviders/src/subproviders/subprovider.ts @@ -9,7 +9,7 @@ import { Callback, ErrorCallback, JSONRPCRequestPayloadWithMethod } from '../typ export abstract class Subprovider { // tslint:disable-next-line:underscore-private-and-protected private engine!: Provider; - public static createFinalPayload( + protected static _createFinalPayload( payload: Partial, ): Partial { const finalPayload = { @@ -47,7 +47,7 @@ export abstract class Subprovider { * @returns JSON RPC response payload */ public async emitPayloadAsync(payload: Partial): Promise { - const finalPayload = Subprovider.createFinalPayload(payload); + const finalPayload = Subprovider._createFinalPayload(payload); const response = await promisify(this.engine.sendAsync, this.engine)(finalPayload); return response; } -- cgit From f5293e6c38b7e4e5352379e2fbd4d03c02b455b2 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 15:09:51 +0200 Subject: Fix linter --- packages/subproviders/src/subproviders/rpc_subprovider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts index cb08e729e..dac821d01 100644 --- a/packages/subproviders/src/subproviders/rpc_subprovider.ts +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -30,7 +30,7 @@ export class RPCSubprovider extends Subprovider { * @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 { + public async handleRequest(payload: JSONRPCRequestPayload, _next: Callback, end: ErrorCallback): Promise { const finalPayload = Subprovider._createFinalPayload(payload); const headers = new Headers({ Accept: 'application/json', -- cgit From 350989bbec59b731a11c69237bec8625f2331ce2 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 15:25:37 +0200 Subject: Move type defs to typescript-typingsd --- packages/subproviders/src/globals.d.ts | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 837ed287d..94e63a32d 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -4,13 +4,3 @@ declare module '*.json' { export default json; /* tslint:enable */ } - -// TODO: Move this to `typescript-typings` once it is more fleshed out -declare module 'json-rpc-error' { - export class InternalError extends Error { - constructor(err: Error | string); - } - export class MethodNotFound extends Error { - constructor(); - } -} -- cgit From 179c487da958dedda0f1a8cd93a2ce67f169e53c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 13 Jul 2018 15:25:48 +0200 Subject: Add missing assertion --- packages/subproviders/src/subproviders/rpc_subprovider.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/subproviders/src') diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts index dac821d01..3ad0e5325 100644 --- a/packages/subproviders/src/subproviders/rpc_subprovider.ts +++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts @@ -18,6 +18,7 @@ export class RPCSubprovider extends Subprovider { constructor(rpcUrl: string, requestTimeoutMs: number = 20000) { super(); assert.isString('rpcUrl', rpcUrl); + assert.isNumber('requestTimeoutMs', requestTimeoutMs); this._rpcUrl = rpcUrl; this._requestTimeoutMs = requestTimeoutMs; } -- cgit