Skip to content

Commit

Permalink
Add proposal that updates default prop params (see #127)
Browse files Browse the repository at this point in the history
  • Loading branch information
tensojka committed Aug 26, 2024
1 parent e79e3b0 commit 2ebdd62
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 49 deletions.
48 changes: 25 additions & 23 deletions frontend/src/components/NewProposalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { CONTRACT_ADDR } from "../lib/config";
import { useAccount, useContractWrite } from "@starknet-react/core";
//import CustomProposal from "./CustomProposal";
import Treasury from "./proposal-form/Treasury";
import Config from "./proposal-form/ConfigProposal";

const proposalTypes = ["airdrop", "signal vote", "AMM", "governance", "treasury"];
const proposalTypes = ["airdrop", "signal vote", "AMM", "governance", "treasury", "config"];

export default function NewProposalForm({
setIsModalOpen,
Expand Down Expand Up @@ -56,36 +57,37 @@ export default function NewProposalForm({
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"
}`}
className={`px-4 py-2 rounded flex-1 ${selectedType === type ? "bg-blue-500 text-white" : "bg-gray-200"
}`}
>
{type}
</button>
))}
</div>
{selectedType && selectedType !== "treasury" && (

{selectedType && selectedType !== "treasury" && selectedType !== "config" && (
<form onSubmit={submitProposal} className="w-full space-y-2">
<label htmlFor="payload" className="block">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)}
/>
<button
type="submit"
className="w-full p-2 bg-blue-500 text-white rounded-lg"
>
Submit
</button>
<label htmlFor="payload" className="block">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)}
/>
<button
type="submit"
className="w-full p-2 bg-blue-500 text-white rounded-lg"
>
Submit
</button>
</form>
)}

{selectedType === "treasury" && <Treasury setIsModalOpen={setIsModalOpen}/>}

{selectedType === "config" && <Config setIsModalOpen={setIsModalOpen} />}

{selectedType === "treasury" && <Treasury setIsModalOpen={setIsModalOpen} />}
</>
);
}
40 changes: 23 additions & 17 deletions frontend/src/components/Proposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import toast from "react-hot-toast";
import NewcommentCommentForm from "./NewProposalCommentForm";
import Comments from "./Comments";

// Convert the proposal type from number to string
const getProposalName = (type: number, payload: number) => {
const proposalTypes = {
0: "amm",
1: "governance",
2: "CARM token",
3: "merkle tree root",
4: "no-op/signal vote",
5: "custom",
};

const customProposalTypes = {
0: "treasury distribution",
1: "default proposal parameters"
};

return type === 5
? customProposalTypes[payload] || "unknown"
: proposalTypes[type] || "unknown";
};


export default function Proposal({
proposalId,
}: {
Expand All @@ -31,20 +53,6 @@ export default function Proposal({
retry: false
});

// Convert the proposal type from number to string
const proposal_type = {
0: "amm",
1: "governance",
2: "CARM token",
3: "merkle tree root",
4: "no-op/signal vote",
5: "custom"
};

const custom_proposal_type = {
0: "treasury distribution"
}

const { writeAsync: write_yes } = useContractWrite({
calls: [
{
Expand Down Expand Up @@ -114,9 +122,7 @@ export default function Proposal({
className="flex-grow font-bold hover:underline cursor-pointer"
onClick={() => setIsCommentModalOpen(true)}
>
{data.valueOf()["to_upgrade"] == 5
? custom_proposal_type[data.valueOf()["payload"]] || "unknown"
: proposal_type[data.valueOf()["to_upgrade"]]}
{getProposalName(data.valueOf()["to_upgrade"], data.valueOf()["payload"])}
</div>

</div>
Expand Down
91 changes: 91 additions & 0 deletions frontend/src/components/proposal-form/ConfigProposal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { useState } from "react";
import { toast } from 'react-hot-toast';
import { useContractWrite } from "@starknet-react/core";
import { CONTRACT_ADDR } from "../../lib/config";

interface ConfigProposalProps {
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
}

export const ConfigProposal: React.FC<ConfigProposalProps> = ({ setIsModalOpen }) => {
const [quorum, setQuorum] = useState("");
const [proposalVotingSeconds, setProposalVotingSeconds] = useState("");

const { writeAsync } = useContractWrite({
calls: [{
contractAddress: CONTRACT_ADDR,
entrypoint: "submit_custom_proposal",
calldata: [1, 2, quorum, proposalVotingSeconds],
}],
});

const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();

const quorumValue = parseInt(quorum);
if (isNaN(quorumValue) || quorumValue < 0 || quorumValue > 50) {
toast.error('Quorum must be a number between 0 and 50');
return;
}

const votingSeconds = parseInt(proposalVotingSeconds);
if (isNaN(votingSeconds) || votingSeconds <= 0) {
toast.error('Proposal voting seconds must be a positive number');
return;
}

if (votingSeconds <= 60 * 60) {
toast.error('Voting should be longer than 1 hour. Enter seconds');
return;
}

writeAsync()
.then(() => {
toast.success("Config proposal submitted");
setIsModalOpen(false);
})
.catch((e) => {
toast.error("Something went wrong");
console.error(e);
});
};

return (
<div>
<h2 className="text-xl font-bold mb-4">Default proposal configuration</h2>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="quorum" className="block mb-2">Quorum (%)</label>
<input
id="quorum"
className='border border-gray-300 rounded-lg p-2 w-full'
type="number"
min="0"
max="100"
value={quorum}
onChange={(e) => setQuorum(e.target.value)}
/>
</div>
<div>
<label htmlFor="proposalVotingSeconds" className="block mb-2">Proposal Voting Duration (seconds)</label>
<input
id="proposalVotingSeconds"
className='border border-gray-300 rounded-lg p-2 w-full'
type="number"
min="3600"
value={proposalVotingSeconds}
onChange={(e) => setProposalVotingSeconds(e.target.value)}
/>
</div>
<button
type="submit"
className="w-full p-2 mt-4 text-white bg-blue-500 rounded-lg"
>
Submit Config Proposal
</button>
</form>
</div>
);
};

export default ConfigProposal;
11 changes: 5 additions & 6 deletions frontend/src/components/proposal-form/Treasury.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { toast } from 'react-hot-toast';
import { CONTRACT_ADDR } from "../../lib/config";

const treasuryProposalTypes = ["distribution", "zklend", "nostra", "carmine"];
const treasuryProposalTypetoId = {"distribution": 0, "zklend": 1, "nostra": 2, "carmine": 3}
const treasuryProposalTypetoId = { "distribution": 0, "zklend": 2, "nostra": 3, "carmine": 4 }

export default function Treasury({
setIsModalOpen,
}: {
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
}) {
const [selectedType, setSelectedType] = useState<string | null>(null);
const { isConnected } = useAccount();
Expand All @@ -29,7 +29,7 @@ export default function Treasury({
}

const selectedTypeId = treasuryProposalTypetoId[selectedType];

if (!selectedType || newCalldata.length === 0) return [];
const calls = [{
contractAddress: CONTRACT_ADDR,
Expand Down Expand Up @@ -60,9 +60,8 @@ export default function Treasury({
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"
}`}
className={`px-4 py-2 rounded flex-1 ${selectedType === type ? "bg-blue-500 text-white" : "bg-gray-200"
}`}
>
{type}
</button>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/constants/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import config from "./config.json";
export const NETWORK = config.NETWORK as "mainnet" | "testnet";
export const API_URL = config.API_URL;
export const AMM_ADDRESS = config.AMM_ADDRESS;
export const GOVERNANCE_ADDRESS = config.GOVERNANCE_ADDRESS;
export const ETH_ADDRESS = config.ETH_ADDRESS;
export const USDC_ADDRESS = config.USDC_ADDRESS;
export const BTC_ADDRESS = config.BTC_ADDRESS;
Expand Down
1 change: 0 additions & 1 deletion frontend/src/constants/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"NETWORK": "sepolia",
"API_URL": "https://api.carmine.finance",
"AMM_ADDRESS": "0x047472e6755afc57ada9550b6a3ac93129cc4b5f98f51c73e0644d129fd208d9",
"GOVERNANCE_ADDRESS": "0x057dfabb5a506bfd1937062562a1adf45c7c4c62d0377ccfc59a0b42d7ab3212",
"TREASURY_ADDRESS": "0x4c990da03da72bdfb10db5c04e8aaa9d5404a07fe454037facb7744c132d42c",
"ETH_ADDRESS": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"USDC_ADDRESS": "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const CONTRACT_ADDR =
// mainnet: "0x001405ab78ab6ec90fba09e6116f373cda53b0ba557789a4578d8c1ec374ba0f";
"0x057dfabb5a506bfd1937062562a1adf45c7c4c62d0377ccfc59a0b42d7ab3212"; // sepolia with treasury
"0x02ba6e05d06a9e7398ae71c330b018415f93710c58e99fb04fa761f97712ec76"; // sepolia with treasury
export const TOKEN_CONTRACT =
"0x2b91dd683bc4bcae7a9c5d0cbeba0e8d62fa657742e4640f1e8163dc10e9bd7";

Expand Down

0 comments on commit 2ebdd62

Please sign in to comment.