generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Auth signed writes #18
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2f7d5c
Admin page: skeleton + read data
carletex 63ba761
Admin page actions (approve / reject) + EIP712
carletex 2c0b5cb
Revert back to OP
carletex 8a9d0eb
Admin page: proposed + submitted grants
carletex 0acf9ad
Only admins can review grants
carletex 4f26782
Save connected user data to global store
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
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,102 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { useAccount, useSignTypedData } from "wagmi"; | ||
import { GrantData } from "~~/services/database/schema"; | ||
import { EIP_712_DOMAIN, EIP_712_TYPES__REVIEW_GRANT } from "~~/utils/eip712"; | ||
import { PROPOSAL_STATUS, ProposalStatusType } from "~~/utils/grants"; | ||
import { notification } from "~~/utils/scaffold-eth"; | ||
|
||
// ToDo. "Protect" with address header or PROTECT with signing the read. | ||
// ToDo. Submitted grants | ||
// ToDo. Loading states (initial, actions, etc) | ||
// ToDo. Refresh list after action | ||
const AdminPage = () => { | ||
const { address } = useAccount(); | ||
const [grants, setGrants] = useState<GrantData[]>([]); | ||
const { signTypedDataAsync } = useSignTypedData(); | ||
|
||
useEffect(() => { | ||
const getGrants = async () => { | ||
try { | ||
const response = await fetch("/api/grants/review"); | ||
const grants: GrantData[] = (await response.json()).data; | ||
setGrants(grants); | ||
} catch (error) { | ||
notification.error("Error getting grants for review"); | ||
} | ||
}; | ||
|
||
getGrants(); | ||
}, []); | ||
|
||
const reviewGrant = async (grant: GrantData, action: ProposalStatusType) => { | ||
let signature; | ||
try { | ||
signature = await signTypedDataAsync({ | ||
domain: EIP_712_DOMAIN, | ||
types: EIP_712_TYPES__REVIEW_GRANT, | ||
primaryType: "Message", | ||
message: { | ||
grantId: grant.id, | ||
action: action, | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error("Error signing message", e); | ||
notification.error("Error signing message"); | ||
return; | ||
} | ||
|
||
try { | ||
await fetch(`/api/grants/${grant.id}/review`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ signature, signer: address, action }), | ||
}); | ||
notification.success(`Grant reviewed: ${action}`); | ||
} catch (error) { | ||
notification.error("Error reviewing grant"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="container mx-auto max-w-screen-md mt-12"> | ||
<h1 className="text-4xl font-bold">Admin page</h1> | ||
{grants && ( | ||
<> | ||
<h2 className="font-bold mt-8">All grants that need review:</h2> | ||
{grants.map(grant => ( | ||
<div key={grant.id} className="border p-4 my-4"> | ||
<h3 className="font-bold"> | ||
{grant.title} | ||
<span className="text-sm text-gray-500 ml-2">({grant.id})</span> | ||
</h3> | ||
<p>{grant.description}</p> | ||
{grant.status === PROPOSAL_STATUS.PROPOSED && ( | ||
<div className="mt-4"> | ||
<button | ||
className="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded" | ||
onClick={() => reviewGrant(grant, PROPOSAL_STATUS.APPROVED)} | ||
> | ||
Approve | ||
</button> | ||
<button | ||
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded ml-4" | ||
onClick={() => reviewGrant(grant, PROPOSAL_STATUS.REJECTED)} | ||
> | ||
Reject | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
))} | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AdminPage; |
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,35 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { recoverTypedDataAddress } from "viem"; | ||
import { reviewGrant } from "~~/services/database/grants"; | ||
import { EIP_712_DOMAIN, EIP_712_TYPES__REVIEW_GRANT } from "~~/utils/eip712"; | ||
|
||
export async function POST(req: NextRequest, { params }: { params: { grantId: string } }) { | ||
const { grantId } = params; | ||
const { signature, signer, action } = await req.json(); | ||
|
||
// Validate Signature | ||
const recoveredAddress = await recoverTypedDataAddress({ | ||
domain: EIP_712_DOMAIN, | ||
types: EIP_712_TYPES__REVIEW_GRANT, | ||
primaryType: "Message", | ||
message: { | ||
grantId: grantId, | ||
action: action, | ||
}, | ||
signature, | ||
}); | ||
|
||
if (recoveredAddress !== signer) { | ||
console.error("Signature error", recoveredAddress, signer); | ||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
try { | ||
await reviewGrant(grantId, action); | ||
} catch (error) { | ||
console.error("Error approving grant", error); | ||
return NextResponse.json({ error: "Error approving grant" }, { status: 500 }); | ||
} | ||
|
||
return NextResponse.json({ success: true }); | ||
} |
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,8 @@ | ||
import { NextResponse } from "next/server"; | ||
import { getAllGrantsForReview } from "~~/services/database/grants"; | ||
|
||
export async function GET() { | ||
const grants = await getAllGrantsForReview(); | ||
|
||
return NextResponse.json({ data: grants }); | ||
} |
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
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,13 @@ | ||
export const EIP_712_DOMAIN = { | ||
name: "BuidlGuidl Grants", | ||
version: "1", | ||
chainId: 10, | ||
} as const; | ||
|
||
// ToDo. We could add more fields (grant title, builder, etc) | ||
export const EIP_712_TYPES__REVIEW_GRANT = { | ||
Message: [ | ||
{ name: "grantId", type: "string" }, | ||
{ name: "action", type: "string" }, | ||
], | ||
} as const; |
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,9 @@ | ||
export const PROPOSAL_STATUS = { | ||
PROPOSED: "proposed", | ||
APPROVED: "approved", | ||
SUBMITTED: "submitted", | ||
COMPLETED: "completed", | ||
REJECTED: "rejected", | ||
} as const; | ||
|
||
export type ProposalStatusType = (typeof PROPOSAL_STATUS)[keyof typeof PROPOSAL_STATUS]; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use something like
swr
? or even tanstack react-query (it might be a bit over powered) because they come in with this built in and also some extra goodies like caching etcThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, happy to try
swr
!This app won't be super intensive on requests, just a couple of forms + /admin... but we can explore if it's worth it.
Maybe in another PR?