-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add claim task for project payouts #677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lordshashank
wants to merge
1
commit into
privacy-ethereum:main
Choose a base branch
from
lordshashank:feat/claim-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* eslint-disable no-console */ | ||
| import { task, types } from "hardhat/config"; | ||
| import { ContractStorage, Deployment, type TallyData } from "maci-contracts"; | ||
| import { genTreeProof } from "maci-crypto"; | ||
|
|
||
| import fs from "fs"; | ||
|
|
||
| import { type MACI, type Poll, type Tally } from "../../typechain-types"; | ||
| import { EContracts } from "../helpers/constants"; | ||
|
|
||
| /** | ||
| * Interface that represents claim task arguments | ||
| */ | ||
| interface IClaimParams { | ||
| poll: string; | ||
| tallyFile: string; | ||
| index: number; | ||
| } | ||
|
|
||
| /** | ||
| * Claim hardhat task for claiming payout for a specific project | ||
| */ | ||
| task("claim", "Command to claim payout for a project") | ||
| .addParam("poll", "The poll id", undefined, types.string) | ||
| .addParam("tallyFile", "The file containing the tally data", undefined, types.string) | ||
| .addParam("index", "The index of the project to claim for", undefined, types.int) | ||
| .setAction(async ({ poll, tallyFile, index }: IClaimParams, hre) => { | ||
| const deployment = Deployment.getInstance(); | ||
|
|
||
| deployment.setHre(hre); | ||
| deployment.setContractNames(EContracts); | ||
|
|
||
| const storage = ContractStorage.getInstance(); | ||
| const signer = await deployment.getDeployer(); | ||
| const { network } = hre; | ||
|
|
||
| const startBalance = await signer.provider.getBalance(signer); | ||
|
|
||
| console.log("Start balance: ", Number(startBalance / 10n ** 12n) / 1e6); | ||
|
|
||
| const { | ||
| MACI__factory: MACIFactory, | ||
| Poll__factory: PollFactory, | ||
| Tally__factory: TallyFactory, | ||
| } = await import("../../typechain-types"); | ||
|
|
||
| // Get MACI contract | ||
| const maciContractAddress = storage.mustGetAddress(EContracts.MACI, network.name); | ||
| const maciContract = await deployment.getContract<MACI>({ | ||
| name: EContracts.MACI, | ||
| address: maciContractAddress, | ||
| abi: MACIFactory.abi, | ||
| }); | ||
|
|
||
| // Get Poll and Tally contracts | ||
| const pollContracts = await maciContract.polls(poll); | ||
| const [pollContract, tallyContract] = await Promise.all([ | ||
| deployment.getContract<Poll>({ | ||
| name: EContracts.Poll, | ||
| address: pollContracts.poll, | ||
| abi: PollFactory.abi, | ||
| }), | ||
| deployment.getContract<Tally>({ | ||
| name: EContracts.Tally, | ||
| address: pollContracts.tally, | ||
| abi: TallyFactory.abi, | ||
| }), | ||
| ]); | ||
|
|
||
| // Check if already claimed | ||
| const isClaimed = await tallyContract.claimed(index); | ||
|
|
||
| if (isClaimed) { | ||
| throw new Error(`Project at index ${index} has already claimed their payout`); | ||
| } | ||
|
|
||
| // Read tally data from file | ||
| if (!fs.existsSync(tallyFile)) { | ||
| throw new Error(`Tally file ${tallyFile} does not exist`); | ||
| } | ||
|
|
||
| const tallyData = await fs.promises | ||
| .readFile(tallyFile, "utf8") | ||
| .then((result) => JSON.parse(result) as unknown as TallyData); | ||
|
|
||
| // Get tree depths from poll contract | ||
| const treeDepths = await pollContract.treeDepths(); | ||
| const voteOptionTreeDepth = Number(treeDepths[3]); | ||
|
|
||
| // Prepare claim parameters | ||
| const tallyResults = tallyData.results.tally.map((x) => BigInt(x)); | ||
|
|
||
| // Validate index | ||
| if (index < 0 || index >= tallyResults.length) { | ||
| throw new Error(`Invalid project index ${index}. Must be between 0 and ${tallyResults.length - 1}`); | ||
| } | ||
|
|
||
| // Generate merkle proof for the tally result | ||
| const tallyResultProof = genTreeProof(index, tallyResults, voteOptionTreeDepth); | ||
|
|
||
| const claimParams = { | ||
| index, | ||
| voiceCreditsPerOption: tallyData.perVOSpentVoiceCredits!.tally[index], | ||
| tallyResultProof, | ||
| tallyResultSalt: tallyData.results.salt, | ||
| voteOptionTreeDepth, | ||
| spentVoiceCreditsHash: tallyData.totalSpentVoiceCredits.commitment, | ||
| perVOSpentVoiceCreditsHash: tallyData.perVOSpentVoiceCredits!.commitment, | ||
| }; | ||
|
|
||
| console.log(`\nClaiming payout for project at index ${index}...`); | ||
| console.log(`Tally result: ${tallyResults[index]}`); | ||
| console.log(`Voice credits per option: ${claimParams.voiceCreditsPerOption}`); | ||
|
|
||
| // Get allocated amount before claiming | ||
| const allocatedAmount = await tallyContract.getAllocatedAmount( | ||
| claimParams.index, | ||
| claimParams.voiceCreditsPerOption, | ||
| ); | ||
|
|
||
| console.log(`Allocated amount: ${allocatedAmount.toString()}`); | ||
|
|
||
| // Execute claim transaction | ||
| const tx = await tallyContract.claim(claimParams); | ||
| const receipt = await tx.wait(); | ||
|
|
||
| if (receipt?.status === 1) { | ||
| console.log(`\n✅ Successfully claimed payout for project at index ${index}`); | ||
| console.log(`Transaction hash: ${receipt.hash}`); | ||
| console.log(`Amount claimed: ${allocatedAmount.toString()}`); | ||
| } else { | ||
| console.log(`\n❌ Claim transaction failed`); | ||
| } | ||
|
|
||
| const endBalance = await signer.provider.getBalance(signer); | ||
|
|
||
| console.log("\nEnd balance: ", Number(endBalance / 10n ** 12n) / 1e6); | ||
| console.log("Claim expenses: ", Number((startBalance - endBalance) / 10n ** 12n) / 1e6); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types should not be defined here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were types in
initPolltask as well, where should I put them?