-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
106 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 |
---|---|---|
@@ -1,86 +1,92 @@ | ||
import React, { useMemo } from "react"; | ||
import React, { useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { CONTRACT_ADDR } from "../lib/config"; | ||
import { useAccount, useContractWrite } from "@starknet-react/core"; | ||
import CustomProposal from "./CustomProposal"; | ||
|
||
const proposalTypes = ["airdrop", "signal vote", "AMM", "governance", "treasury"]; | ||
|
||
export default function NewProposalForm({ | ||
setIsModalOpen, | ||
}: { | ||
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>; | ||
}) { | ||
const { isConnected } = useAccount(); | ||
const [selectedType, setSelectedType] = useState<string | null>(null); | ||
const [payload, setPayload] = useState<string>(""); | ||
|
||
// State variables for the payload and to_upgrade | ||
const [payload, setPayload] = React.useState<string>(""); | ||
const [to_upgrade, setToUpgrade] = React.useState<string>("0"); | ||
|
||
// Create a call to submit a proposal | ||
const calls = useMemo(() => { | ||
const tx = { | ||
const calls = React.useMemo(() => { | ||
if (!selectedType) return []; | ||
const typeIndex = proposalTypes.indexOf(selectedType); | ||
return [{ | ||
contractAddress: CONTRACT_ADDR, | ||
entrypoint: "submit_proposal", | ||
calldata: [payload.toString(), to_upgrade.toString()], | ||
}; | ||
return [tx]; | ||
}, [payload, to_upgrade, submitProposal]); | ||
calldata: [payload, typeIndex.toString()], | ||
}]; | ||
}, [selectedType, payload]); | ||
|
||
// Use the useContractWrite hook to write the proposal | ||
const { writeAsync } = useContractWrite({ calls }); | ||
|
||
function submitProposal(e: React.FormEvent<HTMLFormElement>) { | ||
e.preventDefault(); | ||
|
||
// Check if the user is connected | ||
if (!isConnected) { | ||
toast.error("Please connect your wallet"); | ||
return; | ||
} | ||
|
||
// Check if the payload and to_upgrade fields are filled out | ||
if (!payload || !to_upgrade) { | ||
if (!selectedType || (selectedType !== "treasury" && !payload)) { | ||
toast.error("Please fill out all fields"); | ||
return; | ||
} | ||
|
||
// Call the write function to submit the proposal | ||
writeAsync() | ||
.then(() => { | ||
toast.success("Proposal submitted"); | ||
}) | ||
.then(() => toast.success("Proposal submitted")) | ||
.catch((e) => { | ||
toast.error("Something went wrong"); | ||
console.error(e); | ||
}) | ||
.finally(() => { | ||
setIsModalOpen(false); | ||
}); | ||
.finally(() => setIsModalOpen(false)); | ||
} | ||
|
||
return ( | ||
<form onSubmit={submitProposal}> | ||
<label htmlFor="#payload">Payload</label> | ||
<input | ||
id="#payload" | ||
type="text" | ||
placeholder="(integer or hex, e.g.: 1 or 0x1)" | ||
className="w-full p-2 mb-2 border rounded-lg border-slate-300" | ||
onChange={(e) => setPayload(e.target.value)} | ||
/> | ||
<label htmlFor="#to_upgrade">To Upgrade</label> | ||
<select | ||
id="#to_upgrade" | ||
className="w-full p-2 border rounded-lg border-slate-300" | ||
onChange={(e) => setToUpgrade(e.target.value)} | ||
defaultValue={to_upgrade} | ||
> | ||
<option value="0">amm</option> | ||
<option value="1">governance</option> | ||
<option value="2">CARM token</option> | ||
<option value="3">merkle tree root</option> | ||
<option value="4">no-op/signal vote</option> | ||
</select> | ||
<form onSubmit={submitProposal} className="space-y-4"> | ||
<div className="flex justify-between space-x-2"> | ||
{proposalTypes.map((type) => ( | ||
<button | ||
key={type} | ||
type="button" | ||
onClick={() => setSelectedType(type)} | ||
className={`px-4 py-2 rounded flex-1 ${ | ||
selectedType === type ? "bg-blue-500 text-white" : "bg-gray-200" | ||
}`} | ||
> | ||
{type} | ||
</button> | ||
))} | ||
</div> | ||
|
||
{selectedType && selectedType !== "treasury" && ( | ||
<div> | ||
<label htmlFor="payload" className="block mb-2">Payload</label> | ||
<input | ||
id="payload" | ||
type="text" | ||
placeholder="(integer or hex, e.g.: 1 or 0x1)" | ||
className="w-full p-2 border rounded-lg border-slate-300" | ||
value={payload} | ||
onChange={(e) => setPayload(e.target.value)} | ||
/> | ||
</div> | ||
)} | ||
|
||
{selectedType === "treasury" && <CustomProposal setIsModalOpen={setIsModalOpen}/>} | ||
|
||
{selectedType !== "treasury" && ( | ||
<button | ||
type="submit" | ||
className="w-full p-2 bg-blue-500 text-white rounded-lg" | ||
> | ||
Submit | ||
</button> | ||
)} | ||
</form> | ||
); | ||
} | ||
|
||
|
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,41 @@ | ||
import React from "react"; | ||
import NewProposalForm from "./NewProposalForm"; | ||
|
||
interface SubmitProposalModalProps { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
} | ||
|
||
const SubmitProposalModal: React.FC<SubmitProposalModalProps> = ({ isOpen, onClose }) => { | ||
if (!isOpen) return null; | ||
|
||
return ( | ||
<dialog className="fixed inset-0 z-50 flex items-center justify-center w-full h-full p-6 bg-black bg-opacity-50"> | ||
<div className="relative flex flex-col items-center gap-4 p-8 bg-white rounded-lg"> | ||
<button | ||
className="absolute right-3 top-3 text-slate-400" | ||
onClick={onClose} | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
className="w-6 h-6" | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
stroke="currentColor" | ||
> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth={2} | ||
d="M6 18L18 6M6 6l12 12" | ||
/> | ||
</svg> | ||
</button> | ||
<p className="text-xl font-bold">New proposal</p> | ||
<NewProposalForm setIsModalOpen={onClose} /> | ||
</div> | ||
</dialog> | ||
); | ||
}; | ||
|
||
export default SubmitProposalModal; |
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
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
Oops, something went wrong.