generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use route handler for applying of grants (#20)
- Loading branch information
1 parent
7982fbc
commit ae8d7f2
Showing
7 changed files
with
96 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 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,54 @@ | ||
import { NextResponse } from "next/server"; | ||
import { recoverTypedDataAddress } from "viem"; | ||
import { createGrant } from "~~/services/database/grants"; | ||
import { findUserByAddress } from "~~/services/database/users"; | ||
import { EIP_712_DOMAIN, EIP_712_TYPES__APPLY_FOR_GRANT } from "~~/utils/eip712"; | ||
|
||
type ReqBody = { | ||
title?: string; | ||
description?: string; | ||
askAmount?: string; | ||
signature?: `0x${string}`; | ||
signer?: string; | ||
}; | ||
|
||
// TODO: We could also add extra validtion of nonce | ||
export async function POST(req: Request) { | ||
try { | ||
const { title, description, askAmount, signature, signer } = (await req.json()) as ReqBody; | ||
|
||
if (!title || !description || !askAmount || isNaN(Number(askAmount)) || !signature || !signer) { | ||
return NextResponse.json({ error: "Invalid form details submited" }, { status: 400 }); | ||
} | ||
|
||
// Verif if the builder is present | ||
const builder = await findUserByAddress(signer); | ||
if (!builder.exists) { | ||
return NextResponse.json({ error: "Only buidlguild builders can submit for grants" }, { status: 401 }); | ||
} | ||
|
||
const recoveredAddress = await recoverTypedDataAddress({ | ||
domain: EIP_712_DOMAIN, | ||
types: EIP_712_TYPES__APPLY_FOR_GRANT, | ||
primaryType: "Message", | ||
message: { title, description, askAmount }, | ||
signature: signature, | ||
}); | ||
|
||
if (recoveredAddress !== signer) { | ||
return NextResponse.json({ error: "Recovered address did not match signer" }, { status: 401 }); | ||
} | ||
|
||
const grant = await createGrant({ | ||
title: title, | ||
description: description, | ||
askAmount: Number(askAmount), | ||
builder: signer, | ||
}); | ||
|
||
return NextResponse.json({ grant }, { status: 201 }); | ||
} catch (e) { | ||
console.error(e); | ||
return NextResponse.json({ error: "Error processing form" }, { status: 500 }); | ||
} | ||
} |
This file contains 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
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains 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