-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-answer.ts
113 lines (94 loc) · 2.66 KB
/
check-answer.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
import quizQuestions from "../../lib/questions";
import type { NextApiRequest, NextApiResponse } from "next";
import { ethers, BigNumber } from "ethers";
import { packAddress } from "../../lib/contractAddresses";
import { ThirdwebSDK } from "@3rdweb/sdk";
export type CheckAnswerPayload = {
questionIndex: number;
answerIndex: number;
message: string;
signedMessage: string;
};
type ErrorResponse = {
kind: "error";
error: string;
};
type IncorrectResponse = {
kind: "incorrect";
correctAnswerIndex: number;
};
type CorrectResponse = {
kind: "correct";
};
export type CheckAnswerResponse =
| ErrorResponse
| IncorrectResponse
| CorrectResponse;
export default async function Open(
req: NextApiRequest,
res: NextApiResponse<CheckAnswerResponse>
) {
// Validate the request body contains expected fields
if (!req.body.hasOwnProperty("questionIndex")) {
res.status(400).json({
kind: "error",
error: "No question index in request body",
});
return;
}
if (!req.body.hasOwnProperty("answerIndex")) {
res.status(400).json({
kind: "error",
error: "No answer index in request body",
});
return;
}
const body = req.body as CheckAnswerPayload;
//insert code
let address = ""
try {
address = ethers.utils.verifyMessage(body.message, body.signedMessage)
} catch (err) {
res.status(400).json({
kind: "error",
error: `Unable to verify message: ${err}`,
});
return;
}
// Validate the question index is valid
if (body.questionIndex >= quizQuestions.length) {
res.status(400).json({
kind: "error",
error: `Invalid question index ${body.questionIndex}`,
});
return;
}
const question = quizQuestions[body.questionIndex];
// Check the answer, return if incorrect
if (body.answerIndex !== question.correctAnswerIndex) {
res.status(200).json({
kind: "incorrect",
correctAnswerIndex: question.correctAnswerIndex as number,
});
return;
}
// If we get here then the answer was correct
// TODO: send the reward!
// Initialize the Thirdweb SDK using the private key that owns the wallet
const sdk = new ThirdwebSDK(
new ethers.Wallet(
process.env.WALLET_PRIVATE_KEY as string,
// Using Polygon Mumbai network
ethers.getDefaultProvider("https://winter-icy-sun.matic-testnet.quiknode.pro/f36aa318f8f806e4e15a58ab4a1b6cb9f9e9d9b9/")
),
);
// Transfer a copy of the pack to the user
console.log(`Transferring a pack to ${address}...`);
const packModule = sdk.getPackModule(packAddress);
const packTokenId = '0';
// Note that this is async
packModule.transfer(address, packTokenId, BigNumber.from(1));
res.status(200).json({
kind: 'correct',
});
}