Skip to content

Commit

Permalink
use ethers v6 and hardhat network helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jan 18, 2024
1 parent 8224941 commit 64e0067
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 46 deletions.
3 changes: 2 additions & 1 deletion contracts/cli/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { JSONFile } from '../utils/JSONFile'
import { ethers } from 'hardhat'
import { program } from 'commander'
import { isPathExist } from '../utils/misc'
import { Contract } from 'ethers'

program
.description('Claim funnds for test recipients')
Expand Down Expand Up @@ -51,7 +52,7 @@ async function main(args: any) {
)
const fundingRoundAsRecipient = fundingRoundContract.connect(
recipients[recipientIndex]
)
) as Contract
const claimTx = await fundingRoundAsRecipient.claimFunds(
...recipientClaimData
)
Expand Down
11 changes: 7 additions & 4 deletions contracts/cli/contribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getEventArg } from '../utils/contracts'
import { program } from 'commander'
import { ethers } from 'hardhat'
import { isPathExist } from '../utils/misc'
import type { FundingRound, ERC20 } from '../typechain-types'

program
.description('Contribute to a funding round')
Expand All @@ -39,7 +40,7 @@ async function main(args: any) {
const maciAddress = await fundingRound.maci()
const maci = await ethers.getContractAt('MACI', maciAddress)

const contributionAmount = UNIT.mul(16).div(10)
const contributionAmount = (UNIT * BigInt(16)) / BigInt(10)

state.contributors = {}
for (const contributor of [contributor1, contributor2]) {
Expand All @@ -49,10 +50,12 @@ async function main(args: any) {
await token.transfer(contributorAddress, contributionAmount)

const contributorKeypair = new Keypair()
const tokenAsContributor = token.connect(contributor)
await tokenAsContributor.approve(fundingRound.address, contributionAmount)
const tokenAsContributor = token.connect(contributor) as ERC20
await tokenAsContributor.approve(fundingRound.target, contributionAmount)

const fundingRoundAsContributor = fundingRound.connect(contributor)
const fundingRoundAsContributor = fundingRound.connect(
contributor
) as FundingRound
const contributionTx = await fundingRoundAsContributor.contribute(
contributorKeypair.pubKey.asContractParam(),
contributionAmount
Expand Down
62 changes: 28 additions & 34 deletions contracts/cli/tally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*
* yarn ts-node cli/tally.ts --round-address <address>
*/
import { ethers, network, config } from 'hardhat'
import { ethers } from 'hardhat'
import { Contract } from 'ethers'

import { DEFAULT_SR_QUEUE_OPS } from '../utils/constants'
import {
DEFAULT_SR_QUEUE_OPS,
DEFAULT_GET_LOG_BATCH_SIZE,
} from '../utils/constants'
import { getIpfsHash } from '../utils/ipfs'
import { JSONFile } from '../utils/JSONFile'
import { deployMessageProcesorAndTally } from '../utils/deployment'
import {
getGenProofArgs,
genProofs,
Expand Down Expand Up @@ -57,6 +59,11 @@ program
'The number of operation for tree merging',
DEFAULT_SR_QUEUE_OPS
)
.option(
'-k --blocks-per-batch <blocks>',
'The number of blocks per batch of logs to fetch on-chain',
DEFAULT_GET_LOG_BATCH_SIZE.toString()
)
.parse()

/**
Expand All @@ -73,14 +80,12 @@ async function main(args: any) {
rapidSnark,
maciTxHash,
numQueueOps,
blocksPerBatch,
} = args

const [coordinator] = await ethers.getSigners()
console.log('Coordinator address: ', coordinator.address)

const providerUrl = (network.config as any).url
console.log('providerUrl', providerUrl)

let clrfundContract: Contract
try {
clrfundContract = await ethers.getContractAt(
Expand All @@ -99,7 +104,7 @@ async function main(args: any) {
fundingRound,
coordinator
)
console.log('Funding round contract', fundingRoundContract.address)
console.log('Funding round contract', fundingRoundContract.target)

const publishedTallyHash = await fundingRoundContract.tallyHash()
console.log('publishedTallyHash', publishedTallyHash)
Expand All @@ -121,60 +126,49 @@ async function main(args: any) {
// Generate proof and tally file
const genProofArgs = getGenProofArgs({
maciAddress,
providerUrl,
pollId,
coordinatorMacisk,
maciTxHash,
rapidSnark,
circuitType: circuit,
circuitDirectory,
outputDir,
blocksPerBatch: Number(blocksPerBatch),
maciTxHash,
})
console.log('genProofsArg', genProofArgs)

await mergeMaciSubtrees(maciAddress, pollId, numQueueOps)
await mergeMaciSubtrees({ maciAddress, pollId, numOperations: numQueueOps })
console.log('Completed tree merge')

await genProofs(genProofArgs)
console.log('Completed genProofs')

tally = JSONFile.read(genProofArgs.tally_file)
tally = JSONFile.read(genProofArgs.tallyFile)
if (stateFile) {
// Save tally file in the state
JSONFile.update(stateFile, { tallyFile: genProofArgs.tally_file })
JSONFile.update(stateFile, { tallyFile: genProofArgs.tallyFile })
}

// deploy the MessageProcessor and Tally contracts used by proveOnChain
const { mpContract, tallyContract } = await deployMessageProcesorAndTally({
artifactsPath: config.paths.artifacts,
ethers,
signer: coordinator,
})
console.log('MessageProcessor', mpContract.address)
console.log('Tally Contract', tallyContract.address)

try {
const pollAddress = await fundingRoundContract.poll()
const pollContract = await ethers.getContractAt('Poll', pollAddress)
const tallyAddress = await pollContract.tally()
const tallyContact = await ethers.getContractAt('Tally', tallyAddress)
const messageProcessorAddress = await tallyContact.mp()
// Submit proofs to MACI contract
await proveOnChain({
contract: maciAddress,
poll_id: pollId,
mp: mpContract.address,
tally: tallyContract.address,
//subsidy: tallyContractAddress, // TODO: make subsidy optional
proof_dir: outputDir,
pollId,
proofDir: genProofArgs.outputDir,
subsidyEnabled: false,
maciAddress,
messageProcessorAddress,
tallyAddress,
})
} catch (e) {
console.error('proveOnChain failed')
throw e
}

// set the Tally contract address for verifying tally result on chain
const setTallyTx = await fundingRoundContract.setTally(
tallyContract.address
)
await setTallyTx.wait()
console.log('Tally contract set in funding round')

// Publish tally hash
const tallyHash = await getIpfsHash(tally)
await fundingRoundContract.publishTallyHash(tallyHash)
Expand Down
5 changes: 2 additions & 3 deletions contracts/cli/timeTravel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* HARDHAT_NETWORK=localhost yarn ts-node cli/timeTravel.ts <state file>
*/

import { network } from 'hardhat'
import { time } from '@nomicfoundation/hardhat-network-helpers'
import { program } from 'commander'

program
Expand All @@ -15,8 +15,7 @@ program

async function main(args: any) {
const seconds = Number(args[0])
await network.provider.send('evm_increaseTime', [seconds])
await network.provider.send('evm_mine')
await time.increase(seconds)
}

main(program.args)
Expand Down
3 changes: 1 addition & 2 deletions contracts/cli/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import { JSONFile } from '../utils/JSONFile'
import { PrivKey, Keypair, createMessage } from '@clrfund/common'
import { BigNumber } from 'ethers'
import { ethers } from 'hardhat'
import { program } from 'commander'
import dotenv from 'dotenv'
Expand Down Expand Up @@ -68,7 +67,7 @@ async function main(args: any) {
nonce += 1
// Vote
for (const recipientIndex of [1, 2]) {
const votes = BigNumber.from(contributorData.voiceCredits).div(4)
const votes = BigInt(contributorData.voiceCredits) / BigInt(4)
const [message, encPubKey] = createMessage(
contributorData.stateIndex,
newContributorKeypair,
Expand Down
7 changes: 5 additions & 2 deletions contracts/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
genTallyResultCommitment,
} from '@clrfund/common'

import { UNIT, ALPHA_PRECISION } from '../utils/constants'
import {
UNIT,
ALPHA_PRECISION,
DEFAULT_GET_LOG_BATCH_SIZE,
} from '../utils/constants'
import { getEventArg } from '../utils/contracts'
import {
deployContract,
Expand All @@ -36,7 +40,6 @@ import path from 'path'
const ZERO = BigInt(0)
const DEFAULT_SR_QUEUE_OPS = 4
const roundDuration = 7 * 86400
const DEFAULT_GET_LOG_BATCH_SIZE = 10000

// MACI zkFiles
const circuit = process.env.CIRCUIT_TYPE || DEFAULT_CIRCUIT
Expand Down

0 comments on commit 64e0067

Please sign in to comment.