-
Notifications
You must be signed in to change notification settings - Fork 13
/
mint.ts
131 lines (112 loc) · 3.55 KB
/
mint.ts
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
import { computeHtml } from "@/utils/compute-html";
import { Warpcast } from "../../classes/Warpcast";
import { NextApiRequest, NextApiResponse } from "next";
import NextCors from "nextjs-cors";
import { z } from "zod";
import { ThirdWebEngine } from "@/classes/ThirdWebEngine";
const requestBodyWarpcastSchema = z.object({
trustedData: z.object({
messageBytes: z.string().min(5),
}),
});
const requestQuerySchema = z.object({
// "start" = will display Re-cast to mint & Like to mint
// "recast" = will attempt to see if you have recasted and liked (you can change to 'recast-like' if you want)
// "mint" = will be the actual mint process and display a congratulation html view
type: z.union([z.literal("start"), z.literal("recast"), z.literal("mint")]),
});
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await NextCors(req, res, {
methods: ["GET", "POST"],
origin: "*",
});
if (req.method !== "POST") {
return res.status(400).send({ error: "invalid method" });
}
try {
const { type } = requestQuerySchema.parse(req.query);
const { trustedData } = requestBodyWarpcastSchema.parse(req.body);
const action = await Warpcast.validateMessage(trustedData.messageBytes);
if (type === "start") {
const isNFTOwned = await ThirdWebEngine.isNFTOwned(
action.interactor.custody_address
);
if (isNFTOwned) {
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "start",
content: "You already own the NFT",
})
);
}
const isBalanceLow = await ThirdWebEngine.isBalanceLow();
if (isBalanceLow) {
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "start",
content: "Sorry, we're out of gas!",
})
);
}
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "recast",
content: "Like & recast to mint",
})
);
}
if (type === "recast") {
const hasRecasted = await Warpcast.hasRecasted(action.interactor.fid);
if (!hasRecasted) {
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "recast",
content: "Like is required to mint the NFT",
})
);
}
const hasLiked = await Warpcast.hasLiked(action.interactor.fid);
if (!hasLiked) {
return res.status(200).send(
computeHtml({
imagePath: "/farcaster.png",
postType: "recast",
content: "Like is required to mint the NFT",
})
);
}
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "mint",
content: "Mint",
})
);
}
if (type === "mint") {
await ThirdWebEngine.mint(action.interactor.custody_address);
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "start", // Do your own custom post_url after user has minted the NFT + clicks your button
content: "Congrats! The NFT was sent to your wallet",
})
);
}
} catch (err) {
return res.status(200).send(
computeHtml({
imagePath: "<next_js_image_path>",
postType: "start",
content: "Something went wrong",
})
);
}
}