Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:clrfund/monorepo into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Mar 17, 2023
2 parents 145c04f + 9690144 commit d20227a
Show file tree
Hide file tree
Showing 38 changed files with 1,774 additions and 242 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/cancel-round.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: workflow_dispatch

env:
NODE_VERSION: 16.x
FACTORY_ADDRESS: "0x62cab00605368834c6adf8e6f53da14d6fa9ded1"
SUBGRPAH_URL: "https://api.thegraph.com/subgraphs/name/clrfund/clrfund-arbitrum-goerli"
WALLET_PRIVATE_KEY: ${{ secrets.ARBITRUM_GOERLI_COORDINATOR_WALLET_PRIVATE_KEY }}

jobs:
Expand All @@ -23,4 +23,6 @@ jobs:
- name: Run the cancel round script
run: |
cd contracts
export FACTORY=$(curl -X POST -d '{"query":"{fundingRoundFactories {id}}"}' $SUBGRPAH_URL)
export FACTORY_ADDRESS=$(node -e 'console.log(JSON.parse(process.env.FACTORY).data.fundingRoundFactories[0].id)')
yarn hardhat cancel-round --factory ${FACTORY_ADDRESS} --network arbitrum-goerli
11 changes: 9 additions & 2 deletions .github/workflows/finalize-round.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ on: workflow_dispatch

env:
NODE_VERSION: 16.x
FACTORY_ADDRESS: "0x62cab00605368834c6adf8e6f53da14d6fa9ded1"
SUBGRPAH_URL: "https://api.thegraph.com/subgraphs/name/clrfund/clrfund-arbitrum-goerli"
NETWORK: "arbitrum-goerli"
COORDINATOR_ETH_PK: ${{ secrets.ARBITRUM_GOERLI_COORDINATOR_WALLET_PRIVATE_KEY }}
COORDINATOR_PK: ${{ secrets.ARBITRUM_GOERLI_COORDINATOR_MACI_PRIVATE_KEY }}
ETHERSCAN_API_KEY: ${{ secrets.ETHERSCAN_API_KEY_ARBITRUM }}
ETHERSCAN_URL: "https://api-goerli.arbiscan.io"

jobs:
finalize:
Expand Down Expand Up @@ -41,8 +42,14 @@ jobs:
- name: Run finalize scripts
run: |
export NODE_CONFIG=$(node -e "const snarkParamsPath=process.env.GITHUB_WORKSPACE + '/params'; console.log(JSON.stringify({ snarkParamsPath }));")
export ROUND=$(curl -X POST -d '{"query":"{fundingRoundFactories {currentRound {id}}}"}' $SUBGRPAH_URL)
export ROUND=$(curl -X POST -d '{"query":"{fundingRoundFactories {id currentRound {id maci}}}"}' $SUBGRPAH_URL)
export FACTORY_ADDRESS=$(node -e 'console.log(JSON.parse(process.env.ROUND).data.fundingRoundFactories[0].id)')
export ROUND_ADDRESS=$(node -e 'console.log(JSON.parse(process.env.ROUND).data.fundingRoundFactories[0].currentRound.id)')
export MACI_ADDRESS=$(node -e 'console.log(JSON.parse(process.env.ROUND).data.fundingRoundFactories[0].currentRound.maci)')
export TX_URL="${ETHERSCAN_URL}/api?module=account&action=txlistinternal&address=${MACI_ADDRESS}&startblock=0&page=10&offset=1&sort=asc&apikey=${ETHERSCAN_API_KEY}"
export TXLIST=$(curl $TX_URL)
export MACI_START_BLOCK=$(node -e 'console.log(JSON.parse(process.env.TXLIST).result[0].blockNumber)')
echo "MACI_START_BLOCK:" $MACI_START_BLOCK
# tally and finalize
cd contracts
yarn hardhat run --network "${NETWORK}" scripts/tally.ts
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/new-round.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: workflow_dispatch

env:
NODE_VERSION: 16.x
FACTORY_ADDRESS: "0x62cab00605368834c6adf8e6f53da14d6fa9ded1"
SUBGRPAH_URL: "https://api.thegraph.com/subgraphs/name/clrfund/clrfund-arbitrum-goerli"
WALLET_PRIVATE_KEY: ${{ secrets.ARBITRUM_GOERLI_COORDINATOR_WALLET_PRIVATE_KEY }}

