Skip to content

Commit 88184f8

Browse files
committed
addressed review comments
1 parent 81cfd54 commit 88184f8

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

src/server/schemas/wallet-subscription.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
import { Type } from "@sinclair/typebox";
2-
import type { Prisma, Webhooks } from "@prisma/client";
2+
import type { WalletSubscriptions, Webhooks } from "@prisma/client";
33
import { AddressSchema } from "./address";
44
import {
55
WalletConditionsSchema,
66
validateConditions,
77
} from "../../shared/schemas/wallet-subscription-conditions";
88

9-
interface WalletSubscriptionWithWebhook {
10-
id: string;
11-
chainId: string;
12-
walletAddress: string;
13-
conditions: Prisma.JsonValue[];
14-
webhookId: number | null;
9+
type WalletSubscriptionWithWebhook = WalletSubscriptions & {
1510
webhook: Webhooks | null;
16-
createdAt: Date;
17-
updatedAt: Date;
18-
deletedAt: Date | null;
19-
}
11+
};
2012

2113
export const walletSubscriptionSchema = Type.Object({
2214
id: Type.String(),

src/shared/db/wallet-subscriptions/create-wallet-subscription.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export async function createWalletSubscription({
3434
}
3535
}
3636

37+
const existingSubscriptionsCount = await prisma.walletSubscriptions.count({});
38+
39+
if (existingSubscriptionsCount >= 1000) {
40+
throw new Error("Maximum number of wallet subscriptions reached");
41+
}
42+
3743
// Create a new subscription
3844
return await prisma.walletSubscriptions.create({
3945
data: {

src/shared/db/wallet-subscriptions/get-all-wallet-subscriptions.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { validateConditions } from "../../schemas/wallet-subscription-conditions";
22
import { prisma } from "../client";
33

4-
export async function getAllWalletSubscriptions({
5-
page,
6-
limit,
7-
}: {
8-
page: number;
9-
limit: number;
4+
export async function getAllWalletSubscriptions(args?: {
5+
page?: number;
6+
limit?: number;
107
}) {
8+
const { page, limit } = args || {};
119
const subscriptions = await prisma.walletSubscriptions.findMany({
1210
where: {
1311
deletedAt: null,
1412
},
1513
include: {
1614
webhook: true,
1715
},
18-
skip: (page - 1) * limit,
16+
skip: page && limit ? (page - 1) * limit : undefined,
1917
take: limit,
2018
orderBy: {
2119
updatedAt: "desc",

src/worker/tasks/wallet-subscription-worker.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ import { thirdwebClient } from "../../shared/utils/sdk";
1212
import { getWalletBalance } from "thirdweb/wallets";
1313
import type { Chain } from "thirdweb/chains";
1414
import type { WalletCondition } from "../../shared/schemas/wallet-subscription-conditions";
15-
import type { Webhooks } from "@prisma/client";
15+
import type { WalletSubscriptions, Webhooks } from "@prisma/client";
16+
import { prettifyError } from "../../shared/utils/error";
1617

17-
interface WalletSubscriptionWithWebhook {
18-
id: string;
19-
chainId: string;
20-
walletAddress: string;
18+
type WalletSubscriptionWithWebhook = WalletSubscriptions & {
2119
conditions: WalletCondition[];
22-
webhookId: number | null;
2320
webhook: Webhooks | null;
24-
createdAt: Date;
25-
updatedAt: Date;
26-
deletedAt: Date | null;
27-
}
21+
};
2822

2923
// Split array into chunks of specified size
3024
function chunk<T>(arr: T[], size: number): T[][] {
@@ -45,7 +39,7 @@ async function verifyCondition({
4539
condition: WalletCondition;
4640
walletAddress: string;
4741
chain: Chain;
48-
}): Promise<string | undefined> {
42+
}): Promise<string | null> {
4943
switch (condition.type) {
5044
case "token_balance_lt":
5145
case "token_balance_gt": {
@@ -67,12 +61,7 @@ async function verifyCondition({
6761
? currentBalance < threshold
6862
: currentBalance > threshold;
6963

70-
return isConditionMet ? currentBalance.toString() : undefined;
71-
}
72-
default: {
73-
// For TypeScript exhaustiveness check
74-
const _exhaustiveCheck: never = condition;
75-
return undefined;
64+
return isConditionMet ? currentBalance.toString() : null;
7665
}
7766
}
7867
}
@@ -112,7 +101,7 @@ async function processSubscriptions(
112101
}
113102
} catch (error) {
114103
// Log error but continue processing other subscriptions
115-
const message = error instanceof Error ? error.message : String(error);
104+
const message = prettifyError(error);
116105
logger({
117106
service: "worker",
118107
level: "error",
@@ -127,7 +116,8 @@ async function processSubscriptions(
127116
// Must be explicitly called for the worker to run on this host.
128117
export const initWalletSubscriptionWorker = async () => {
129118
const config = await getConfig();
130-
const cronPattern = config.walletSubscriptionsCronSchedule || "*/30 * * * * *"; // Default to every 30 seconds
119+
const cronPattern =
120+
config.walletSubscriptionsCronSchedule || "*/30 * * * * *"; // Default to every 30 seconds
131121

132122
logger({
133123
service: "worker",
@@ -152,10 +142,7 @@ export const initWalletSubscriptionWorker = async () => {
152142
*/
153143
const handler: Processor<string, void, string> = async (_job: Job<string>) => {
154144
// Get all active wallet subscriptions
155-
const subscriptions = await getAllWalletSubscriptions({
156-
page: 1,
157-
limit: 1000, // Process 1000 subscriptions at a time
158-
});
145+
const subscriptions = await getAllWalletSubscriptions();
159146
if (subscriptions.length === 0) {
160147
return;
161148
}

0 commit comments

Comments
 (0)