From 9ff4cacf0f9122c035537632d68169d7c2d5763f Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 7 Feb 2018 11:00:42 -0800 Subject: Addressed review comments --- packages/testnet-faucets/src/ts/dispatch_queue.ts | 19 +++++++--- .../testnet-faucets/src/ts/dispense_asset_tasks.ts | 44 +++++++++------------- packages/testnet-faucets/src/ts/error_reporter.ts | 3 -- packages/testnet-faucets/src/ts/handler.ts | 9 +---- 4 files changed, 31 insertions(+), 44 deletions(-) (limited to 'packages/testnet-faucets/src/ts') diff --git a/packages/testnet-faucets/src/ts/dispatch_queue.ts b/packages/testnet-faucets/src/ts/dispatch_queue.ts index 94d094203..672511619 100644 --- a/packages/testnet-faucets/src/ts/dispatch_queue.ts +++ b/packages/testnet-faucets/src/ts/dispatch_queue.ts @@ -1,6 +1,9 @@ import { intervalUtils } from '@0xproject/utils'; import * as _ from 'lodash'; +import { errorReporter } from './error_reporter'; +import { utils } from './utils'; + const MAX_QUEUE_SIZE = 500; const DEFAULT_QUEUE_INTERVAL_MS = 1000; @@ -13,11 +16,11 @@ export class DispatchQueue { this._queue = []; this._start(); } - public add(task: () => Promise): boolean { + public add(taskAsync: () => Promise): boolean { if (this.isFull()) { return false; } - this._queue.push(task); + this._queue.push(taskAsync); return true; } public size(): number { @@ -34,14 +37,18 @@ export class DispatchQueue { private _start() { this._queueIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval( async () => { - const task = this._queue.shift(); - if (_.isUndefined(task)) { + const taskAsync = this._queue.shift(); + if (_.isUndefined(taskAsync)) { return Promise.resolve(); } - await task(); + await taskAsync(); }, this._queueIntervalMs, - _.noop, + (err: Error) => { + utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); + // tslint:disable-next-line:no-floating-promises + errorReporter.reportAsync(err); + }, ); } } diff --git a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts index c70458fbe..9aa47463c 100644 --- a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts +++ b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts @@ -15,40 +15,30 @@ export const dispenseAssetTasks = { return async () => { utils.consoleLog(`Processing ETH ${recipientAddress}`); const sendTransactionAsync = promisify(web3.eth.sendTransaction); - try { - const txHash = await sendTransactionAsync({ - from: configs.DISPENSER_ADDRESS, - to: recipientAddress, - value: web3.toWei(DISPENSE_AMOUNT_ETHER, 'ether'), - }); - utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ETHER} ETH to ${recipientAddress} tx: ${txHash}`); - } catch (err) { - utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); - await errorReporter.reportAsync(err); - } + const txHash = await sendTransactionAsync({ + from: configs.DISPENSER_ADDRESS, + to: recipientAddress, + value: web3.toWei(DISPENSE_AMOUNT_ETHER, 'ether'), + }); + utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ETHER} ETH to ${recipientAddress} tx: ${txHash}`); }; }, dispenseTokenTask(recipientAddress: string, tokenSymbol: string, zeroEx: ZeroEx) { return async () => { utils.consoleLog(`Processing ${tokenSymbol} ${recipientAddress}`); const amountToDispense = new BigNumber(DISPENSE_AMOUNT_TOKEN); - try { - const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(tokenSymbol); - if (_.isUndefined(token)) { - throw new Error(`Unsupported asset type: ${tokenSymbol}`); - } - const baseUnitAmount = ZeroEx.toBaseUnitAmount(amountToDispense, token.decimals); - const txHash = await zeroEx.token.transferAsync( - token.address, - configs.DISPENSER_ADDRESS, - recipientAddress, - baseUnitAmount, - ); - utils.consoleLog(`Sent ${amountToDispense} ZRX to ${recipientAddress} tx: ${txHash}`); - } catch (err) { - utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); - await errorReporter.reportAsync(err); + const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(tokenSymbol); + if (_.isUndefined(token)) { + throw new Error(`Unsupported asset type: ${tokenSymbol}`); } + const baseUnitAmount = ZeroEx.toBaseUnitAmount(amountToDispense, token.decimals); + const txHash = await zeroEx.token.transferAsync( + token.address, + configs.DISPENSER_ADDRESS, + recipientAddress, + baseUnitAmount, + ); + utils.consoleLog(`Sent ${amountToDispense} ZRX to ${recipientAddress} tx: ${txHash}`); }; }, }; diff --git a/packages/testnet-faucets/src/ts/error_reporter.ts b/packages/testnet-faucets/src/ts/error_reporter.ts index 6865d3893..7fd76bde5 100644 --- a/packages/testnet-faucets/src/ts/error_reporter.ts +++ b/packages/testnet-faucets/src/ts/error_reporter.ts @@ -9,9 +9,7 @@ export const errorReporter = { rollbar.init(configs.ROLLBAR_ACCESS_KEY, { environment: configs.ENVIRONMENT, }); - rollbar.handleUncaughtExceptions(configs.ROLLBAR_ACCESS_KEY); - process.on('unhandledRejection', async (err: Error) => { utils.consoleLog(`Uncaught exception ${err}. Stack: ${err.stack}`); await this.reportAsync(err); @@ -22,7 +20,6 @@ export const errorReporter = { if (configs.ENVIRONMENT === 'development') { return; // Do not log development environment errors } - return new Promise((resolve, reject) => { rollbar.handleError(err, req, (rollbarErr: Error) => { if (rollbarErr) { diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts index ce7a81eba..ad05a13f5 100644 --- a/packages/testnet-faucets/src/ts/handler.ts +++ b/packages/testnet-faucets/src/ts/handler.ts @@ -96,26 +96,19 @@ export class Handler { private _dispenseAsset(req: express.Request, res: express.Response, requestedAssetType: RequestedAssetType) { const networkId = req.params.networkId; const recipient = req.params.recipient; - const networkConfig = _.get(this._networkConfigByNetworkId, networkId); - if (_.isUndefined(networkConfig)) { - res.status(400).send('UNSUPPORTED_NETWORK_ID'); - return; - } + const networkConfig = this._networkConfigByNetworkId[networkId]; let dispenserTask; switch (requestedAssetType) { case RequestedAssetType.ETH: dispenserTask = dispenseAssetTasks.dispenseEtherTask(recipient, networkConfig.web3); break; case RequestedAssetType.WETH: - dispenserTask = dispenseAssetTasks.dispenseTokenTask(recipient, requestedAssetType, networkConfig.zeroEx); - break; case RequestedAssetType.ZRX: dispenserTask = dispenseAssetTasks.dispenseTokenTask(recipient, requestedAssetType, networkConfig.zeroEx); break; default: throw new Error(`Unsupported asset type: ${requestedAssetType}`); } - const didAddToQueue = networkConfig.dispatchQueue.add(dispenserTask); if (!didAddToQueue) { res.status(503).send('QUEUE_IS_FULL'); -- cgit v1.2.3