diff --git a/dev-config.json b/dev-config.json index 6c765b9..79ee80a 100644 --- a/dev-config.json +++ b/dev-config.json @@ -8,7 +8,7 @@ "FAUCET_PRIVATE_KEY": "c3d40c98585e2c61add9c6a94b66cd7f5c5577e45d900c6c0e3139df1310292f", "GAS_PRICE": 60000000, "GAS_LIMIT": 100000, - "VALUE_TO_DISPENSE": 0.005, + "VALUE_TO_DISPENSE": 0.0005, "TAG_MANAGER_ID": "NO_ID", "FILTER_BY_IP": true, "TIMER_LIMIT": 180000 diff --git a/pages/api/dispense.ts b/pages/api/dispense.ts index 790ccb3..33b1fda 100644 --- a/pages/api/dispense.ts +++ b/pages/api/dispense.ts @@ -11,7 +11,7 @@ import { DispenseResponse, } from '../../types/types'; import { CronJob } from 'cron'; -import { filterByIP, provider } from '../../utils/env-util'; +import { filterByIP, provider, valueToDispense } from '../../utils/env-util'; import { alreadyDispensed, captchaRejected, @@ -96,8 +96,9 @@ const handleDispense = async (req: NextApiRequest, res: NextApiResponse): Promis res.status(409).end(JSON.stringify(data)); //409 Conflict } else { + const fee = await estimationFee(dispenseAddress); const txParametersGenerator = new TxParametersGenerator(); - const txParameters: TxParameters = await txParametersGenerator.generate(dispenseAddress, web3); + const txParameters: TxParameters = await txParametersGenerator.generate(dispenseAddress, web3, fee); logger.txParameters(txParameters); @@ -190,4 +191,19 @@ function filterAddresses(faucetHistory: FaucetHistory, dispenseAddress: string, return faucetHistory; } +async function estimationFee(dispenseAddress:string) { + const VALUE_TO_DISPENSE = valueToDispense(); + const value = web3.utils.toWei(VALUE_TO_DISPENSE.toString(), 'ether'); + const gasEstimate = await web3.eth.estimateGas({ + from: faucetAddress(), + to: dispenseAddress, + data: '', + value: value + }); + const gasPrice = await web3.eth.getGasPrice(); + const estimatedCost = web3.utils.toBN(gasEstimate).mul(web3.utils.toBN(gasPrice)); + const fee = web3.utils.fromWei(estimatedCost, 'ether'); + return Number(fee) || 0; +} + export default handleDispense; diff --git a/prod-config.json b/prod-config.json index 0cc65ad..06a29ce 100644 --- a/prod-config.json +++ b/prod-config.json @@ -6,7 +6,7 @@ "SITE_KEY_CAPTCHA": "", "GAS_PRICE": 60000000, "GAS_LIMIT": 100000, - "VALUE_TO_DISPENSE": 0.005, + "VALUE_TO_DISPENSE": 0.0005, "TAG_MANAGER_ID": "NO_ID", "FILTER_BY_IP": true, "TIMER_LIMIT": 180000 diff --git a/test-config.json b/test-config.json index 40a5b71..e48cf4b 100644 --- a/test-config.json +++ b/test-config.json @@ -8,7 +8,7 @@ "FAUCET_PRIVATE_KEY": "c3d40c98585e2c61add9c6a94b66cd7f5c5577e45d900c6c0e3139df1310292f", "GAS_PRICE": 60000000, "GAS_LIMIT": 100000, - "VALUE_TO_DISPENSE": 0.005, + "VALUE_TO_DISPENSE": 0.0005, "FILTER_BY_IP": true, "TIMER_LIMIT": 180000 } diff --git a/utils/tx-parameters-generator.ts b/utils/tx-parameters-generator.ts index 44876a9..59593db 100644 --- a/utils/tx-parameters-generator.ts +++ b/utils/tx-parameters-generator.ts @@ -4,14 +4,20 @@ import { faucetAddress } from './faucet-sensitive-util'; import Web3 from 'web3'; class TxParametersGenerator { - async generate(dispenseAddress: string, web3: Web3) { + async generate(dispenseAddress: string, web3: Web3, fee: number) { + const VALUE_TO_DISPENSE = valueToDispense(); + + const valueInWei = web3.utils.toWei(VALUE_TO_DISPENSE.toString(), 'ether'); + const feeInWei = web3.utils.toWei(fee.toString(), 'ether'); + const valueToSend = web3.utils.toBN(valueInWei).sub(web3.utils.toBN(feeInWei)); + const txParameters: TxParameters = { from: faucetAddress(), to: dispenseAddress, nonce: web3.utils.toHex(await web3.eth.getTransactionCount(faucetAddress(), 'pending')), gasPrice: web3.utils.toHex(gasPrice()), gas: web3.utils.toHex(gasLimit()), - value: web3.utils.toHex(web3.utils.toWei(valueToDispense().toString())) + value: web3.utils.toHex(valueToSend) }; return txParameters;