-
Notifications
You must be signed in to change notification settings - Fork 18
basic challenge submission #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cad69a5
configure basic schema
technophile-04 996a324
remove usersRelation for events
technophile-04 5455f36
basi submission flow
technophile-04 a6d6c80
make autograder result true
technophile-04 94b23fb
basic builders address page
technophile-04 1fa1490
push to builderProfile
technophile-04 5296451
add basic validation
technophile-04 566bec0
make the submit button float in challenge details page
technophile-04 cb795dc
use new syntax for adding constrain on useChallenge table
technophile-04 1aaf825
remove console and add todo submit event
technophile-04 df64ac4
add case-incensitve uniqness to table and repositires, remove from ap…
technophile-04 2ac7e1d
move eip-712 inside services dir
technophile-04 ad5a2b3
Move table to UserChallengesTable compoent
technophile-04 61334f8
use composite primary-key for userChallenges table
technophile-04 4521936
add router.refresh for revalidating the dynmaic builder profile route…
technophile-04 ed3d8e7
separte out hook logic in useSubmitChallenge hook
technophile-04 ce814ce
Add TODO
carletex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
packages/nextjs/app/api/challenges/[challengeId]/submit/route.ts
This file contains hidden or 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,92 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { createEvent } from "~~/services/database/repositories/events"; | ||
import { upsertUserChallenge } from "~~/services/database/repositories/userChallenges"; | ||
import { findUserByAddress } from "~~/services/database/repositories/users"; | ||
import { isValidEIP712ChallengeSubmitSignature } from "~~/services/eip712/challenge"; | ||
|
||
export type ChallengeSubmitPayload = { | ||
userAddress: string; | ||
frontendUrl: string; | ||
contractUrl: string; | ||
signature: `0x${string}`; | ||
}; | ||
|
||
export type AutogradingResult = { | ||
success: boolean; | ||
feedback: string; | ||
}; | ||
|
||
// TODO: Remove this and make request to actual autograder | ||
async function mockAutograding(contractUrl: string): Promise<AutogradingResult> { | ||
console.log("Mock autograding for contract:", contractUrl); | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
return { | ||
success: true, | ||
feedback: "All tests passed successfully! Great work!", | ||
}; | ||
} | ||
|
||
export async function POST(req: NextRequest, { params }: { params: { challengeId: string } }) { | ||
try { | ||
const challengeId = params.challengeId; | ||
const { userAddress, frontendUrl, contractUrl, signature } = (await req.json()) as ChallengeSubmitPayload; | ||
|
||
if (!userAddress || !frontendUrl || !contractUrl || !signature) { | ||
return NextResponse.json({ error: "Missing required fields" }, { status: 400 }); | ||
} | ||
|
||
const isValidSignature = await isValidEIP712ChallengeSubmitSignature({ | ||
address: userAddress, | ||
signature, | ||
challengeId, | ||
frontendUrl, | ||
contractUrl, | ||
}); | ||
|
||
if (!isValidSignature) { | ||
return NextResponse.json({ error: "Invalid signature" }, { status: 401 }); | ||
} | ||
|
||
const user = await findUserByAddress(userAddress); | ||
if (user.length === 0) { | ||
return NextResponse.json({ error: "User not found" }, { status: 404 }); | ||
} | ||
|
||
// TODO: Create challenge submission only when autograder is turned on for that challenge | ||
/* await createEvent({ | ||
eventType: "challenge.submit", | ||
userAddress: lowerCasedUserAddress, | ||
challengeCode: challengeId, | ||
}); */ | ||
|
||
// TODO: Make request to actual autograder | ||
// TODO: Think if we want to wait the autograder to finish or just return the result immediately | ||
// - Check Vercel timeout limit and see if we return and have the function idle until the result is ready | ||
// An alternative is have and endpoint that receives the autograder result and update the database | ||
const gradingResult = await mockAutograding(contractUrl); | ||
|
||
await upsertUserChallenge({ | ||
userAddress: userAddress, | ||
challengeCode: challengeId, | ||
frontendUrl, | ||
contractUrl, | ||
reviewAction: gradingResult.success ? "ACCEPTED" : "REJECTED", | ||
reviewComment: gradingResult.feedback, | ||
}); | ||
|
||
await createEvent({ | ||
eventType: "challenge.autograde", | ||
userAddress: userAddress, | ||
challengeCode: challengeId, | ||
}); | ||
|
||
return NextResponse.json({ | ||
success: true, | ||
message: "Challenge submitted and graded successfully", | ||
autoGradingResult: gradingResult, | ||
}); | ||
} catch (error) { | ||
console.error("Error submitting challenge:", error); | ||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
import { UserChallengesTable } from "../_component/UserChallengesTable"; | ||
import { findUserChallengesByAddress } from "~~/services/database/repositories/userChallenges"; | ||
|
||
export default async function BuilderPage({ params }: { params: { address: string } }) { | ||
const { address: userAddress } = params; | ||
const challenges = await findUserChallengesByAddress(userAddress); | ||
|
||
return <UserChallengesTable challenges={challenges} />; | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/nextjs/app/builders/_component/UserChallengesTable.tsx
This file contains hidden or 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,72 @@ | ||
import { QuestionMarkCircleIcon } from "@heroicons/react/24/outline"; | ||
import { UserChallenges } from "~~/services/database/repositories/userChallenges"; | ||
|
||
export const UserChallengesTable = ({ challenges }: { challenges: UserChallenges }) => { | ||
return ( | ||
<div className="flex flex-col gap-8 py-8 px-4 lg:px-8"> | ||
<div> | ||
<h1 className="text-4xl font-bold mb-0">Challenges</h1> | ||
</div> | ||
<div className="w-full"> | ||
<table className="table table-zebra bg-base-100"> | ||
<thead> | ||
<tr className="text-sm"> | ||
<th className="bg-primary">NAME</th> | ||
<th className="bg-primary">CONTRACT</th> | ||
<th className="bg-primary">LIVE DEMO</th> | ||
<th className="bg-primary">SUBMITED AT</th> | ||
<th className="bg-primary">STATUS</th> | ||
<th className="bg-primary"></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{challenges.map(challenge => ( | ||
<tr key={challenge.challengeCode} className="hover"> | ||
<td>🏃♂️ Challenge {challenge.challengeCode}</td> | ||
<td> | ||
{challenge.contractUrl ? ( | ||
<a href={challenge.contractUrl} target="_blank" rel="noopener noreferrer" className="link"> | ||
Code | ||
</a> | ||
) : ( | ||
"-" | ||
)} | ||
</td> | ||
<td> | ||
{challenge.frontendUrl ? ( | ||
<a href={challenge.frontendUrl} target="_blank" rel="noopener noreferrer" className="link"> | ||
Demo | ||
</a> | ||
) : ( | ||
"-" | ||
)} | ||
</td> | ||
<td>{challenge.submittedTimestamp ? challenge.submittedTimestamp.toLocaleString() : "-"}</td> | ||
<td> | ||
<span | ||
className={`badge ${ | ||
challenge.reviewAction === "ACCEPTED" | ||
? "badge-success" | ||
: challenge.reviewAction === "REJECTED" | ||
? "badge-error" | ||
: "badge-warning" | ||
}`} | ||
> | ||
{challenge.reviewAction?.toLowerCase() || "pending"} | ||
</span> | ||
</td> | ||
<td> | ||
{challenge.reviewComment && ( | ||
<div className="tooltip" data-tip={challenge.reviewComment}> | ||
<QuestionMarkCircleIcon className="h-4 w-4 cursor-help" /> | ||
</div> | ||
)} | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
); | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/nextjs/app/challenge/[challengeId]/_components/SubmitChallengeButton.tsx
This file contains hidden or 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,29 @@ | ||
"use client"; | ||
|
||
import { useRef } from "react"; | ||
import { SubmitChallengeModal } from "./SubmitChallengeModal"; | ||
import { useAccount } from "wagmi"; | ||
import { useUser } from "~~/hooks/useUser"; | ||
|
||
export const SubmitChallengeButton = ({ challengeId }: { challengeId: string }) => { | ||
const submitChallengeModalRef = useRef<HTMLDialogElement>(null); | ||
const { address: connectedAddress } = useAccount(); | ||
|
||
const { data: user, isLoading: isLoadingUser } = useUser(connectedAddress); | ||
return ( | ||
<> | ||
<button | ||
className="btn btn-lg btn-primary mt-2 fixed bottom-8 inset-x-0 mx-auto w-fit" | ||
disabled={!user || isLoadingUser} | ||
onClick={() => submitChallengeModalRef && submitChallengeModalRef.current?.showModal()} | ||
> | ||
Submit | ||
</button> | ||
<SubmitChallengeModal | ||
challengeId={challengeId} | ||
ref={submitChallengeModalRef} | ||
closeModal={() => submitChallengeModalRef.current?.close()} | ||
/> | ||
</> | ||
); | ||
}; |
89 changes: 89 additions & 0 deletions
89
packages/nextjs/app/challenge/[challengeId]/_components/SubmitChallengeModal.tsx
This file contains hidden or 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,89 @@ | ||
import { forwardRef, useState } from "react"; | ||
import { QuestionMarkCircleIcon } from "@heroicons/react/24/outline"; | ||
import { InputBase } from "~~/components/scaffold-eth"; | ||
import { useSubmitChallenge } from "~~/hooks/useSubmitChallenge"; | ||
|
||
type SubmitChallengeModalProps = { | ||
challengeId: string; | ||
closeModal: () => void; | ||
}; | ||
|
||
export const SubmitChallengeModal = forwardRef<HTMLDialogElement, SubmitChallengeModalProps>( | ||
({ closeModal, challengeId }, ref) => { | ||
const [frontendUrl, setFrontendUrl] = useState(""); | ||
const [contractUrl, setContractUrl] = useState(""); | ||
|
||
const { submitChallenge, isPending } = useSubmitChallenge({ | ||
onSuccess: closeModal, | ||
}); | ||
|
||
return ( | ||
<dialog ref={ref} className="modal"> | ||
<div className="modal-box flex flex-col space-y-3"> | ||
<form method="dialog" className="bg-secondary -mx-6 -mt-6 px-6 py-4 flex items-center justify-between"> | ||
<div className="flex justify-between items-center"> | ||
<p className="font-bold text-xl m-0">Submit Challenge</p> | ||
</div> | ||
<button onClick={closeModal} className="btn btn-sm btn-circle btn-ghost text-xl h-auto"> | ||
✕ | ||
</button> | ||
</form> | ||
|
||
<h1 className="text-2xl font-semibold ml-2">{challengeId}</h1> | ||
|
||
<div className="flex flex-col space-y-5"> | ||
<div className="flex flex-col gap-1.5 w-full"> | ||
<div className="flex items-base ml-2"> | ||
<span className="text-sm font-medium mr-2 leading-none">Deployed URL</span> | ||
<div className="tooltip" data-tip="Your deployed challenge URL on vercel"> | ||
<QuestionMarkCircleIcon className="h-4 w-4" /> | ||
</div> | ||
</div> | ||
<InputBase | ||
placeholder="https://your-site.vercel.app" | ||
value={frontendUrl} | ||
onChange={e => setFrontendUrl(e)} | ||
/> | ||
</div> | ||
|
||
<div className="flex flex-col gap-1.5 w-full"> | ||
<div className="flex items-base ml-2"> | ||
<span className="text-sm font-medium mr-2 leading-none">Etherscan URL</span> | ||
<div className="tooltip" data-tip="Your verfied contract URL on etherscan"> | ||
<QuestionMarkCircleIcon className="h-4 w-4" /> | ||
</div> | ||
</div> | ||
<InputBase | ||
placeholder="https://sepolia.etherscan.io/address/**YourContractAddress**" | ||
value={contractUrl} | ||
onChange={e => setContractUrl(e)} | ||
/> | ||
</div> | ||
|
||
<div className="modal-action"> | ||
<button | ||
className="btn btn-primary self-center" | ||
disabled={!Boolean(frontendUrl && contractUrl) || isPending} | ||
onClick={() => submitChallenge({ challengeId, frontendUrl, contractUrl })} | ||
> | ||
{isPending ? ( | ||
<> | ||
<span className="loading loading-spinner loading-xs"></span> | ||
Submitting... | ||
</> | ||
) : ( | ||
"Submit Challenge" | ||
)} | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
<form method="dialog" className="modal-backdrop"> | ||
<button onClick={closeModal}>close</button> | ||
</form> | ||
</dialog> | ||
); | ||
}, | ||
); | ||
|
||
SubmitChallengeModal.displayName = "SubmitChallengeModal"; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.