diff --git a/src/scripts/interactions/funding/fund.ts b/src/scripts/interactions/funding/fund.ts index 5dfbb36..bfad6b3 100644 --- a/src/scripts/interactions/funding/fund.ts +++ b/src/scripts/interactions/funding/fund.ts @@ -75,16 +75,19 @@ async function main() { const campaignId = 1; const projectId = 1; + const committeePublicKey = PublicKey.fromBase58( + 'B62qph1Mj9atGbPUqDszvwFJ3LVGoXWkNjoDura5kjkK6Pw15UHHDZy' + ); // Compile programs // await compile(CreateReduceProof, cache); // await compile(CreateRollupProof, cache); // await compile(FundingContract, cache); - const fundingAddress = - 'B62qoGy5GKNRaa2P8uh4ppdRqnCVjNyHqVSsaD4JMcE6FawLRKmmN4u'; - const participationAddress = - 'B62qqnpYUoCsDYeK71o25roU13WYjPX1HZKQ47UzvzBScXA3n5eQQLi'; + const fundingAddress = process.env.BERKELEY_FUNDING_ADDRESS as string; + const participationAddress = process.env + .BERKELEY_PARTICIPATION_ADDRESS as string; + const treasuryAddress = process.env.BERKELEY_TREASURY_ADDRESS as string; const fundingContract = new FundingContract( PublicKey.fromBase58(fundingAddress) ); @@ -93,23 +96,8 @@ async function main() { await fetchZkAppState(fundingAddress); await fetchZkAppState(participationAddress); - // Project storage - let requestIdStorage = new RequestIdStorage(); - // Participation storage - let valueStorage = new ValueStorage(); + // Storage let fundingAddressStorage = new AddressStorage(addressMerkleTree); - console.log('Root: ', fundingAddressStorage.root); - - // Fetch storage trees - const projectsInCampaign = ( - await axios.get( - `https://api.auxo.fund/v0/campaigns/${campaignId}/projects` - ) - ).data; - - // Build storage - // IndexStorage - // RequestIdStorage // total fund 0.02 = 2e7 let secretVectors: CustomScalarArray[] = [ @@ -145,17 +133,12 @@ async function main() { let input = new FundingInput({ campaignId: Field(campaignId), // Publickey of a committee - committeePublicKey: PublicKey.fromBase58( - 'B62qph1Mj9atGbPUqDszvwFJ3LVGoXWkNjoDura5kjkK6Pw15UHHDZy' - ), + committeePublicKey: committeePublicKey, secretVector: secretVectors[0], random: randomsVectors[0], treasuryContract: fundingAddressStorage.getZkAppRef( ZkAppEnum.TREASURY, - // Treasury address - PublicKey.fromBase58( - 'B62qpKo4mxwp9eZqisM3KS2xHK8i4GMKJoeTt1768sWzi9TWXidioYK' - ) + PublicKey.fromBase58(treasuryAddress) ), }); diff --git a/src/scripts/interactions/funding/reduce.ts b/src/scripts/interactions/funding/reduce.ts new file mode 100644 index 0000000..8a95955 --- /dev/null +++ b/src/scripts/interactions/funding/reduce.ts @@ -0,0 +1,169 @@ +import fs from 'fs'; +import { + Cache, + Field, + Mina, + PrivateKey, + Provable, + PublicKey, + Reducer, + fetchAccount, + Scalar, +} from 'o1js'; +import { Config, JSONKey, Key } from '../../helper/config.js'; +import { + ContractList, + compile, + wait, + proveAndSend, +} from '../../helper/deploy.js'; +import { fetchActions, fetchZkAppState } from '../../helper/deploy.js'; +import { + CampaignContract, + CreateCampaign, + CreateCampaignInput, + CampaignAction, +} from '../../../contracts/Campaign.js'; +import { + InfoStorage as CampaignInfoStorage, + OwnerStorage, + StatusStorage, + ConfigStorage, + StatusEnum, +} from '../../../contracts/CampaignStorage.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, +} from '../../../contracts/ParticipationStorage.js'; +import { + MemberStorage, + Level2Witness, + EMPTY_LEVEL_2_TREE, +} from '../../../contracts/ProjectStorage.js'; +import axios from 'axios'; +import { IPFSHash, CustomScalar } from '@auxo-dev/auxo-libs'; +import { prepare } from '../prepare.js'; +import { + AddressStorage, + getZkAppRef, + ReduceStorage, + ActionStatus, +} from '../../../contracts/SharedStorage.js'; +import { + FundingContract, + CreateReduceProof, + CreateRollupProof, + FundingAction, + FundingInput, +} from '../../../contracts/Funding.js'; +import { + RequestIdStorage, + ValueStorage, +} from '../../../contracts/FundingStorage.js'; +import { ZkAppEnum } from '../../../constants.js'; +import { CustomScalarArray, ZkApp } from '@auxo-dev/dkg'; + +async function main() { + const { cache, feePayer, addressMerkleTree } = await prepare(); + + // Compile programs + await compile(CreateReduceProof, cache); + await compile(CreateRollupProof, cache); + await compile(FundingContract, cache); + + const fundingAddress = process.env.BERKELEY_FUNDING_ADDRESS as string; + const fundingContract = new FundingContract( + PublicKey.fromBase58(fundingAddress) + ); + + // Do this and state value of contract is fetched in Mina + await fetchZkAppState(fundingAddress); + + // Fetch storage + const actionStatus = ( + await axios.get('https://api.auxo.fund/v0/funding/reduce') + ).data; // Nam viet them cai nay nha, chua co dau t viet tam mau~ thoi + + // Build storage + let fundingReduceStorage = new ReduceStorage(); + for (let key in actionStatus) { + fundingReduceStorage.updateLeaf( + Field(key), + Field(actionStatus[key]['leaf']) + ); + } + + let reduceFundingProof = await CreateReduceProof.firstStep( + fundingContract.actionState.get(), + fundingContract.actionStatus.get() + ); + + const lastReduceAction = fundingContract.actionState.get(); + const rawAllActions = await fetchActions(fundingAddress); + + const allActions: FundingAction[] = rawAllActions.map((e) => { + let action: Field[] = e.actions[0].map((e) => Field(e)); + return FundingAction.fromFields(action); + }); + + const rawReduceActions = await fetchActions( + fundingAddress, + lastReduceAction + ); + const reduceAction: FundingAction[] = rawReduceActions.map((e) => { + let action: Field[] = e.actions[0].map((e) => Field(e)); + return FundingAction.fromFields(action); + }); + + let index = rawAllActions.findIndex((obj) => + Field(obj.hash).equals(lastReduceAction).toBoolean() + ); + + for (let i = 0; i < reduceAction.length; i++) { + console.log('Step', i); + reduceFundingProof = await CreateReduceProof.nextStep( + reduceFundingProof, + reduceAction[i], + fundingReduceStorage.getWitness( + Field(rawAllActions[index + 1 + i].hash) + ) + ); + + // update storage: + fundingReduceStorage.updateLeaf( + fundingReduceStorage.calculateIndex( + Field(rawAllActions[index + 1 + i].hash) + ), + fundingReduceStorage.calculateLeaf(ActionStatus.REDUCED) + ); + } + + let tx = await Mina.transaction( + { + sender: feePayer.key.publicKey, + fee: feePayer.fee, + nonce: feePayer.nonce++, + }, + () => { + fundingContract.reduce(reduceFundingProof); + } + ); + + await proveAndSend(tx, feePayer.key, 'funding', 'fund'); +} + +main() + .then() + .catch((err) => { + console.error(err); + process.exit(1); + });