import { Injectable } from '@nestjs/common'; import { RateLimitStore } from './ports/rate-limit.store'; /** Proposals from one address within the window. */ const LIMIT = 5; const WINDOW_MS = 15 * 60_000; /** * Keeps one address from flooding a project with proposals. Counted in * Redis, so the limit holds across every instance of the API. */ @Injectable() export class ProposalQuota { constructor(private readonly store: RateLimitStore) {} /** Counts one more proposal from `address`; false once it is over. */ async take(address: string): Promise { const count = await this.store.hit(`proposals:${address}`, WINDOW_MS); return count <= LIMIT; } }