Skip to content

Commit

Permalink
update contribute and vote scripts to make many contributions and votes
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Jan 31, 2022
1 parent 8e74169 commit c805981
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 54 deletions.
3 changes: 3 additions & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"deployTestRound:local": "hardhat run --network localhost scripts/deployTestRound.ts",
"contribute:local": "hardhat run --network localhost scripts/contribute.ts",
"vote:local": "hardhat run --network localhost scripts/vote.ts",
"deploy:rinkarby": "hardhat run --network rinkarby scripts/deployRound.ts",
"contribute:rinkarby": "hardhat run --network rinkarby scripts/contribute.ts",
"vote:rinkarby": "hardhat run --network rinkarby scripts/vote.ts",
"tally:local": "hardhat run --network localhost scripts/tally.ts",
"finalize:local": "hardhat run --network localhost scripts/finalize.ts",
"claim:local": "hardhat run --network localhost scripts/claim.ts",
Expand Down
30 changes: 27 additions & 3 deletions contracts/scripts/contribute.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import fs from 'fs'
import { Wallet } from 'ethers'
import { ethers } from 'hardhat'
import { Keypair } from 'maci-domainobjs'

import { UNIT } from '../utils/constants'
import { getEventArg } from '../utils/contracts'

async function main() {
const [, , , , , , , , , , , , contributor1, contributor2] =
await ethers.getSigners()
// const [, , , , , , , , , , , , contributor1, contributor2, contributor3] =
// await ethers.getSigners()

const contributor1 = new Wallet(
process.env.CONTRIBUTOR_PK_1!,
ethers.provider
)
const contributor2 = new Wallet(
process.env.CONTRIBUTOR_PK_2!,
ethers.provider
)
const contributor3 = new Wallet(
process.env.CONTRIBUTOR_PK_3!,
ethers.provider
)
const contributor4 = new Wallet(
process.env.CONTRIBUTOR_PK_4!,
ethers.provider
)

const state = JSON.parse(fs.readFileSync('state.json').toString())
const fundingRound = await ethers.getContractAt(
'FundingRound',
Expand All @@ -21,7 +40,12 @@ async function main() {
const contributionAmount = UNIT.mul(16).div(10)
state.contributors = {}

for (const contributor of [contributor1, contributor2]) {
for (const contributor of [
contributor1,
contributor2,
contributor3,
contributor4,
]) {
const contributorAddress = await contributor.getAddress()
const contributorKeypair = new Keypair()
const tokenAsContributor = token.connect(contributor)
Expand Down
96 changes: 53 additions & 43 deletions contracts/scripts/deployRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ async function main() {
deployer
)
userRegistry = await SimpleUserRegistry.deploy()

const users = [
'0x63bb006f4E87Ce48dC62f440d14a114718302db7',
'0xf75E7D74F0C425457E6389e152AEf6241aC62E0e',
'0xa70bd8D67A222F8D8E0D15999A87666da0406Fe4',
'0x03C6464805102E497EA695e8b0423Ae36e5Df240',
]
let addUserTx
for (const account of users) {
addUserTx = await userRegistry.addUser(account)
addUserTx.wait()
}
} else if (userRegistryType === 'brightid') {
const BrightIdUserRegistry = await ethers.getContractFactory(
'BrightIdUserRegistry',
Expand Down Expand Up @@ -112,8 +124,8 @@ async function main() {
maciFactory = await ethers.getContractAt('MACIFactory', maciFactory.address)
const maciParameters = await MaciParameters.read(maciFactory)
maciParameters.update({
signUpDuration: 86400 * 14,
votingDuration: 86400 * 3,
signUpDuration: 60 * 30,
votingDuration: 60 * 1,
})
const setMaciParametersTx = await fundingRoundFactory.setMaciParameters(
...maciParameters.values()
Expand Down Expand Up @@ -184,52 +196,50 @@ async function main() {
}
} else if (recipientRegistryType === 'optimistic') {
const deposit = await recipientRegistry.baseDeposit()
const recipient1Added = await recipientRegistry.addRecipient(
deployer.address,
JSON.stringify(metadataRecipient1),
{ value: deposit }
)
await recipient1Added.wait()

const recipient1Id = await getEventArg(
recipient1Added,
recipientRegistry,
'RequestSubmitted',
'_recipientId'
)
const executeRequest1 = await recipientRegistry.executeRequest(recipient1Id)
await executeRequest1.wait()

const recipient2Added = await recipientRegistry.addRecipient(
deployer.address,
JSON.stringify(metadataRecipient2),
{ value: deposit }
)
await recipient2Added.wait()
const recipientsNumber = 10
for (
let recipientsIndex = 0;
recipientsIndex < recipientsNumber;
recipientsIndex++
) {
const metadata = {
...metadataRecipient1,
description: `${metadataRecipient1.description}${recipientsIndex}`,
}
const recipient3Added = await recipientRegistry.addRecipient(
deployer.getAddress(),
JSON.stringify(metadata),
{ value: deposit }
)
recipient3Added.wait()

const recipient2Id = await getEventArg(
recipient2Added,
recipientRegistry,
'RequestSubmitted',
'_recipientId'
)
const executeRequest2 = await recipientRegistry.executeRequest(recipient2Id)
await executeRequest2.wait()
const recipient3Id = await getEventArg(
recipient3Added,
recipientRegistry,
'RequestSubmitted',
'_recipientId'
)
const executeRequest3 = await recipientRegistry.executeRequest(
recipient3Id
)
await executeRequest3.wait()
}
}

const fundingRoundAddress = await fundingRoundFactory.getCurrentRound()
console.log('fundingRound.address: ', fundingRoundAddress)
const fundingRoundAddress = await fundingRoundFactory.getCurrentRound()
console.log('fundingRound.address: ', fundingRoundAddress)

const fundingRound = await ethers.getContractAt(
'FundingRound',
fundingRoundAddress
)
const maciAddress = await fundingRound.maci()
console.log('maci.address: ', maciAddress)
const fundingRound = await ethers.getContractAt(
'FundingRound',
fundingRoundAddress
)
const maciAddress = await fundingRound.maci()
console.log('maci.address: ', maciAddress)

console.log('*******************')
console.log('Deploy complete!')
console.log('*******************')
}
console.log('*******************')
console.log('Deploy complete!')
console.log('*******************')
}

main()
Expand Down
53 changes: 45 additions & 8 deletions contracts/scripts/vote.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import fs from 'fs'
import { ethers } from 'hardhat'
import { BigNumber } from 'ethers'
import { BigNumber, Wallet } from 'ethers'
import { PrivKey, Keypair } from 'maci-domainobjs'

import { createMessage } from '../utils/maci'

async function main() {
const [, , , , , , , , , , , , contributor1, contributor2] =
await ethers.getSigners()
// const [, , , , , , , , , , , , contributor1, contributor2, contributor3] =
// await ethers.getSigners()

const contributor1 = new Wallet(
process.env.CONTRIBUTOR_PK_1!,
ethers.provider
)
const contributor2 = new Wallet(
process.env.CONTRIBUTOR_PK_2!,
ethers.provider
)
const contributor3 = new Wallet(
process.env.CONTRIBUTOR_PK_3!,
ethers.provider
)
const contributor4 = new Wallet(
process.env.CONTRIBUTOR_PK_4!,
ethers.provider
)

const state = JSON.parse(fs.readFileSync('state.json').toString())
const coordinatorKeyPair = new Keypair(
PrivKey.unserialize(state.coordinatorPrivKey)
)

for (const contributor of [contributor1, contributor2]) {
for (const contributor of [
contributor1,
contributor2,
contributor3,
contributor4,
]) {
const contributorAddress = await contributor.getAddress()
const contributorData = state.contributors[contributorAddress]
const contributorKeyPair = new Keypair(
Expand All @@ -37,8 +60,11 @@ async function main() {
encPubKeys.push(encPubKey.asContractParam())
nonce += 1
// Vote
for (const recipientIndex of [1, 2]) {
const votes = BigNumber.from(contributorData.voiceCredits).div(4)
const recipients = [1, 2, 3, 4, 5, 6, 7]
for (const recipientIndex of recipients) {
const votes = BigNumber.from(contributorData.voiceCredits).div(
recipients.length
)
const [message, encPubKey] = createMessage(
contributorData.stateIndex,
newContributorKeypair,
Expand All @@ -58,10 +84,21 @@ async function main() {
state.fundingRound,
contributor
)
await fundingRoundAsContributor.submitMessageBatch(

const tx = await fundingRoundAsContributor.submitMessageBatch(
messages.reverse(),
encPubKeys.reverse()
encPubKeys.reverse(),
{
gasLimit: 20000000,
}
)
try {
await tx.wait()
} catch (err) {
console.log('error!', err)
return
}

console.log(`Contributor ${contributorAddress} voted.`)
}
}
Expand Down

0 comments on commit c805981

Please sign in to comment.