diff --git a/apps/api/src/routes/gas-sponsorship.ts b/apps/api/src/routes/gas-sponsorship.ts index c2f84fd2..2cfe4f75 100644 --- a/apps/api/src/routes/gas-sponsorship.ts +++ b/apps/api/src/routes/gas-sponsorship.ts @@ -10,8 +10,59 @@ import { } from "../schema/gas-sponsorship"; import type { TdkApiContext } from "../types"; -// TODO: Replace with actual sponsored partner IDs or logic to fetch them from another service like TMC -const fullySponsoredPatnerIds = new Set(["zeeverse", "smols"]); +// TODO: tweak data here based on final resolutions. +// TODO: move this to the DB so it can be updated without code changes (through some sort of dashboard) +const sponsorshipRules = { + partner: { + fullySponsoredIds: ["zeeverse", "smols"], + }, + user: { + maxTransactionsPerMonth: 100, + maxGasPerMonth: 0, + }, +}; + +const validateRules = ({ + partnerId, + monthlyTransactions, + monthlyGas, +}: { + partnerId: string; + monthlyTransactions: number; + monthlyGas: number; +}) => { + const { partner, user } = sponsorshipRules; + + const isPartnerFullySponsored = partner.fullySponsoredIds.includes(partnerId); + const hasExceededTransactions = + monthlyTransactions > user.maxTransactionsPerMonth; + const hasExceededGas = monthlyGas > user.maxGasPerMonth; + + let reason = "does not meet the criteria"; + + switch (true) { + case isPartnerFullySponsored: + reason = "partner is fully sponsored"; + break; + case hasExceededTransactions: + reason = "exceeded the maximum number of transactions per month"; + break; + case hasExceededGas: + reason = "exceeded the maximum gas per month"; + break; + + default: + break; + } + + const isAllowed = + isPartnerFullySponsored || !hasExceededTransactions || !hasExceededGas; + + return { + isAllowed, + reason, + }; +}; export const gasSponsorshipRoutes = ({ db }: TdkApiContext): FastifyPluginAsync => @@ -33,37 +84,22 @@ export const gasSponsorshipRoutes = async (req, reply) => { const { body, params } = req; - const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000); + const oneMonthAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); const last24hTransactions = await db.transaction.count({ where: { fromAddress: body.userOp.sender, blockTimestamp: { - gte: yesterday, + gte: oneMonthAgo, }, }, }); - const [isPartnerFullySponsored, hasLessThan10Transactions] = [ - fullySponsoredPatnerIds.has(params.partnerId), - last24hTransactions < 10, - ]; - - let reason = "does not meet the criteria"; - - switch (true) { - case isPartnerFullySponsored: - reason = "partner is fully sponsored"; - break; - case hasLessThan10Transactions: - reason = "less than 10 transactions in the last 24 hours"; - break; - - default: - break; - } - - const isAllowed = isPartnerFullySponsored || hasLessThan10Transactions; + const { isAllowed, reason } = validateRules({ + partnerId: params.partnerId, + monthlyTransactions: last24hTransactions, + monthlyGas: 1, // TODO: calculate the total gas used in the last 24 hours + }); reply.send({ isAllowed,