From e63841a604385d8b20561cee683a105bd5b33096 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 5 Sep 2018 18:11:57 +0100 Subject: Check token exists for network for user params --- .../testnet-faucets/src/ts/dispense_asset_tasks.ts | 15 ++++++++------ packages/testnet-faucets/src/ts/handler.ts | 24 +++++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts index 5c2002600..fa63ab63e 100644 --- a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts +++ b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts @@ -42,15 +42,18 @@ export const dispenseAssetTasks = { return async () => { logUtils.log(`Processing ${tokenSymbol} ${recipientAddress}`); const amountToDispense = new BigNumber(DISPENSE_AMOUNT_TOKEN); - const token = TOKENS_BY_NETWORK[networkId][tokenSymbol]; - if (_.isUndefined(token)) { + const tokenIfExists = _.get(TOKENS_BY_NETWORK, [networkId, tokenSymbol]); + if (_.isUndefined(tokenIfExists)) { throw new Error(`Unsupported asset type: ${tokenSymbol}`); } - const baseUnitAmount = Web3Wrapper.toBaseUnitAmount(amountToDispense, token.decimals); - const userBalanceBaseUnits = await erc20TokenWrapper.getBalanceAsync(token.address, recipientAddress); + const baseUnitAmount = Web3Wrapper.toBaseUnitAmount(amountToDispense, tokenIfExists.decimals); + const userBalanceBaseUnits = await erc20TokenWrapper.getBalanceAsync( + tokenIfExists.address, + recipientAddress, + ); const maxAmountBaseUnits = Web3Wrapper.toBaseUnitAmount( new BigNumber(DISPENSE_MAX_AMOUNT_TOKEN), - token.decimals, + tokenIfExists.decimals, ); if (userBalanceBaseUnits.greaterThanOrEqualTo(maxAmountBaseUnits)) { logUtils.log( @@ -59,7 +62,7 @@ export const dispenseAssetTasks = { return; } const txHash = await erc20TokenWrapper.transferAsync( - token.address, + tokenIfExists.address, configs.DISPENSER_ADDRESS, recipientAddress, baseUnitAmount, diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts index 2a816cebd..82e9a9494 100644 --- a/packages/testnet-faucets/src/ts/handler.ts +++ b/packages/testnet-faucets/src/ts/handler.ts @@ -107,7 +107,11 @@ export class Handler { private _dispenseAsset(req: express.Request, res: express.Response, requestedAssetType: RequestedAssetType): void { const networkId = req.params.networkId; const recipient = req.params.recipient; - const networkConfig = this._networkConfigByNetworkId[networkId]; + const networkConfig = _.get(this._networkConfigByNetworkId, networkId); + if (_.isUndefined(networkConfig)) { + res.status(constants.BAD_REQUEST_STATUS).send('UNSUPPORTED_NETWORK_ID'); + return; + } let dispenserTask; switch (requestedAssetType) { case RequestedAssetType.ETH: @@ -144,20 +148,20 @@ export class Handler { return; } res.setHeader('Content-Type', 'application/json'); - const makerToken = TOKENS_BY_NETWORK[networkConfig.networkId][requestedAssetType]; - if (_.isUndefined(makerToken)) { + const makerTokenIfExists = _.get(TOKENS_BY_NETWORK, [networkConfig.networkId, requestedAssetType]); + if (_.isUndefined(makerTokenIfExists)) { throw new Error(`Unsupported asset type: ${requestedAssetType}`); } const takerTokenSymbol = requestedAssetType === RequestedAssetType.WETH ? RequestedAssetType.ZRX : RequestedAssetType.WETH; - const takerToken = TOKENS_BY_NETWORK[networkConfig.networkId][takerTokenSymbol]; - if (_.isUndefined(takerToken)) { - throw new Error(`Unsupported asset type: ${requestedAssetType}`); + const takerTokenIfExists = _.get(TOKENS_BY_NETWORK, [networkConfig.networkId, takerTokenSymbol]); + if (_.isUndefined(takerTokenIfExists)) { + throw new Error(`Unsupported asset type: ${takerTokenSymbol}`); } - const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(ASSET_AMOUNT, makerToken.decimals); - const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(ASSET_AMOUNT, takerToken.decimals); - const makerAssetData = assetDataUtils.encodeERC20AssetData(makerToken.address); - const takerAssetData = assetDataUtils.encodeERC20AssetData(takerToken.address); + const makerAssetAmount = Web3Wrapper.toBaseUnitAmount(ASSET_AMOUNT, makerTokenIfExists.decimals); + const takerAssetAmount = Web3Wrapper.toBaseUnitAmount(ASSET_AMOUNT, takerTokenIfExists.decimals); + const makerAssetData = assetDataUtils.encodeERC20AssetData(makerTokenIfExists.address); + const takerAssetData = assetDataUtils.encodeERC20AssetData(takerTokenIfExists.address); const order: Order = { makerAddress: configs.DISPENSER_ADDRESS, takerAddress: req.params.recipient as string, -- cgit