Skip to content

Commit

Permalink
refactor(contracts): do not return contract addresses on poll deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Aug 5, 2024
1 parent e634b47 commit ad5496d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 61 deletions.
12 changes: 4 additions & 8 deletions cli/ts/commands/deployPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ export const deployPoll = async ({
const log = iface.parseLog(receiptLog as unknown as { topics: string[]; data: string }) as unknown as {
args: {
_pollId: bigint;
pollAddr: {
poll: string;
messageProcessor: string;
tally: string;
};
};
name: string;
};
Expand All @@ -141,9 +136,10 @@ export const deployPoll = async ({

// eslint-disable-next-line no-underscore-dangle
const pollId = log.args._pollId;
pollAddr = log.args.pollAddr.poll;
messageProcessorContractAddress = log.args.pollAddr.messageProcessor;
tallyContractAddress = log.args.pollAddr.tally;
const pollContracts = await maciContract.getPoll(pollId);
pollAddr = pollContracts.poll;
messageProcessorContractAddress = pollContracts.messageProcessor;
tallyContractAddress = pollContracts.tally;

logGreen(quiet, info(`Poll ID: ${pollId.toString()}`));
logGreen(quiet, info(`Poll contract: ${pollAddr}`));
Expand Down
8 changes: 3 additions & 5 deletions contracts/contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ contract MACI is IMACI, DomainObjs, Params, Utilities {
uint256 _pollId,
uint256 indexed _coordinatorPubKeyX,
uint256 indexed _coordinatorPubKeyY,
PollContracts pollAddr,
Mode _mode
);

Expand Down Expand Up @@ -174,15 +173,14 @@ contract MACI is IMACI, DomainObjs, Params, Utilities {
/// @param _verifier The Verifier Contract
/// @param _vkRegistry The VkRegistry Contract
/// @param _mode Voting mode
/// @return pollAddr a new Poll contract address
function deployPoll(
uint256 _duration,
TreeDepths memory _treeDepths,
PubKey memory _coordinatorPubKey,
address _verifier,
address _vkRegistry,
Mode _mode
) public virtual returns (PollContracts memory pollAddr) {
) public virtual {
// cache the poll to a local variable so we can increment it
uint256 pollId = nextPollId;

Expand Down Expand Up @@ -211,11 +209,11 @@ contract MACI is IMACI, DomainObjs, Params, Utilities {
address tally = tallyFactory.deploy(_verifier, _vkRegistry, p, mp, msg.sender, _mode);

// store the addresses in a struct so they can be returned
pollAddr = PollContracts({ poll: p, messageProcessor: mp, tally: tally });
PollContracts memory pollAddr = PollContracts({ poll: p, messageProcessor: mp, tally: tally });

polls[pollId] = pollAddr;

emit DeployPoll(pollId, _coordinatorPubKey.x, _coordinatorPubKey.y, pollAddr, _mode);
emit DeployPoll(pollId, _coordinatorPubKey.x, _coordinatorPubKey.y, _mode);
}

/// @inheritdoc IMACI
Expand Down
20 changes: 5 additions & 15 deletions contracts/tasks/deploy/poll/01-poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ deployment.deployTask("poll:deploy-poll", "Deploy poll").then((task) =>
const unserializedKey = PubKey.deserialize(coordinatorPubkey);
const mode = useQuadraticVoting ? EMode.QV : EMode.NON_QV;

const [pollContractAddress, messageProcessorContractAddress, tallyContractAddress] =
await maciContract.deployPoll.staticCall(
pollDuration,
{
intStateTreeDepth,
messageTreeSubDepth,
messageTreeDepth,
voteOptionTreeDepth,
},
unserializedKey.asContractParam(),
verifierContractAddress,
vkRegistryContractAddress,
mode,
);

const tx = await maciContract.deployPoll(
pollDuration,
{
Expand All @@ -84,6 +69,11 @@ deployment.deployTask("poll:deploy-poll", "Deploy poll").then((task) =>
throw new Error("Deploy poll transaction is failed");
}

const pollContracts = await maciContract.getPoll(pollId);
const pollContractAddress = pollContracts.poll;
const messageProcessorContractAddress = pollContracts.messageProcessor;
const tallyContractAddress = pollContracts.tally;

const pollContract = await deployment.getContract<Poll>({ name: EContracts.Poll, address: pollContractAddress });
const extContracts = await pollContract.extContracts();

Expand Down
36 changes: 10 additions & 26 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ export const genMaciStateFromContract = async (
const id = event.args._pollId;

const pubKey = new PubKey([BigInt(event.args._coordinatorPubKeyX), BigInt(event.args._coordinatorPubKeyY)]);
const pollAddr = event.args.pollAddr.poll;

actions.push({
type: "DeployPoll",
blockNumber: event.blockNumber,
transactionIndex: event.transactionIndex,
data: { pollId: id, pollAddr, pubKey },
maciContract.getPoll(id).then(({ poll }) => {
actions.push({
type: "DeployPoll",
blockNumber: event.blockNumber,
transactionIndex: event.transactionIndex,
data: { pollId: id, pollAddr: poll, pubKey },
});

foundPollIds.add(Number(id));
pollContractAddresses.set(BigInt(id), poll);
});

foundPollIds.add(Number(id));
pollContractAddresses.set(BigInt(id), pollAddr);
});

if (sleepAmount) {
Expand Down Expand Up @@ -140,12 +140,10 @@ export const genMaciStateFromContract = async (

const [
publishMessageLogs,
mergeMessageAqSubRootsLogs,
mergeMessageAqLogs,
// eslint-disable-next-line no-await-in-loop
] = await Promise.all([
pollContract.queryFilter(pollContract.filters.PublishMessage(), i, toBlock),
pollContract.queryFilter(pollContract.filters.MergeMessageAqSubRoots(), i, toBlock),
pollContract.queryFilter(pollContract.filters.MergeMessageAq(), i, toBlock),
]);

Expand All @@ -167,20 +165,6 @@ export const genMaciStateFromContract = async (
});
});

mergeMessageAqSubRootsLogs.forEach((event) => {
assert(!!event);

const numSrQueueOps = Number(event.args._numSrQueueOps);
actions.push({
type: "MergeMessageAqSubRoots",
blockNumber: event.blockNumber,
transactionIndex: event.transactionIndex,
data: {
numSrQueueOps,
},
});
});

mergeMessageAqLogs.forEach((event) => {
assert(!!event);

Expand Down
16 changes: 10 additions & 6 deletions subgraph/src/maci.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-underscore-dangle */
import { Address, BigInt as GraphBN } from "@graphprotocol/graph-ts";

import { DeployPoll as DeployPollEvent, SignUp as SignUpEvent } from "../generated/MACI/MACI";
import { DeployPoll as DeployPollEvent, SignUp as SignUpEvent, MACI as MaciContract } from "../generated/MACI/MACI";
import { Poll } from "../generated/schema";
import { Poll as PollTemplate } from "../generated/templates";
import { Poll as PollContract } from "../generated/templates/Poll/Poll";
Expand All @@ -12,14 +12,18 @@ import { createOrLoadMACI, createOrLoadUser, createOrLoadAccount } from "./utils
export function handleDeployPoll(event: DeployPollEvent): void {
const maci = createOrLoadMACI(event);

const poll = new Poll(event.params.pollAddr.poll);
const contract = PollContract.bind(event.params.pollAddr.poll);
const id = event.params._pollId;

const maciContract = MaciContract.bind(Address.fromBytes(maci.id));
const contracts = maciContract.getPoll(id);
const poll = new Poll(contracts.poll);
const contract = PollContract.bind(contracts.poll);
const treeDepths = contract.treeDepths();
const durations = contract.getDeployTimeAndDuration();

poll.pollId = event.params._pollId;
poll.messageProcessor = event.params.pollAddr.messageProcessor;
poll.tally = event.params.pollAddr.tally;
poll.pollId = id;
poll.messageProcessor = contracts.messageProcessor;
poll.tally = contracts.tally;
poll.maxMessages = GraphBN.fromI32(MESSAGE_TREE_ARITY ** treeDepths.getMessageTreeDepth());
poll.maxVoteOption = GraphBN.fromI32(MESSAGE_TREE_ARITY ** treeDepths.getVoteOptionTreeDepth());
poll.treeDepth = GraphBN.fromI32(treeDepths.value0);
Expand Down
2 changes: 1 addition & 1 deletion subgraph/templates/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dataSources:
- name: Poll
file: ./node_modules/maci-contracts/build/artifacts/contracts/Poll.sol/Poll.json
eventHandlers:
- event: DeployPoll(uint256,indexed uint256,indexed uint256,(address,address,address),uint8)
- event: DeployPoll(uint256,indexed uint256,indexed uint256,uint8)
handler: handleDeployPoll
- event: SignUp(uint256,indexed uint256,indexed uint256,uint256,uint256)
handler: handleSignUp
Expand Down

0 comments on commit ad5496d

Please sign in to comment.