diff --git a/CHANGELOG.md b/CHANGELOG.md index e2c1d216f3..15f2b40350 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/faucet/README.md b/packages/faucet/README.md index 66cab433c0..e983367c06 100644 --- a/packages/faucet/README.md +++ b/packages/faucet/README.md @@ -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 diff --git a/packages/faucet/src/actions/help.ts b/packages/faucet/src/actions/help.ts index e018375332..797929e780 100644 --- a/packages/faucet/src/actions/help.ts +++ b/packages/faucet/src/actions/help.ts @@ -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`); diff --git a/packages/faucet/src/api/webserver.ts b/packages/faucet/src/api/webserver.ts index e5c6fc65ef..1a9cad8e8b 100644 --- a/packages/faucet/src/api/webserver.ts +++ b/packages/faucet/src/api/webserver.ts @@ -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!`, ); } } diff --git a/packages/faucet/src/constants.ts b/packages/faucet/src/constants.ts index 40ca9d9f1b..8a63744deb 100644 --- a/packages/faucet/src/constants.ts +++ b/packages/faucet/src/constants.ts @@ -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;