Skip to content

Instantly share code, notes, and snippets.

@ctrlc03
Last active May 7, 2024 18:02
Show Gist options
  • Save ctrlc03/714bd5dc5cc21456396dd3742801c3cd to your computer and use it in GitHub Desktop.
Save ctrlc03/714bd5dc5cc21456396dd3742801c3cd to your computer and use it in GitHub Desktop.
import { Contract, MaxUint256, Provider, Signer } from "ethers";
import { MACI__factory, Poll__factory } from "../typechain-types";
import { assert } from "console";
import { Message, PubKey } from "maci-domainobjs";
import { getDefaultSigner } from "./utils";
import fs from "fs";
const clrFundAbi = JSON.parse(fs.readFileSync("./ts/fundingRoundAbi.json").toString()).abi;
const tokenAbi = JSON.parse(fs.readFileSync("./ts/tokenAbi.json").toString()).abi;
const userRegistryAbi = JSON.parse(fs.readFileSync("./ts/userRegistryAbi.json").toString()).abi;
const getSignups = async (
address: string,
clrFundAddress: string,
provider: Provider,
fromBlock = 0,
blocksPerRequest = 50,
endBlock: number | undefined = undefined,
) => {
const maciContract = MACI__factory.connect(address, provider);
const clrFundContract = new Contract(clrFundAddress, clrFundAbi, provider);
// if no last block is set then we fetch until the current block number
const lastBlock = endBlock || (await provider.getBlockNumber());
const actions: any[] = [];
// Fetch event logs in batches (lastBlock inclusive)
for (let i = fromBlock; i <= lastBlock; i += blocksPerRequest + 1) {
// the last block batch will be either current iteration block + blockPerRequest
// or the end block if it is set
const toBlock = i + blocksPerRequest >= lastBlock ? lastBlock : i + blocksPerRequest;
const [signUpLogs, clrFundLogs] =
// eslint-disable-next-line no-await-in-loop
await Promise.all([
maciContract.queryFilter(maciContract.filters.SignUp(), i, toBlock),
clrFundContract.queryFilter(clrFundContract.filters.Contribution(), i, toBlock),
]);
assert(signUpLogs.length === clrFundLogs.length, "wtf");
for (let i = 0; i < signUpLogs.length; i++) {
const signupEvent = signUpLogs[i];
const contributeEvent = clrFundLogs[i];
actions.push({
key: new PubKey([BigInt(signupEvent.args._userPubKeyX), BigInt(signupEvent.args._userPubKeyY)]),
// @ts-ignore
amount: Number(contributeEvent.args._amount),
});
}
}
return actions;
};
const getMessages = async (
pollContractAddr: string,
provider: Provider,
fromBlock = 0,
blocksPerRequest = 50,
endBlock: number | undefined = undefined,
) => {
const pollContract = Poll__factory.connect(pollContractAddr, provider);
// if no last block is set then we fetch until the current block number
const lastBlock = endBlock || (await provider.getBlockNumber());
const messages: any[] = [];
// fetch poll contract logs
for (let i = fromBlock; i <= lastBlock; i += blocksPerRequest + 1) {
const toBlock = i + blocksPerRequest >= lastBlock ? lastBlock : i + blocksPerRequest;
const [
publishMessageLogs,
// eslint-disable-next-line no-await-in-loop
] = await Promise.all([pollContract.queryFilter(pollContract.filters.PublishMessage(), i, toBlock)]);
publishMessageLogs.forEach((event) => {
if (event.transactionHash !== "0x69bb877937e95ebeffc5256f59f8fccf92be9d42eb8b560f4e18ed26bc881090") {
assert(!!event);
const message = new Message(
BigInt(event.args._message[0]),
event.args._message[1].map((x) => BigInt(x)),
);
const encPubKey = new PubKey(event.args._encPubKey.map((x) => BigInt(x.toString())) as [bigint, bigint]);
messages.push({
message,
encPubKey,
});
} else {
console.log("Skipping transaction: ", event.transactionHash);
}
});
}
return messages;
};
const portSignups = async (
address: string,
clrFundAddress: string,
signer: Signer,
fromBlock = 0,
blocksPerRequest = 50,
// @ts-ignore
tokenAddress: string,
newFundingAddress: string,
endBlock: number | undefined = undefined,
) => {
// get EthDAM Token contract
const tokenContract = new Contract(tokenAddress, tokenAbi, signer);
// pre approve max so need to approve only once
const tx = await tokenContract.approve(newFundingAddress, MaxUint256);
await tx.wait();
const userRegistry = new Contract("0x27Ea159b2ccFC1A51c5EF29e96702c093736867b", userRegistryAbi, signer);
await (await userRegistry.addUser(await signer.getAddress())).wait();
// @ts-ignore
const clrFundContract = new Contract(newFundingAddress, clrFundAbi, signer);
// get all signups
// const data = await getSignups(address, clrFundAddress, signer.provider!, fromBlock, blocksPerRequest, endBlock);
const data = JSON.parse(fs.readFileSync("./ts/signups.json").toString());
console.log("Total signups", data.length);
// contribute
for (let i = 0; i < data.length; i++) {
let status = 0;
while (status !== 1) {
try {
console.log(data[i]);
console.log("Calling contribute again with key: ", PubKey.deserialize(data[i].key.pubKey).asContractParam(), " and amount: ", data[i].amount);
const contributeTx = await clrFundContract.contribute(PubKey.deserialize(data[i].key.pubKey).asContractParam(), BigInt(data[i].amount));
const contributeReceipt = await contributeTx.wait();
status = contributeReceipt.status;
} catch (e) {
console.log("Error: ", e);
console.log("Retrying");
}
}
}
};
const portMessages = async (
oldPollContractAddr: string,
newPollContractAddr: string,
signer: Signer,
fromBlock = 0,
blocksPerRequest = 50,
endBlock: number | undefined = undefined,
) => {
const provider = signer.provider!;
// const messages = await getMessages(oldPollContractAddr, provider, fromBlock, blocksPerRequest, endBlock);
const messages = JSON.parse(fs.readFileSync("./ts/messages.json").toString());
console.log("Total votes", messages.length);
// @ts-ignore
const pollContract = Poll__factory.connect(newPollContractAddr, signer);
for (let i = 0; i < messages.length; i++) {
let status = 0;
while (status !== 1) {
try {
console.log(messages[i]);
console.log(
"Calling submitMessage again with message: ",
Message.fromJSON(messages[i].message).asContractParam(),
" and encPubKey: ",
PubKey.deserialize(messages[i].encPubKey.pubKey).asContractParam(),
);
const publishTx = await pollContract.publishMessage(Message.fromJSON(messages[i].message).asContractParam(), PubKey.deserialize(messages[i].encPubKey.pubKey).asContractParam());
const publishReceipt = await publishTx.wait();
// @ts-ignore
status = publishReceipt.status;
} catch (e) {
console.log("Error: ", e);
console.log("Retrying");
}
}
}
};
const main = async () => {
const signer = await getDefaultSigner();
// console.log(signer.address)
await portSignups(
"0xf5aA8C642BD4EeC9C44FE6997A790b57BCd410D5",
"0xdc36cb99274e7007864512129202F97637832B61",
signer,
33348777,
1000,
"0xBbbe1B8460b5fBC7f43C1dd6929A66F54c1B90ef",
// @todo change below to the new CLR Funding Round address
"0xAbCF7403c8806B1DEF9c83D90dd2Ef6bAA5BcbD4",
33620960,
);
await portMessages(
"0x37f9dcBd2486eDCa7fF2aaD52f1dd7d7D7d70A7c",
// @todo change below to the new Poll address
"0x3A56A74326550f333587Aa25A2D7a2640589A5B4",
signer!,
33348777,
1000,
33620960,
);
};
main().catch();
@ctrlc03
Copy link
Author

ctrlc03 commented Apr 30, 2024

git clone https://github.com/privacy-scaling-explorations/maci
run pnpm install && pnpm build
put file in contracts/ts/rescueDam.ts
run npx hardhat run contracts/ts/rescueDam.ts from inside contracts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment