Skip to content

Commit

Permalink
feat(subgraph): add subgraph support
Browse files Browse the repository at this point in the history
- [x] Use the same subgraph from maci
- [x] Add registry related entities
- [x] Update events for contracts
  • Loading branch information
0xmad committed Sep 6, 2024
1 parent a5b3609 commit c4340d6
Show file tree
Hide file tree
Showing 40 changed files with 1,808 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ zkeys
public/mockServiceWorker.js
playwright-report/
test-results/
packages/subgraph/templates/subgraph.template.yaml
packages/subgraph/subgraph.yaml
packages/subgraph/generated/

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ interface IRecipientRegistry {
}

/// @notice Events
event RecipientAdded(uint256 indexed index, bytes32 id, bytes32 indexed metadataUrl, address indexed recipient);
event RecipientRemoved(uint256 indexed index, bytes32 id, address indexed recipient);
event RecipientChanged(uint256 indexed index, bytes32 id, bytes32 indexed metadataUrl, address indexed newAddress);
event RecipientAdded(uint256 indexed index, bytes32 id, bytes32 indexed metadataUrl, address indexed payout);
event RecipientRemoved(uint256 indexed index, bytes32 id, address indexed payout);
event RecipientChanged(uint256 indexed index, bytes32 id, bytes32 indexed metadataUrl, address indexed newPayout);