jobs:
Expand All @@ -23,4 +23,6 @@ jobs:
- name: Run create new round script
run: |
cd contracts
export FACTORY=$(curl -X POST -d '{"query":"{fundingRoundFactories {id}}"}' $SUBGRPAH_URL)
export FACTORY_ADDRESS=$(node -e 'console.log(JSON.parse(process.env.FACTORY).data.fundingRoundFactories[0].id)')
yarn hardhat run scripts/newRound.ts --network arbitrum-goerli
2 changes: 1 addition & 1 deletion .github/workflows/test-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
yarn deployTestRound:local
yarn contribute:local
yarn vote:local
yarn hardhat evm-increase-time 360 --network localhost
yarn hardhat evm-increase-time 1200 --network localhost
yarn tally:local
yarn finalize:local
yarn claim:local
7 changes: 6 additions & 1 deletion contracts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ ROUND_ADDRESS=
COORDINATOR_PK=
COORDINATOR_ETH_PK=

# used in the finalize script to add tally results to funding round by batch
# Used in the tally script to add tally results to funding round by batch, default is 20
TALLY_BATCH_SIZE=
# The block that the MACI contract was created in. Used in fetchLogs
MACI_START_BLOCK=
# Number of blocks of the MACI event logs to fetch, default is 20,000
NUM_BLOCKS_PER_REQUEST=


# Used to verify contracts
# Get the etherscan api keys for goerli, mainnet from https://etherscan.io
Expand Down
6 changes: 5 additions & 1 deletion contracts/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ethers, waffle } from 'hardhat'
import { use, expect } from 'chai'
import { solidity } from 'ethereum-waffle'
import { BigNumber, Contract, Signer, Wallet } from 'ethers'
import { BigNumber, Contract, Signer, Wallet, utils } from 'ethers'
import { genProofs, proveOnChain } from 'maci-cli'
import { Keypair } from 'maci-domainobjs'

