From f96dfa5db7dd0c88330324ae3ea7d4e4b165193b Mon Sep 17 00:00:00 2001 From: huyminh1115 Date: Wed, 31 Jan 2024 01:44:10 +0700 Subject: [PATCH] Update script --- src/scripts/Platform.ts | 14 +- src/scripts/interactions/campaign/rollup.ts | 17 +- .../interactions/participation/rollup.ts | 407 +++++++++--------- 3 files changed, 223 insertions(+), 215 deletions(-) diff --git a/src/scripts/Platform.ts b/src/scripts/Platform.ts index aa36e04..163e4c2 100644 --- a/src/scripts/Platform.ts +++ b/src/scripts/Platform.ts @@ -1089,13 +1089,13 @@ async function main() { let balanceBefore = Number( Account(projects[i].publicKey).balance.get() ); - // tx = await Mina.transaction( - // { sender: projects[i].publicKey, fee }, - // () => { - // treasuryContract.claimFund(claimFundInput[i]); - // } - // ); - // await proveAndSend(tx, [projects[i]], Contract.TREASURY, 'claimFund'); + tx = await Mina.transaction( + { sender: projects[i].publicKey, fee }, + () => { + treasuryContract.claimFund(claimFundInput[i]); + } + ); + await proveAndSend(tx, [projects[i]], Contract.TREASURY, 'claimFund'); let balanceAfter = Number( Account(projects[i].publicKey).balance.get() ); diff --git a/src/scripts/interactions/campaign/rollup.ts b/src/scripts/interactions/campaign/rollup.ts index bae729d..3104cb8 100644 --- a/src/scripts/interactions/campaign/rollup.ts +++ b/src/scripts/interactions/campaign/rollup.ts @@ -66,7 +66,6 @@ async function main() { // Do this and state value of contract is fetched in Mina await fetchZkAppState(zkAppAddress); let nextCampaignId = Number(zkContract.nextCampaignId.get()); - nextCampaignId = 4; // Storage let campaignInfoStorage = new CampaignInfoStorage(); @@ -165,23 +164,23 @@ async function main() { // update storage: ownerStorage.updateLeaf( - ownerStorage.calculateLeaf(action.owner), - Field(i) + Field(i), + ownerStorage.calculateLeaf(action.owner) ); campaignInfoStorage.updateLeaf( - campaignInfoStorage.calculateLeaf(action.ipfsHash), - Field(i) + Field(i), + campaignInfoStorage.calculateLeaf(action.ipfsHash) ); statusStorage.updateLeaf( - statusStorage.calculateLeaf(StatusEnum.APPLICATION), - Field(i) + Field(i), + statusStorage.calculateLeaf(StatusEnum.APPLICATION) ); configStorage.updateLeaf( + Field(i), configStorage.calculateLeaf({ committeeId: action.committeeId, keyId: action.keyId, - }), - Field(i) + }) ); console.log('DONE'); diff --git a/src/scripts/interactions/participation/rollup.ts b/src/scripts/interactions/participation/rollup.ts index e1bb974..be69522 100644 --- a/src/scripts/interactions/participation/rollup.ts +++ b/src/scripts/interactions/participation/rollup.ts @@ -1,235 +1,244 @@ import fs from 'fs'; import { - Cache, - Field, - Mina, - PrivateKey, - Provable, - PublicKey, - Reducer, - fetchAccount, + Cache, + Field, + Mina, + PrivateKey, + Provable, + PublicKey, + Reducer, + fetchAccount, + Poseidon, } from 'o1js'; import { Config, JSONKey, Key } from '../../helper/config.js'; import { - ContractList, - compile, - wait, - proveAndSend, + ContractList, + compile, + wait, + proveAndSend, } from '../../helper/deploy.js'; import { fetchActions, fetchZkAppState } from '../../helper/deploy.js'; import { - ProjectContract, - ProjectAction, - CreateProject, - CreateProjectInput, - ProjectProof, + ProjectContract, + ProjectAction, + CreateProject, + CreateProjectInput, + ProjectProof, } from '../../../contracts/Project.js'; import { - MemberStorage, - InfoStorage, - MemberArray, - InfoStorage as ProjectInfoStorage, - AddressStorage as PayeeStorage, - EMPTY_LEVEL_2_TREE, - Level2Witness, + MemberStorage, + InfoStorage, + MemberArray, + InfoStorage as ProjectInfoStorage, + AddressStorage as PayeeStorage, + EMPTY_LEVEL_2_TREE, + Level2Witness, } from '../../../contracts/ProjectStorage.js'; import { - CampaignContract, - CreateCampaign, - CreateCampaignInput, - CampaignAction, + CampaignContract, + CreateCampaign, + CreateCampaignInput, + CampaignAction, } from '../../../contracts/Campaign.js'; import { - InfoStorage as CampaignInfoStorage, - OwnerStorage, - StatusStorage, - ConfigStorage, - StatusEnum, + InfoStorage as CampaignInfoStorage, + OwnerStorage, + StatusStorage, + ConfigStorage, + StatusEnum, } from '../../../contracts/CampaignStorage.js'; -import { AddressStorage } from '../../../contracts/SharedStorage.js'; import { - ParticipationContract, - JoinCampaign, - ParticipationAction, - JoinCampaignInput, + AddressStorage, + ZkAppRef, + AddressWitness, +} from '../../../contracts/SharedStorage.js'; +import { + ParticipationContract, + JoinCampaign, + ParticipationAction, + JoinCampaignInput, } from '../../../contracts/Participation.js'; import { - InfoStorage as ParticipationInfoStorage, - CounterStorage, - IndexStorage, - EMPTY_LEVEL_1_TREE, - EMPTY_LEVEL_1_COMBINED_TREE, + InfoStorage as ParticipationInfoStorage, + CounterStorage, + IndexStorage, + EMPTY_LEVEL_1_TREE, + EMPTY_LEVEL_1_COMBINED_TREE, } from '../../../contracts/ParticipationStorage.js'; import axios from 'axios'; import { IPFSHash } from '@auxo-dev/auxo-libs'; import { prepare } from '../prepare.js'; +import { ZkApp } from '@auxo-dev/dkg'; +import { Contract, ZkAppEnum } from '../../../constants.js'; // Da test reduce 1 action, 2 action co the sai :v async function main() { - const { cache, feePayer, addressMerkleTree } = await prepare(); - - const campaignId = 4; - const projectId = 1; - - // Compile programs - await compile(JoinCampaign, cache); - await compile(ParticipationContract, cache); - - const projectAddress = process.env.BERKELEY_PROJECT_ADDRESS as string; - const participationAddress = process.env - .BERKELEY_PARTICIPATION_ADDRESS as string; - const campaignAddress = process.env.BERKELEY_CAMPAIGN_ADDRESS as string; - - const participationContract = new ParticipationContract( - PublicKey.fromBase58(participationAddress) - ); - const campaignContract = new CampaignContract( - PublicKey.fromBase58(campaignAddress) - ); - - // Do this and state value of contract is fetched in Mina - await fetchZkAppState(projectAddress); - await fetchZkAppState(participationAddress); - await fetchZkAppState(campaignAddress); - - let nextCampaignId = Number(campaignContract.nextCampaignId.get()); - nextCampaignId = 5; - - // Storage - // Project - let memberStorage = new MemberStorage(); - // Participation - let participationInfoStorage = new ParticipationInfoStorage(); - let counterStorage = new CounterStorage(); - let indexStorage = new IndexStorage(); - let participationAddressStorage = new AddressStorage(addressMerkleTree); - - // Fetch storage trees - const projects = (await axios.get('https://api.auxo.fund/v0/projects/')).data; - - // Build storage - projects.map((project: any) => { - if (Boolean(project.active)) { - console.log(project); - let level2Tree = EMPTY_LEVEL_2_TREE(); - for (let i = 0; i < project.members.length; i++) { - level2Tree.setLeaf( - BigInt(i), - MemberArray.hash(PublicKey.fromBase58(project.members[i])) + const { cache, feePayer, addressMerkleTree } = await prepare(); + + // Compile programs + await compile(JoinCampaign, cache); + await compile(ParticipationContract, cache); + + const projectAddress = process.env.BERKELEY_PROJECT_ADDRESS as string; + const participationAddress = process.env + .BERKELEY_PARTICIPATION_ADDRESS as string; + const campaignAddress = process.env.BERKELEY_CAMPAIGN_ADDRESS as string; + + const participationContract = new ParticipationContract( + PublicKey.fromBase58(participationAddress) + ); + const campaignContract = new CampaignContract( + PublicKey.fromBase58(campaignAddress) + ); + + // Do this and state value of contract is fetched in Mina + await fetchZkAppState(projectAddress); + await fetchZkAppState(participationAddress); + await fetchZkAppState(campaignAddress); + + let nextCampaignId = Number(campaignContract.nextCampaignId.get()); + + // Storage + // Project + let memberStorage = new MemberStorage(); + // Participation + let participationInfoStorage = new ParticipationInfoStorage(); + let counterStorage = new CounterStorage(); + let indexStorage = new IndexStorage(); + + // Fetch storage trees + const [projects, index, counter, info] = await Promise.all([ + (await axios.get('https://api.auxo.fund/v0/projects/')).data, + ( + await axios.get( + 'https://api.auxo.fund/v0/storages/participation/index/leafs' + ) + ).data, + ( + await axios.get( + 'https://api.auxo.fund/v0/storages/participation/counter/leafs' + ) + ).data, + ( + await axios.get( + 'https://api.auxo.fund/v0/storages/participation/info/leafs' + ) + ).data, + ]); + + for (let key in index) { + indexStorage.updateLeaf(Field(key), Field(index[key]['leaf'])); + } + + for (let key in counter) { + counterStorage.updateLeaf(Field(key), Field(index[key]['leaf'])); + } + + for (let key in info) { + participationInfoStorage.updateLeaf( + Field(key), + Field(index[key]['leaf']) ); - } - memberStorage.updateInternal(Field(project.projectId), level2Tree); } - }); - - for (let i = 0; i < nextCampaignId; i++) { - let campaignId = Field(i); - const projectsInCampaign = ( - await axios.get( - `https://api.auxo.fund/v0/campaigns/${campaignId}/projects` - ) - ).data; - let numParticipant = projectsInCampaign.length; - counterStorage.updateLeaf( - counterStorage.calculateLeaf(Field(numParticipant)), - counterStorage.calculateLevel1Index(campaignId) - ); + // Build storage - for (let j = 0; j < numParticipant; j++) { - let index = Field(j + 1); - indexStorage.updateLeaf( - indexStorage.calculateLeaf(index), - indexStorage.calculateLevel1Index({ - campaignId: campaignId, - projectId: projectsInCampaign[j].projectId, - }) - ); - } - } - - const fromState = participationContract.lastRolledUpActionState.get(); - const rawActions = await fetchActions(participationAddress, fromState); - - const actions: ParticipationAction[] = rawActions.map((e) => { - let action: Field[] = e.actions[0].map((e) => Field(e)); - return ParticipationAction.fromFields(action); - }); - - const reduceActions = actions; - - // console.log('JoinCampaign.firstStep...'); - let proof = await JoinCampaign.firstStep( - participationContract.indexTreeRoot.get(), - participationContract.infoTreeRoot.get(), - participationContract.counterTreeRoot.get(), - participationContract.lastRolledUpActionState.get() - ); - - for (let i = 0; i < reduceActions.length; i++) { - let action = reduceActions[i]; - console.log(`${i} - JoinCampaign.joinCampaign...`); - - proof = await JoinCampaign.joinCampaign( - proof, - action, - indexStorage.getLevel1Witness( - indexStorage.calculateLevel1Index({ - campaignId: action.campaignId, - projectId: action.projectId, - }) - ), - participationInfoStorage.getLevel1Witness( - participationInfoStorage.calculateLevel1Index({ - campaignId: action.campaignId, - projectId: action.projectId, - }) - ), - Field(i), // current couter of each campaign is 0 - counterStorage.getLevel1Witness( - counterStorage.calculateLevel1Index(action.campaignId) - ) + projects.map((project: any) => { + if (Boolean(project.active)) { + let level2Tree = EMPTY_LEVEL_2_TREE(); + for (let i = 0; i < project.members.length; i++) { + level2Tree.setLeaf( + BigInt(i), + MemberArray.hash(PublicKey.fromBase58(project.members[i])) + ); + } + memberStorage.updateInternal(Field(project.projectId), level2Tree); + } + }); + + const fromState = participationContract.lastRolledUpActionState.get(); + const rawActions = await fetchActions(participationAddress, fromState); + + const actions: ParticipationAction[] = rawActions.map((e) => { + let action: Field[] = e.actions[0].map((e) => Field(e)); + return ParticipationAction.fromFields(action); + }); + + const reduceActions = actions; + console.log('reduceActions: ', reduceActions); + + console.log('JoinCampaign.firstStep...'); + let proof = await JoinCampaign.firstStep( + participationContract.indexTreeRoot.get(), + participationContract.infoTreeRoot.get(), + participationContract.counterTreeRoot.get(), + participationContract.lastRolledUpActionState.get() ); - // update storage: - indexStorage.updateLeaf( - indexStorage.calculateLeaf(Field(i + 1)), // index start from 1 - indexStorage.calculateLevel1Index({ - campaignId: action.campaignId, - projectId: action.projectId, - }) - ); + for (let i = 0; i < reduceActions.length; i++) { + let action = reduceActions[i]; + console.log(`${i} - JoinCampaign.joinCampaign...`); + + proof = await JoinCampaign.joinCampaign( + proof, + action, + indexStorage.getLevel1Witness( + indexStorage.calculateLevel1Index({ + campaignId: action.campaignId, + projectId: action.projectId, + }) + ), + participationInfoStorage.getLevel1Witness( + participationInfoStorage.calculateLevel1Index({ + campaignId: action.campaignId, + projectId: action.projectId, + }) + ), + Field(i), // current couter of each campaign is 0 + counterStorage.getLevel1Witness( + counterStorage.calculateLevel1Index(action.campaignId) + ) + ); - participationInfoStorage.updateLeaf( - participationInfoStorage.calculateLeaf(action.participationInfo), - participationInfoStorage.calculateLevel1Index({ - campaignId: action.campaignId, - projectId: action.projectId, - }) - ); - counterStorage.updateLeaf( - counterStorage.calculateLeaf(Field(i + 1)), - counterStorage.calculateLevel1Index(action.campaignId) - ); + // update storage: + indexStorage.updateLeaf( + indexStorage.calculateLevel1Index({ + campaignId: action.campaignId, + projectId: action.projectId, + }), + indexStorage.calculateLeaf(Field(i + 1)) // index start from 1 + ); + + participationInfoStorage.updateLeaf( + participationInfoStorage.calculateLevel1Index({ + campaignId: action.campaignId, + projectId: action.projectId, + }), + participationInfoStorage.calculateLeaf(action.participationInfo) + ); + counterStorage.updateLeaf( + counterStorage.calculateLevel1Index(action.campaignId), + counterStorage.calculateLeaf(Field(i + 1)) + ); - console.log('DONE'); - } - - let tx = await Mina.transaction( - { - sender: feePayer.key.publicKey, - fee: feePayer.fee, - nonce: feePayer.nonce++, - }, - () => { - participationContract.rollup(proof); + console.log('DONE'); } - ); - await proveAndSend(tx, feePayer.key, 'ProjectContract', 'rollup'); + + let tx = await Mina.transaction( + { + sender: feePayer.key.publicKey, + fee: feePayer.fee, + nonce: feePayer.nonce++, + }, + () => { + participationContract.rollup(proof); + } + ); + await proveAndSend(tx, feePayer.key, 'ProjectContract', 'rollup'); } main() - .then() - .catch((err) => { - console.error(err); - process.exit(1); - }); + .then() + .catch((err) => { + console.error(err); + process.exit(1); + });