Skip to content

Commit

Permalink
Merge pull request #21 from rsksmart/feat/flag-filter-by-ip
Browse files Browse the repository at this point in the history
Filter by IP
  • Loading branch information
IOVgomezdn authored May 29, 2024
2 parents 4f409a3 + 20e6ec4 commit 2e8234f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

*.htm
*.htm

*.env
3 changes: 2 additions & 1 deletion dev-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"GAS_PRICE": 60000000,
"GAS_LIMIT": 100000,
"VALUE_TO_DISPENSE": 0.005,
"TAG_MANAGER_ID": "NO_ID"
"TAG_MANAGER_ID": "NO_ID",
"FILTER_BY_IP": true
}
5 changes: 3 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 { provider } from '../../utils/env-util';
import { filterByIP, provider } from '../../utils/env-util';
import {
alreadyDispensed,
captchaRejected,
Expand Down Expand Up @@ -180,9 +180,10 @@ function runValidations(
}

function filterAddresses(faucetHistory: FaucetHistory, dispenseAddress: string, ip:string) {
const isFilterByIP = filterByIP();
const key = Object.keys(faucetHistory).find((key) => {
const historyEntry = faucetHistory[key];
return historyEntry.ip === ip || historyEntry.address === dispenseAddress.toLowerCase()
return (historyEntry.ip === ip && isFilterByIP) || historyEntry.address === dispenseAddress.toLowerCase()
});
const adddress = key ? faucetHistory[key!] : null;
if (!adddress?.mint && !adddress?.loading) delete faucetHistory[dispenseAddress.toLowerCase()]
Expand Down
3 changes: 2 additions & 1 deletion prod-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"GAS_PRICE": 60000000,
"GAS_LIMIT": 100000,
"VALUE_TO_DISPENSE": 0.005,
"TAG_MANAGER_ID": "NO_ID"
"TAG_MANAGER_ID": "NO_ID",
"FILTER_BY_IP": true
}
3 changes: 2 additions & 1 deletion test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"FAUCET_PRIVATE_KEY": "c3d40c98585e2c61add9c6a94b66cd7f5c5577e45d900c6c0e3139df1310292f",
"GAS_PRICE": 60000000,
"GAS_LIMIT": 100000,
"VALUE_TO_DISPENSE": 0.005
"VALUE_TO_DISPENSE": 0.005,
"FILTER_BY_IP": true
}
8 changes: 8 additions & 0 deletions utils/env-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ export function siteKey(): string {
export function tagManagerId(): string {
return productionDevelopmentTest(prodConfig.TAG_MANAGER_ID, devConfig.TAG_MANAGER_ID, devConfig.TAG_MANAGER_ID);
}

export function filterByIP(): string {
return productionDevelopmentTest(
prodConfig.FILTER_BY_IP,
devConfig.FILTER_BY_IP,
devConfig.FILTER_BY_IP
);
}
4 changes: 3 additions & 1 deletion utils/validations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CaptchaSolutionResponse, FaucetHistory } from '../types/types';
import {isValidAddress} from 'rskjs-util';
import { filterByIP } from './env-util';

const EROR_CODE = {
'missing-input-secret': 'The secret parameter is missing.',
Expand All @@ -18,7 +19,8 @@ export const alreadyDispensed = (address: string, ip:string, faucetHistory: Fauc
const usedAddress = faucetHistory.hasOwnProperty(address)
const key = Object.keys(faucetHistory).find((key) => faucetHistory[key].ip === ip);
const usedIp = key ? faucetHistory[key!] : null;
if (usedIp?.ip) return 'IP already used today, try again tomorrow.'
const isFilterByIP = filterByIP();
if (usedIp?.ip && isFilterByIP) return 'IP already used today, try again tomorrow.'
if (usedAddress) return 'Address already used today, try again tomorrow.'
faucetHistory[address] = {
address,
Expand Down

0 comments on commit 2e8234f

Please sign in to comment.