/// @notice Custom errors
error MaxRecipientsReached();
Expand Down
12 changes: 6 additions & 6 deletions packages/contracts/contracts/interfaces/IRegistryManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ interface IRegistryManager {
event RequestSent(
address indexed registry,
RequestType indexed requestType,
address indexed recipient,
bytes32 indexed recipient,
uint256 index,
bytes32 id,
address payout,
bytes32 metadataUrl
);
event RequestApproved(
address indexed registry,
RequestType indexed requestType,
address indexed recipient,
bytes32 indexed recipient,
uint256 index,
bytes32 id,
address payout,
bytes32 metadataUrl
);
event RequestRejected(
address indexed registry,
RequestType indexed requestType,
address indexed recipient,
bytes32 indexed recipient,
uint256 index,
bytes32 id,
address payout,
bytes32 metadataUrl
);

Expand Down
14 changes: 12 additions & 2 deletions packages/contracts/contracts/maci/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,26 @@ contract MACI is Ownable, BaseMACI, ICommon {

/// @notice Initialize the poll by given poll id and transfer poll ownership to the caller.
/// @param pollId The poll id
function initPoll(uint256 pollId, address registryAddress) public onlyOwner {
function initPoll(uint256 pollId) public onlyOwner {
PollContracts memory pollAddresses = polls[pollId];
IPoll poll = IPoll(pollAddresses.poll);

poll.setRegistry(registryAddress);
poll.init();
poll.transferOwnership(msg.sender);
}

/// @notice Set RegistryManager for MACI
/// @param pollId The poll id
/// @param registryAddress The registry address
function setPollRegistry(uint256 pollId, address registryAddress) public onlyOwner {
PollContracts memory pollAddresses = polls[pollId];
IPoll poll = IPoll(pollAddresses.poll);

poll.setRegistry(registryAddress);
}

/// @notice Set RegistryManager for MACI
/// @param registryManagerAddress Registry manager address
function setRegistryManager(address registryManagerAddress) public onlyOwner {
if (registryManagerAddress == address(0)) {
revert InvalidAddress();
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/contracts/maci/PollFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pragma solidity ^0.8.20;

import { PollFactory as BasePollFactory } from "maci-contracts/contracts/PollFactory.sol";
import { IMACI } from "maci-contracts/contracts/interfaces/IMACI.sol";
import { AccQueue } from "maci-contracts/contracts/trees/AccQueue.sol";
import { AccQueueQuinaryMaci } from "maci-contracts/contracts/trees/AccQueueQuinaryMaci.sol";

import { Poll } from "./Poll.sol";

/// @title PollFactory
Expand All @@ -20,7 +20,7 @@ contract PollFactory is BasePollFactory {
uint256 emptyBallotRoot
) public virtual override returns (address pollAddr) {
/// @notice deploy a new AccQueue contract to store messages
AccQueue messageAq = new AccQueueQuinaryMaci(treeDepths.messageTreeSubDepth);
AccQueueQuinaryMaci messageAq = new AccQueueQuinaryMaci(treeDepths.messageTreeSubDepth);

/// @notice the smart contracts that a Poll would interact with
ExtContracts memory extContracts = ExtContracts({ maci: IMACI(maci), messageAq: messageAq });
Expand Down
12 changes: 6 additions & 6 deletions packages/contracts/contracts/registryManager/RegistryManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
emit RequestSent(
request.registry,
request.requestType,
request.recipient.recipient,
request.index,
request.recipient.id,
request.index,
request.recipient.recipient,
request.recipient.metadataUrl
);
}
Expand All @@ -83,9 +83,9 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
emit RequestApproved(
request.registry,
request.requestType,
request.recipient.recipient,
request.index,
request.recipient.id,
request.index,
request.recipient.recipient,
request.recipient.metadataUrl
);

Expand All @@ -106,9 +106,9 @@ contract RegistryManager is Ownable, IRegistryManager, ICommon {
emit RequestRejected(
request.registry,
request.requestType,
request.recipient.recipient,
request.index,
request.recipient.id,
request.index,
request.recipient.recipient,
request.recipient.metadataUrl
);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@
"test": "hardhat test --network hardhat",
"deploy": "hardhat deploy-full",
"initPoll": "hardhat initPoll",
"verify": "hardhat verify-full",
"deploy-poll": "hardhat deploy-poll",
"deploy:localhost": "pnpm run deploy",
"deploy-poll:localhost": "pnpm run deploy-poll",
"initPoll:localhost": "pnpm run initPoll"
"initPoll:localhost": "pnpm run initPoll",
"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"
},
"dependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.6",
Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/tasks/deploy/poll/01-poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ deployment.deployTask(EDeploySteps.Poll, "Deploy poll").then((task) =>
});
const extContracts = await pollContract.extContracts();

await maciContract
.setPollRegistry(pollId, await pollRegistry.getAddress())
.then((transaction) => transaction.wait());

const messageProcessorContract = await deployment.getContract({
name: EContracts.MessageProcessor,
address: messageProcessorContractAddress,
Expand Down
10 changes: 2 additions & 8 deletions packages/contracts/tasks/runner/initPoll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import { ZeroAddress } from "ethers";
import { task, types } from "hardhat/config";
import { ContractStorage, Deployment } from "maci-contracts";
import { Deployment } from "maci-contracts";

import { type MACI } from "../../typechain-types";
import { EContracts } from "../helpers/constants";
Expand All @@ -23,7 +23,6 @@ task("initPoll", "Initialize poll")
.addParam("poll", "The poll id", undefined, types.string)
.setAction(async ({ poll }: IInitPollParams, hre) => {
const deployment = Deployment.getInstance(hre);
const storage = ContractStorage.getInstance();
const { MACI__factory: MACIFactory } = await import("../../typechain-types");

deployment.setHre(hre);
Expand All @@ -32,11 +31,6 @@ task("initPoll", "Initialize poll")

const maciContract = await deployment.getContract<MACI>({ name: EContracts.MACI, abi: MACIFactory.abi });
const pollContracts = await maciContract.polls(poll);
const registryAddress = storage.mustGetAddress<keyof typeof EContracts>(
EContracts.EASRegistry,
hre.network.name,
`poll-${poll}`,
);

if (pollContracts.poll === ZeroAddress) {
throw new Error(`No poll ${poll} found`);
Expand All @@ -46,7 +40,7 @@ task("initPoll", "Initialize poll")

console.log("Start balance: ", Number(startBalance / 10n ** 12n) / 1e6);

const tx = await maciContract.initPoll(poll, registryAddress);
const tx = await maciContract.initPoll(poll);
const receipt = await tx.wait();

if (receipt?.status !== 1) {
Expand Down
12 changes: 6 additions & 6 deletions packages/contracts/tests/EASRegistryManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe("EASRegistryManager", () => {
.withArgs(
addRequest.registry,
addRequest.requestType,
addRequest.recipient.recipient,
addRequest.index,
addRequest.recipient.id,
addRequest.index,
addRequest.recipient.recipient,
addRequest.recipient.metadataUrl,
);

Expand All @@ -96,9 +96,9 @@ describe("EASRegistryManager", () => {
.withArgs(
changeRequest.registry,
changeRequest.requestType,
changeRequest.recipient.recipient,
changeRequest.index,
changeRequest.recipient.id,
changeRequest.index,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);

Expand All @@ -107,9 +107,9 @@ describe("EASRegistryManager", () => {
.withArgs(
changeRequest.registry,
changeRequest.requestType,
changeRequest.recipient.recipient,
changeRequest.index,
changeRequest.recipient.id,
changeRequest.index,
changeRequest.recipient.recipient,
changeRequest.recipient.metadataUrl,
);

Expand Down
26 changes: 15 additions & 11 deletions packages/contracts/tests/Maci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ describe("Maci", () => {
});

it("should fail if unauthorized user tries to init the poll", async () => {
await expect(maciContract.initPoll(pollId, await user.getAddress())).not.to.be.revertedWithCustomError(
pollContract,
"PollAlreadyInit",
);
await expect(maciContract.connect(user).initPoll(pollId, await user.getAddress())).to.be.revertedWithCustomError(
await expect(maciContract.initPoll(pollId)).not.to.be.revertedWithCustomError(pollContract, "PollAlreadyInit");
await expect(maciContract.connect(user).initPoll(pollId)).to.be.revertedWithCustomError(
pollContract,
"OwnableUnauthorizedAccount",
);
});

it("should fail if unauthorized user tries to set the poll registry", async () => {
await expect(
maciContract.connect(user).setPollRegistry(pollId, await user.getAddress()),
).to.be.revertedWithCustomError(maciContract, "OwnableUnauthorizedAccount");
});

it("should fail if try to set zero address as registry", async () => {
await expect(maciContract.initPoll(pollId, ZeroAddress)).not.to.be.revertedWithCustomError(
await expect(maciContract.setPollRegistry(pollId, ZeroAddress)).to.be.revertedWithCustomError(
pollContract,
"InvalidAddress",
);
Expand All @@ -111,11 +114,12 @@ describe("Maci", () => {

const registryAddress = await registry.getAddress();

await expect(maciContract.initPoll(pollId, registryAddress)).not.to.be.revertedWithCustomError(
pollContract,
"PollAlreadyInit",
);
await expect(maciContract.initPoll(pollId, registryAddress)).to.be.revertedWithCustomError(
const receipt = await maciContract.setPollRegistry(pollId, registryAddress).then((tx) => tx.wait());

expect(receipt?.status).to.equal(1);

await expect(maciContract.initPoll(pollId)).not.to.be.revertedWithCustomError(pollContract, "PollAlreadyInit");
await expect(maciContract.initPoll(pollId)).to.be.revertedWithCustomError(
pollContract,
"OwnableUnauthorizedAccount",
);
Expand Down
Loading

0 comments on commit c4340d6

Please sign in to comment.