Expand Down Expand Up @@ -223,6 +223,9 @@ describe('End-to-end Tests', function () {
return contributions
}

function makeMaciFilename(): string {
return `macistate_${utils.hexlify(utils.randomBytes(10))}`
}
async function finalizeRound(): Promise<any> {
const providerUrl = (provider as any)._hardhatNetwork.config.url

Expand All @@ -231,6 +234,7 @@ describe('End-to-end Tests', function () {
contract: maci.address,
eth_provider: providerUrl,
privkey: coordinatorKeypair.privKey.serialize(),
macistate: makeMaciFilename(),
})
if (!results) {
throw new Error('generation of proofs failed')
Expand Down
8 changes: 4 additions & 4 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@openzeppelin/contracts": "3.2.0",
"dotenv": "^8.2.0",
"maci-contracts": "0.9.4",
"maci-contracts": "0.10.1",
"solidity-rlp": "2.0.3"
},
"devDependencies": {
Expand All @@ -46,9 +46,9 @@
"hardhat": "^2.12.5",
"hardhat-contract-sizer": "^2.6.1",
"ipfs-only-hash": "^2.0.1",
"maci-cli": "0.9.4",
"maci-crypto": "~0.9.1",
"maci-domainobjs": "~0.9.1",
"maci-cli": "npm:@clrfund/[email protected]",
"maci-crypto": "npm:@clrfund/[email protected]",
"maci-domainobjs": "npm:@clrfund/[email protected]",
"solhint": "^3.3.2",
"ts-generator": "^0.0.8",
"ts-node": "^8.8.1",
Expand Down
52 changes: 51 additions & 1 deletion contracts/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import { ethers } from 'hardhat'
import { Contract, utils } from 'ethers'
import { Contract, utils, Wallet } from 'ethers'

import { UNIT } from '../utils/constants'
import { deployMaciFactory } from '../utils/deployment'
import { Keypair, PrivKey } from 'maci-domainobjs'

/**
* Set the coordinator address and maci public key in the funding round factory
*
* @param fundingRoundFactory funding round factory contract
* @param coordinatorAddress
* @param MaciPrivateKey
*/
async function setCoordinator(
fundingRoundFactory: Contract,
coordinatorAddress: string,
coordinatorKey?: string
) {
// Generate or use the passed in coordinator key
const privKey = coordinatorKey
? PrivKey.unserialize(coordinatorKey)
: undefined
const keypair = new Keypair(privKey)
const coordinatorPubKey = keypair.pubKey
const serializedCoordinatorPrivKey = keypair.privKey.serialize()
const serializedCoordinatorPubKey = keypair.pubKey.serialize()
const setCoordinatorTx = await fundingRoundFactory.setCoordinator(
coordinatorAddress,
coordinatorPubKey.asContractParam()
)
await setCoordinatorTx.wait()
console.log('coordinator address:', coordinatorAddress)
console.log('serializedCoordinatorPrivKey: ', serializedCoordinatorPrivKey)
console.log('serializedCoordinatorPubKey: ', serializedCoordinatorPubKey)
}

async function main() {
const [deployer] = await ethers.getSigners()
Expand Down Expand Up @@ -88,6 +119,25 @@ async function main() {
recipientRegistry.address
)
await setRecipientRegistryTx.wait()

if (process.env.NATIVE_TOKEN_ADDRESS) {
const setTokenTx = await fundingRoundFactory.setToken(
process.env.NATIVE_TOKEN_ADDRESS
)
await setTokenTx.wait()
console.log('Set token', process.env.NATIVE_TOKEN_ADDRESS)
}

const coordinatorAddress = process.env.COORDINATOR_ETH_PK
? new Wallet(process.env.COORDINATOR_ETH_PK).address
: await deployer.getAddress()

await setCoordinator(
fundingRoundFactory,
coordinatorAddress,
process.env.COORDINATOR_PK
)

console.log(`Deployment complete!`)
}

Expand Down
22 changes: 0 additions & 22 deletions contracts/scripts/finalize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import { Wallet } from 'ethers'
import { ethers, network } from 'hardhat'
import { addTallyResultsBatch } from '../utils/maci'

async function main() {
let factoryAddress, coordinator
Expand Down Expand Up @@ -36,27 +35,6 @@ async function main() {
)
console.log('Current round', fundingRound.address)

const maciAddress = await fundingRound.maci()
const maci = await ethers.getContractAt('MACI', maciAddress, coordinator)
const [, , voteOptionTreeDepth] = await maci.treeDepths()
console.log('Vote option tree depth', voteOptionTreeDepth)

const batchSize = Number(process.env.TALLY_BATCH_SIZE) || 20
const startIndex = await fundingRound.totalTallyResults()
const total = tally.results.tally.length
console.log('Adding tally results in batches of', batchSize)
const addTallyGas = await addTallyResultsBatch(
fundingRound,
voteOptionTreeDepth,
tally,
batchSize,
startIndex.toNumber(),
(processed) => {
console.log(`Processed ${processed} / ${total}`)
}
)
console.log('Tally results added. Gas used:', addTallyGas.toString())

const totalSpent = parseInt(tally.totalVoiceCredits.spent)
const totalSpentSalt = tally.totalVoiceCredits.salt
const tx = await factory.transferMatchingFunds(totalSpent, totalSpentSalt)
Expand Down
54 changes: 53 additions & 1 deletion contracts/scripts/tally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
import fs from 'fs'
import { network, ethers } from 'hardhat'
import { Wallet } from 'ethers'
import { genProofs, proveOnChain } from 'maci-cli'
import { genProofs, proveOnChain, fetchLogs } from 'maci-cli'

import { getIpfsHash } from '../utils/ipfs'
import { addTallyResultsBatch } from '../utils/maci'

async function main() {
let fundingRoundAddress: string
let coordinatorPrivKey: string
let coordinatorEthPrivKey: string
let startBlock = 0
let numBlocksPerRequest = 20000
const batchSize = Number(process.env.TALLY_BATCH_SIZE) || 20
if (network.name === 'localhost') {
const stateStr = fs.readFileSync('state.json').toString()
const state = JSON.parse(stateStr)
Expand All @@ -23,7 +27,21 @@ async function main() {
fundingRoundAddress = process.env.ROUND_ADDRESS || ''
coordinatorPrivKey = process.env.COORDINATOR_PK || ''
coordinatorEthPrivKey = process.env.COORDINATOR_ETH_PK || ''
numBlocksPerRequest =
Number(process.env.NUM_BLOCKS_PER_REQUEST) || numBlocksPerRequest

if (process.env.MACI_START_BLOCK) {
startBlock = Number(process.env.MACI_START_BLOCK)
} else {
throw new Error(
'Please set MACI_START_BLOCK environment variable for fetchLogs'
)
}
}

const timeMs = new Date().getTime()
const maciStateFile = `maci_state_${timeMs}.json`
const logsFile = `maci_logs_${timeMs}.json`
const coordinator = new Wallet(coordinatorEthPrivKey, ethers.provider)
const fundingRound = await ethers.getContractAt(
'FundingRound',
Expand All @@ -35,13 +53,27 @@ async function main() {
console.log('maci address', maciAddress)
const providerUrl = (network.config as any).url

// Fetch Maci logs
console.log('Fetching MACI logs from block', startBlock)
await fetchLogs({
contract: maciAddress,
eth_provider: providerUrl,
privkey: coordinatorPrivKey,
start_block: startBlock,
num_blocks_per_request: numBlocksPerRequest,
output: logsFile,
})
console.log('MACI logs generated at', logsFile)

// Process messages and tally votes
const results = await genProofs({
contract: maciAddress,
eth_provider: providerUrl,
privkey: coordinatorPrivKey,
tally_file: 'tally.json',
output: 'proofs.json',
logs_file: logsFile,
macistate: maciStateFile,
})
if (!results) {
throw new Error('generation of proofs failed')
Expand All @@ -61,6 +93,26 @@ async function main() {
const tallyHash = await getIpfsHash(tally)
await fundingRound.publishTallyHash(tallyHash)
console.log(`Tally hash is ${tallyHash}`)

// Submit results to the funding round contract
const maci = await ethers.getContractAt('MACI', maciAddress, coordinator)
const [, , voteOptionTreeDepth] = await maci.treeDepths()
console.log('Vote option tree depth', voteOptionTreeDepth)

const startIndex = await fundingRound.totalTallyResults()
const total = tally.results.tally.length
console.log('Uploading tally results in batches of', batchSize)
const addTallyGas = await addTallyResultsBatch(
fundingRound,
voteOptionTreeDepth,
tally,
batchSize,
startIndex.toNumber(),
(processed: number) => {
console.log(`Processed ${processed} / ${total}`)
}
)
console.log('Tally results uploaded. Gas used:', addTallyGas.toString())
}

main()
Expand Down
1 change: 1 addition & 0 deletions contracts/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ import './evmIncreaseTime'
import './auditTally'
import './fetchRound'
import './mergeAllocations'
import './setDurations'
34 changes: 34 additions & 0 deletions contracts/tasks/setDurations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { task, types } from 'hardhat/config'
import { MaciParameters } from '../utils/maci'

task('set-durations', 'Set the signup and voting durations for future rounds')
.addParam('factory', 'The funding round factory contract address')
.addParam('signup', 'Sign up duration in minutes', 60, types.int)
.addParam('voting', 'Voting duration in minutes', 10, types.int)
.setAction(async ({ factory, signup, voting }, { ethers }) => {
const signUpDuration = signup * 60
const votingDuration = voting * 60

const fundingRoundFactory = await ethers.getContractAt(
'FundingRoundFactory',
factory
)

const maciFactoryAddress = await fundingRoundFactory.maciFactory()
const maciFactory = await ethers.getContractAt(
'MACIFactory',
maciFactoryAddress
)
const maciParameters = await MaciParameters.read(maciFactory)
maciParameters.update({
signUpDuration,
votingDuration,
})
const setMaciParametersTx = await fundingRoundFactory.setMaciParameters(
...maciParameters.values()
)
await setMaciParametersTx.wait()

const newParams = await MaciParameters.read(maciFactory)
console.log('New durations set', newParams)
})
Loading

0 comments on commit d20227a

Please sign in to comment.