Addressed review comments
This commit is contained in:
@@ -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<void>): boolean {
|
||||
public add(taskAsync: () => Promise<void>): 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);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}`);
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user