Allow configuring faucet cooldown value via env var FAUCET_COOLDOWN_TIME

This commit is contained in:
Simon Warta 2022-08-18 16:45:37 +02:00
parent e514144e48
commit 6b11defd4c
5 changed files with 20 additions and 4 deletions

View File

@ -26,6 +26,8 @@ and this project adheres to
- @cosmjs/stargate: Add Amino JSON support for `MsgCreateVestingAccount`.
- @cosmjs/stargate and @cosmjs/cosmwasm-stargate: Create and use
BroadcastTxError ([#1096]).
- @cosmjs/faucet: Allow configuring the cooldown value via
`FAUCET_COOLDOWN_TIME` environment variable.
[#1072]: https://github.com/cosmos/cosmjs/issues/1072
[#1096]: https://github.com/cosmos/cosmjs/issues/1096

View File

@ -64,6 +64,9 @@ FAUCET_CREDIT_AMOUNT_TKN Send this amount of TKN to a user requesting TKN. TKN
FAUCET_REFILL_FACTOR Send factor times credit amount on refilling. Defauls to 8.
FAUCET_REFILL_THRESHOLD Refill when balance gets below factor times credit amount.
Defaults to 20.
FAUCET_COOLDOWN_TIME Time (in seconds) after which an address can request
more tokens. Can be set to "0". Defaults to 24 hours
if unset or an empty string.
```
### Faucet HD wallet

View File

@ -37,6 +37,9 @@ FAUCET_CREDIT_AMOUNT_TKN Send this amount of TKN to a user requesting TKN. TKN
FAUCET_REFILL_FACTOR Send factor times credit amount on refilling. Defauls to 8.
FAUCET_REFILL_THRESHOLD Refill when balance gets below factor times credit amount.
Defaults to 20.
FAUCET_COOLDOWN_TIME Time (in seconds) after which an address can request
more tokens. Can be set to "0". Defaults to 24 hours
if unset or an empty string.
`.trim();
process.stdout.write(`${out}\n`);

View File

@ -67,11 +67,11 @@ export class Webserver {
const entry = this.addressCounter.get(address);
if (entry !== undefined) {
const cooldownMs = constants.cooldown * 3600 * 1000;
if (entry.getTime() + cooldownMs > Date.now()) {
const cooldownTimeMs = constants.cooldownTime * 1000;
if (entry.getTime() + cooldownTimeMs > Date.now()) {
throw new HttpError(
405,
`Too many request for the same address. Blocked to prevent draining. Please wait ${constants.cooldown}h and try it again!`,
`Too many request for the same address. Blocked to prevent draining. Please wait ${constants.cooldownTime} seconds and try it again!`,
);
}
}

View File

@ -17,4 +17,12 @@ export const pathPattern = process.env.FAUCET_PATH_PATTERN || "m/44'/118'/0'/0/a
export const tokenConfig: TokenConfiguration = {
bankTokens: parseBankTokens(process.env.FAUCET_TOKENS || "ucosm, ustake"),
};
export const cooldown = 24; // hours
/**
* Cooldown time in seconds.
*
* Defaults to 24 hours if FAUCET_COOLDOWN_TIME unset or an empty string.
* FAUCET_COOLDOWN_TIME can be set to "0" to deactivate.
*/
export const cooldownTime = process.env.FAUCET_COOLDOWN_TIME
? Number.parseInt(process.env.FAUCET_COOLDOWN_TIME, 10)
: 24 * 3600;