-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathroute.ts
More file actions
132 lines (119 loc) · 3.52 KB
/
route.ts
File metadata and controls
132 lines (119 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { count, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { getAddress, isAddress } from "thirdweb";
import * as x402 from "thirdweb/x402";
import z from "zod";
import { db } from "@/db/client";
import { comments } from "@/db/schema";
import { chain } from "@/lib/constants";
import { serverClient } from "@/lib/thirdweb.server";
import { getDynamicPrice } from "../constants";
const addressSchema = z
.string()
.refine((val) => isAddress(val), {
message: "Invalid address",
})
.transform((val) => getAddress(val));
const commentSchema = z.object({
ownerAddress: addressSchema,
text: z
.string()
.transform((val) => val.trim())
.refine((val) => val.length > 0 && val.length <= 1000, {
message: "Comment must be between 1 and 1000 characters",
}),
});
export async function POST(request: Request) {
const data = await request.json();
const validatedData = commentSchema.safeParse(data);
if (!validatedData.success) {
return Response.json(
{ error: validatedData.error.message },
{ status: 400 },
);
}
const facilitator = x402.facilitator({
client: serverClient,
serverWalletAddress: process.env.SERVER_WALLET_ADDRESS || "",
});
const paymentData = request.headers.get("x-payment");
// Count existing comments for this owner
const [{ count: existingCommentsCount }] = await db
.select({ count: count() })
.from(comments)
.where(eq(comments.ownerAddress, validatedData.data.ownerAddress));
const result = await x402.settlePayment({
resourceUrl: "https://x402.chat/api/comment",
routeConfig: {
resource: "https://x402.chat/api/comment",
discoverable: true,
description: "Leave a comment on a wallet.",
inputSchema: {
bodyFields: {
ownerAddress: {
type: "string",
description: "The wallet address to post the comment to.",
required: true,
},
text: {
type: "string",
description: "The text of the comment.",
required: true,
maxLength: 1000,
},
},
},
outputSchema: {
commentId: {
type: "string",
description: "The ID of the comment.",
required: true,
},
},
},
method: "POST",
paymentData: paymentData,
payTo: validatedData.data.ownerAddress,
network: chain,
price: getDynamicPrice(existingCommentsCount),
facilitator: facilitator,
});
if (result.status !== 200) {
// Payment required
return Response.json(result.responseBody, {
status: result.status,
headers: result.responseHeaders,
});
}
const payerAddress = result.paymentReceipt.payer;
if (!payerAddress || !isAddress(payerAddress)) {
return Response.json(
{
error: "No valid payer address found in the payment receipt.",
},
{ status: 500 },
);
}
// actually create the comment and then revalidate the pages
const insertResult = await db
.insert(comments)
.values({
ownerAddress: validatedData.data.ownerAddress,
fromAddress: getAddress(payerAddress),
text: validatedData.data.text,
})
.returning();
if (!insertResult[0]) {
return Response.json(
{ error: "Failed to create comment" },
{ status: 500 },
);
}
// revalidate the pages
revalidatePath(`/${validatedData.data.ownerAddress}`);
revalidatePath("/");
return Response.json(
{ success: true, commentId: insertResult[0].id },
{ status: 200 },
);
}