-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gas-sponsorship route with basic validations
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { FastifyPluginAsync } from "fastify"; | ||
|
||
import type { ErrorReply } from "../schema"; | ||
import { | ||
type ValidateBody, | ||
type ValidateParams, | ||
type ValidateReply, | ||
validateBodySchema, | ||
validateReplySchema, | ||
} 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"]); | ||
|
||
export const gasSponsorshipRoutes = | ||
({ db }: TdkApiContext): FastifyPluginAsync => | ||
async (app) => { | ||
app.post<{ | ||
Params: ValidateParams; | ||
Body: ValidateBody; | ||
Reply: ValidateReply | ErrorReply; | ||
}>( | ||
"/gas-sponsorship/:partnerId/validate", | ||
{ | ||
schema: { | ||
body: validateBodySchema, | ||
response: { | ||
200: validateReplySchema, | ||
}, | ||
}, | ||
}, | ||
async (req, reply) => { | ||
const { body, params } = req; | ||
|
||
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000); | ||
|
||
const last24hTransactions = await db.transaction.count({ | ||
where: { | ||
fromAddress: body.userOp.sender, | ||
blockTimestamp: { | ||
gte: yesterday, | ||
}, | ||
}, | ||
}); | ||
|
||
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; | ||
|
||
reply.send({ | ||
isAllowed, | ||
reason, | ||
}); | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { type Static, Type } from "@sinclair/typebox"; | ||
|
||
export const validateBodySchema = Type.Object({ | ||
clientId: Type.String(), | ||
chainId: Type.Number(), | ||
userOp: Type.Object({ | ||
sender: Type.String(), | ||
targets: Type.Array(Type.String()), | ||
gasLimit: Type.String(), | ||
gasPrice: Type.String(), | ||
data: Type.Object({ | ||
targets: Type.Array(Type.String()), | ||
callDatas: Type.Array(Type.String()), | ||
values: Type.Array(Type.String()), | ||
}), | ||
}), | ||
}); | ||
|
||
export const validateReplySchema = Type.Object({ | ||
isAllowed: Type.Boolean(), | ||
reason: Type.Optional(Type.String()), | ||
}); | ||
|
||
const validateParamsSchema = Type.Object({ | ||
partnerId: Type.String(), | ||
}); | ||
|
||
export type ValidateParams = Static<typeof validateParamsSchema>; | ||
export type ValidateReply = Static<typeof validateReplySchema>; | ||
export type ValidateBody = Static<typeof validateBodySchema>; |