Skip to content

Commit

Permalink
feat: use rules json for gas-sponsorship
Browse files Browse the repository at this point in the history
  • Loading branch information
cdotta committed Oct 24, 2024
1 parent e15aafd commit aae1a13
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions apps/api/src/routes/gas-sponsorship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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,
Expand Down

0 comments on commit aae1a13

Please sign in to comment.