From addca63938c459635aba777104bab05dee0a5183 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 26 Feb 2018 18:45:56 -0800 Subject: Prevent a single account from draining the faucet --- .../testnet-faucets/src/ts/dispense_asset_tasks.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts index 9aa47463c..fd7350704 100644 --- a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts +++ b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts @@ -9,11 +9,21 @@ import { utils } from './utils'; const DISPENSE_AMOUNT_ETHER = 0.1; const DISPENSE_AMOUNT_TOKEN = 0.1; +const DISPENSE_MAX_AMOUNT_TOKEN = 5; +const DISPENSE_MAX_AMOUNT_ETHER = 5; export const dispenseAssetTasks = { dispenseEtherTask(recipientAddress: string, web3: Web3) { return async () => { utils.consoleLog(`Processing ETH ${recipientAddress}`); + const userBalance = await promisify(web3.eth.getBalance)(recipientAddress); + const maxAmountInWei = new BigNumber(web3.toWei(DISPENSE_MAX_AMOUNT_ETHER, 'ether')); + if (userBalance.greaterThan(maxAmountInWei)) { + utils.consoleLog( + `User exceeded ETH balance maximum (${maxAmountInWei}) ${recipientAddress} ${userBalance} `, + ); + return; + } const sendTransactionAsync = promisify(web3.eth.sendTransaction); const txHash = await sendTransactionAsync({ from: configs.DISPENSER_ADDRESS, @@ -32,6 +42,17 @@ export const dispenseAssetTasks = { throw new Error(`Unsupported asset type: ${tokenSymbol}`); } const baseUnitAmount = ZeroEx.toBaseUnitAmount(amountToDispense, token.decimals); + const userBalanceBaseUnits = await zeroEx.token.getBalanceAsync(token.address, recipientAddress); + const maxAmountBaseUnits = ZeroEx.toBaseUnitAmount( + new BigNumber(DISPENSE_MAX_AMOUNT_TOKEN), + token.decimals, + ); + if (userBalanceBaseUnits.greaterThan(maxAmountBaseUnits)) { + utils.consoleLog( + `User exceeded token balance maximum (${maxAmountBaseUnits}) ${recipientAddress} ${userBalanceBaseUnits} `, + ); + return; + } const txHash = await zeroEx.token.transferAsync( token.address, configs.DISPENSER_ADDRESS, -- cgit From a46199e37dddd255fbfa6339d9f5a2a4d035ee86 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 28 Feb 2018 14:25:07 -0800 Subject: Set max to 2 ETH/2 ZRX --- packages/testnet-faucets/src/ts/dispense_asset_tasks.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts index fd7350704..56b0a9e45 100644 --- a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts +++ b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts @@ -9,8 +9,8 @@ import { utils } from './utils'; const DISPENSE_AMOUNT_ETHER = 0.1; const DISPENSE_AMOUNT_TOKEN = 0.1; -const DISPENSE_MAX_AMOUNT_TOKEN = 5; -const DISPENSE_MAX_AMOUNT_ETHER = 5; +const DISPENSE_MAX_AMOUNT_TOKEN = 2; +const DISPENSE_MAX_AMOUNT_ETHER = 2; export const dispenseAssetTasks = { dispenseEtherTask(recipientAddress: string, web3: Web3) { @@ -18,7 +18,7 @@ export const dispenseAssetTasks = { utils.consoleLog(`Processing ETH ${recipientAddress}`); const userBalance = await promisify(web3.eth.getBalance)(recipientAddress); const maxAmountInWei = new BigNumber(web3.toWei(DISPENSE_MAX_AMOUNT_ETHER, 'ether')); - if (userBalance.greaterThan(maxAmountInWei)) { + if (userBalance.greaterThanOrEqualTo(maxAmountInWei)) { utils.consoleLog( `User exceeded ETH balance maximum (${maxAmountInWei}) ${recipientAddress} ${userBalance} `, ); @@ -47,7 +47,7 @@ export const dispenseAssetTasks = { new BigNumber(DISPENSE_MAX_AMOUNT_TOKEN), token.decimals, ); - if (userBalanceBaseUnits.greaterThan(maxAmountBaseUnits)) { + if (userBalanceBaseUnits.greaterThanOrEqualTo(maxAmountBaseUnits)) { utils.consoleLog( `User exceeded token balance maximum (${maxAmountBaseUnits}) ${recipientAddress} ${userBalanceBaseUnits} `, ); -- cgit