Skip to content

Commit

Permalink
feat: implement contract registries
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Oct 3, 2024
1 parent d78fb04 commit 9210c61
Show file tree
Hide file tree
Showing 63 changed files with 1,396 additions and 755 deletions.
3 changes: 3 additions & 0 deletions packages/contracts/contracts/interfaces/IRegistryManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface IRegistryManager {
address indexed registry,
RequestType indexed requestType,
bytes32 indexed recipient,
uint256 recipientIndex,
uint256 index,
address payout,
string metadataUrl
Expand All @@ -47,6 +48,7 @@ interface IRegistryManager {
address indexed registry,
RequestType indexed requestType,
bytes32 indexed recipient,
uint256 recipientIndex,
uint256 index,
address payout,
string metadataUrl
Expand All @@ -55,6 +57,7 @@ interface IRegistryManager {
address indexed registry,
RequestType indexed requestType,
bytes32 indexed recipient,
uint256 recipientIndex,
uint256 index,
address payout,
string metadataUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
/// @inheritdoc IRegistryManager
function process(Request memory request) public virtual override isValidRequest(request) {
request.status = Status.Pending;
requests[requestCount] = request;
uint256 index = requestCount;

unchecked {
requestCount++;
}

requests[index] = request;

emit RequestSent(
request.registry,
request.requestType,
request.recipient.id,
request.index,
index,
request.recipient.recipient,
request.recipient.metadataUrl
);
Expand All @@ -85,6 +88,7 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
request.requestType,
request.recipient.id,
request.index,
index,
request.recipient.recipient,
request.recipient.metadataUrl
);
Expand All @@ -108,6 +112,7 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
request.requestType,
request.recipient.id,
request.index,
index,
request.recipient.recipient,
request.recipient.metadataUrl
);
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "hardhat-contract-sizer";
import "maci-contracts/tasks/deploy";
import "maci-contracts/tasks/runner/deployFull";
import "maci-contracts/tasks/runner/deployPoll";
import "maci-contracts/tasks/runner/merge";
import "maci-contracts/tasks/runner/prove";
import "maci-contracts/tasks/runner/verifyFull";
import "solidity-docgen";

Expand Down
8 changes: 7 additions & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@
"deploy:optimism-sepolia": "pnpm run deploy --network optimism_sepolia",
"deploy-poll:optimism-sepolia": "pnpm run deploy-poll --network optimism_sepolia",
"initPoll:optimism-sepolia": "pnpm run initPoll --network optimism_sepolia",
"verify:optimism-sepolia": "pnpm run verify --network optimism_sepolia"
"verify:optimism-sepolia": "pnpm run verify --network optimism_sepolia",
"merge": "hardhat merge",
"merge:localhost": "pnpm run merge",
"merge:optimism-sepolia": "pnpm run merge --network optimism_sepolia",
"prove": "hardhat prove",
"prove:localhost": "pnpm run prove",
"prove:optimism-sepolia": "pnpm run prove --network optimism_sepolia"
},
"dependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.8",
Expand Down
10 changes: 7 additions & 3 deletions packages/contracts/tests/EASRegistryManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,25 @@ describe("EASRegistryManager", () => {
});

it("should allow owner to approve requests to the registry", async () => {
const addRequest = await registryManager.getRequest(0);
const requestIndex = 0;
const addRequest = await registryManager.getRequest(requestIndex);

expect(addRequest.status).to.equal(ERegistryManagerRequestStatus.Pending);

await expect(registryManager.connect(owner).approve(0))
await expect(registryManager.connect(owner).approve(requestIndex))
.to.emit(registryManager, "RequestApproved")
.withArgs(
addRequest.registry,
addRequest.requestType,
addRequest.recipient.id,
addRequest.index,
requestIndex,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);

const changeRequest = {
index: 0,
index: requestIndex,
registry: await mockRegistry.getAddress(),
requestType: ERegistryManagerRequestType.Change,
status: ERegistryManagerRequestStatus.Pending,
Expand All @@ -98,6 +100,7 @@ describe("EASRegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
1,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand All @@ -109,6 +112,7 @@ describe("EASRegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
1,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand Down
10 changes: 10 additions & 0 deletions packages/contracts/tests/RegistryManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe("RegistryManager", () => {
addRequest.requestType,
addRequest.recipient.id,
addRequest.index,
addRequest.index,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);
Expand Down Expand Up @@ -177,6 +178,7 @@ describe("RegistryManager", () => {
addRequest.requestType,
addRequest.recipient.id,
addRequest.index,
addRequest.index,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);
Expand All @@ -200,6 +202,7 @@ describe("RegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
1,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand All @@ -211,6 +214,7 @@ describe("RegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
1,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand Down Expand Up @@ -274,6 +278,7 @@ describe("RegistryManager", () => {
addRequest.requestType,
addRequest.recipient.id,
addRequest.index,
2,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);
Expand All @@ -285,6 +290,7 @@ describe("RegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
3,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand All @@ -296,6 +302,7 @@ describe("RegistryManager", () => {
addRequest.requestType,
addRequest.recipient.id,
addRequest.index,
2,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);
Expand All @@ -307,6 +314,7 @@ describe("RegistryManager", () => {
changeRequest.requestType,
changeRequest.recipient.id,
changeRequest.index,
3,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);
Expand Down Expand Up @@ -352,6 +360,7 @@ describe("RegistryManager", () => {
removeRequest.requestType,
removeRequest.recipient.id,
removeRequest.index,
4,
removeRequest.recipient.recipient,
removeRequest.recipient.metadataUrl,
);
Expand All @@ -365,6 +374,7 @@ describe("RegistryManager", () => {
removeRequest.requestType,
removeRequest.recipient.id,
removeRequest.index,
count - 1n,
removeRequest.recipient.recipient,
removeRequest.recipient.metadataUrl,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { ERegistryManagerRequestType, ERegistryManagerRequestStatus } from "./constants";

export * from "../typechain-types";
1 change: 1 addition & 0 deletions packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"lucide-react": "^0.316.0",
"maci-cli": "^2.4.0",
"maci-domainobjs": "^2.4.0",
"maci-platform-contracts": "workspace:*",
"next": "^14.1.0",
"next-auth": "^4.24.5",
"next-themes": "^0.2.1",
Expand Down
11 changes: 9 additions & 2 deletions packages/interface/src/components/AddedProjects.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { zeroAddress } from "viem";
import { useAccount } from "wagmi";

import { useBallot } from "~/contexts/Ballot";
import { useMaci } from "~/contexts/Maci";
import { useProjectCount } from "~/features/projects/hooks/useProjects";

export const AddedProjects = (): JSX.Element => {
const { ballot } = useBallot();
const { pollData } = useMaci();
const { chain } = useAccount();
const { data: projectCount } = useProjectCount({ registryAddress: pollData?.registry ?? zeroAddress, chain: chain! });

const allocations = ballot.votes;
const { data: projectCount } = useProjectCount();

return (
<div className="border-b border-gray-200 py-2">
Expand All @@ -20,7 +27,7 @@ export const AddedProjects = (): JSX.Element => {
</span>

<span className="text-gray-300">
<b>{projectCount?.count}</b>
<b>{projectCount?.count.toString()}</b>
</span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/components/EmptyState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type PropsWithChildren } from "react";
import { Heading } from "./ui/Heading";

export const EmptyState = ({ title, children = null }: PropsWithChildren<{ title: string }>): JSX.Element => (
<div className="flex flex-col items-center justify-center rounded border p-8">
<div className="m-2 flex flex-col items-center justify-center rounded border p-8">
<Heading as="h3" className="mt-0" size="lg">
{title}
</Heading>
Expand Down
10 changes: 6 additions & 4 deletions packages/interface/src/contexts/Ballot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useMaci } from "./Maci";

export const BallotContext = createContext<BallotContextType | undefined>(undefined);

// the default ballot is an empty ballot
const defaultBallot = { votes: [], published: false, edited: false };

export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: BallotProviderProps) => {
Expand All @@ -28,7 +29,8 @@ export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: Ball
[pollData],
);

const ballotContains = useCallback((id: string) => ballot.votes.find((v) => v.projectId === id), [ballot]);
// check if the ballot contains a specific project based on its index
const ballotContains = useCallback((index: number) => ballot.votes.find((v) => v.projectIndex === index), [ballot]);

const toObject = useCallback(
(key: string, arr: object[] = []) => arr.reduce((acc, x) => ({ ...acc, [x[key as keyof typeof acc]]: x }), {}),
Expand All @@ -55,8 +57,8 @@ export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: Ball

// remove certain project from the ballot
const removeFromBallot = useCallback(
(projectId: string) => {
const votes = ballot.votes.filter((v) => v.projectId !== projectId);
(projectIndex: number) => {
const votes = ballot.votes.filter((v) => v.projectIndex !== projectIndex);

setBallot({ ...ballot, votes });
},
Expand Down Expand Up @@ -86,7 +88,7 @@ export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: Ball
setBallot({ ...ballot, published: true, edited: false });
}, [ballot, setBallot]);

/// Read existing ballot in localStorage
// Read existing ballot in localStorage
useEffect(() => {
const savedBallot = JSON.parse(
localStorage.getItem("ballot") ?? JSON.stringify(defaultBallot),
Expand Down
Loading

0 comments on commit 9210c61

Please sign in to comment.