Skip to content

Commit

Permalink
Merge pull request #25 from rsksmart/feat/estimation-fee
Browse files Browse the repository at this point in the history
Feat/estimation fee
  • Loading branch information
ezequiel-rodriguez authored Aug 22, 2024
2 parents 70c344b + 892479d commit 1cb0f5e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dev-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions pages/api/dispense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion prod-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 8 additions & 2 deletions utils/tx-parameters-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1cb0f5e

Please sign in to comment.