Skip to content

Commit 14af84d

Browse files
committed
Update contract Funding
1 parent a1b1d9f commit 14af84d

File tree

5 files changed

+111
-39
lines changed

5 files changed

+111
-39
lines changed

src/contracts/Funding.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
EMPTY_LEVEL_1_TREE,
3838
ValueStorage,
3939
RequestIdStorage,
40+
TotalFundStorage,
4041
} from './FundingStorage.js';
4142

4243
const DefaultLevel1Root = EMPTY_LEVEL_1_TREE().getRoot();
@@ -53,6 +54,7 @@ export class RequestSent extends Struct({
5354
requestId: Field,
5455
sumR: DKG_Contracts.Request.RequestVector,
5556
sumM: DKG_Contracts.Request.RequestVector,
57+
totalFundAmount: Field,
5658
}) {
5759
static fromFields(action: Field[]): RequestSent {
5860
return super.fromFields(action) as RequestSent;
@@ -77,6 +79,7 @@ export class FundingAction extends Struct({
7779
campaignId: Field,
7880
R: DKG_Contracts.Request.RequestVector,
7981
M: DKG_Contracts.Request.RequestVector,
82+
fundAmount: Field,
8083
}) {
8184
hash(): Field {
8285
return Poseidon.hash(FundingAction.toFields(this));
@@ -171,9 +174,8 @@ export class RollupActionsOutput extends Struct({
171174
campaignId: Field,
172175
sum_R: DKG_Contracts.Request.RequestVector,
173176
sum_M: DKG_Contracts.Request.RequestVector,
174-
// TODO: make that we can increase number of investor
177+
totalFundAmount: Field,
175178
initialStatusRoot: Field,
176-
finalStatusRoot: Field,
177179
}) {
178180
hash(): Field {
179181
return Poseidon.hash(RollupActionsOutput.toFields(this));
@@ -211,12 +213,7 @@ export const CreateRollupProof = ZkProgram({
211213
Field(ActionStatus.REDUCED)
212214
);
213215
key.assertEquals(actionState);
214-
root.assertEquals(preProof.publicOutput.finalStatusRoot);
215-
216-
// Update satus to ROLL_UPED
217-
let [newRoot] = rollupStatusWitness.computeRootAndKey(
218-
Field(ActionStatus.ROLL_UPED)
219-
);
216+
root.assertEquals(preProof.publicOutput.initialStatusRoot);
220217

221218
let sum_R = preProof.publicOutput.sum_R;
222219
let sum_M = preProof.publicOutput.sum_M;
@@ -232,12 +229,16 @@ export const CreateRollupProof = ZkProgram({
232229
);
233230
}
234231

232+
let newTotalFund = preProof.publicOutput.totalFundAmount.add(
233+
action.fundAmount
234+
);
235+
235236
return new RollupActionsOutput({
236237
campaignId: campaignId,
237238
sum_R,
238239
sum_M,
240+
totalFundAmount: newTotalFund,
239241
initialStatusRoot: preProof.publicOutput.initialStatusRoot,
240-
finalStatusRoot: newRoot,
241242
});
242243
},
243244
},
@@ -258,8 +259,8 @@ export const CreateRollupProof = ZkProgram({
258259
sum_M: DKG_Contracts.Request.RequestVector.empty(
259260
maxInvestorSize
260261
),
262+
totalFundAmount: Field(0),
261263
initialStatusRoot,
262-
finalStatusRoot: initialStatusRoot,
263264
});
264265
},
265266
},
@@ -273,6 +274,7 @@ export class FundingContract extends SmartContract {
273274
@state(Field) actionStatus = State<Field>();
274275
@state(Field) R_root = State<Field>(); // campaignId -> sum R
275276
@state(Field) M_root = State<Field>(); // campaignId -> sum M
277+
@state(Field) totalFundAmount_root = State<Field>(); // campaignId -> total fubnd amount
276278
@state(Field) requestId_root = State<Field>(); // campaignId -> requestId
277279
@state(Field) zkApps = State<Field>();
278280

@@ -288,6 +290,7 @@ export class FundingContract extends SmartContract {
288290
this.actionStatus.set(EMPTY_REDUCE_MT().getRoot());
289291
this.R_root.set(DefaultLevel1Root);
290292
this.M_root.set(DefaultLevel1Root);
293+
this.totalFundAmount_root.set(DefaultLevel1Root);
291294
this.requestId_root.set(DefaultLevel1Root);
292295
}
293296

@@ -387,6 +390,7 @@ export class FundingContract extends SmartContract {
387390
campaignId: fundingInput.campaignId,
388391
R,
389392
M,
393+
fundAmount: totalMinaInvest,
390394
})
391395
);
392396

@@ -416,14 +420,16 @@ export class FundingContract extends SmartContract {
416420
this.emitEvent(EventEnum.ACTIONS_REDUCED, lastActionState);
417421
}
418422

419-
// TODO: adding N, T to check REQUEST_MAX_SIZE by interact with Committee contract
420-
// TODO: checking Campaign contract config -> committeeId and keyId
423+
// TODO: add time condition to check rollup
424+
// TODO: adding N, T to check REQUEST_MAX_SIZE by interact with Committee contract (???)
425+
// TODO: checking Campaign contract config -> committeeId and keyId is valid
421426
@method rollupRequest(
422427
proof: ProofRollupAction,
423428
committeeId: Field,
424429
keyId: Field,
425430
R_witness: Level1Witness,
426431
M_witness: Level1Witness,
432+
totalFundAmount_witness: Level1Witness,
427433
requestId_witness: Level1Witness,
428434
requestZkAppRef: ZkAppRef
429435
) {
@@ -440,26 +446,34 @@ export class FundingContract extends SmartContract {
440446
let R_root = this.R_root.getAndRequireEquals();
441447
let M_root = this.M_root.getAndRequireEquals();
442448
let requestId_root = this.requestId_root.getAndRequireEquals();
449+
let totalFundAmount_root =
450+
this.totalFundAmount_root.getAndRequireEquals();
443451
let actionStatus = this.actionStatus.getAndRequireEquals();
444452

445453
actionStatus.assertEquals(proof.publicOutput.initialStatusRoot);
454+
446455
let R_key = R_witness.calculateIndex();
447456
let old_R_root = R_witness.calculateRoot(Field(0));
448457
let M_key = M_witness.calculateIndex();
449458
let old_M_root = M_witness.calculateRoot(Field(0));
450459
let requestId_key = requestId_witness.calculateIndex();
451460
let old_requestId_root = requestId_witness.calculateRoot(Field(0));
461+
let totalFundAmount_key = totalFundAmount_witness.calculateIndex();
462+
let old_totalFundAmount_root = totalFundAmount_witness.calculateRoot(
463+
Field(0)
464+
);
452465

453466
R_key.assertEquals(proof.publicOutput.campaignId);
454467
M_key.assertEquals(proof.publicOutput.campaignId);
455468
requestId_key.assertEquals(proof.publicOutput.campaignId);
469+
totalFundAmount_key.assertEquals(proof.publicOutput.campaignId);
456470

457471
R_root.assertEquals(old_R_root);
458472
M_root.assertEquals(old_M_root);
459473
requestId_root.assertEquals(old_requestId_root);
474+
totalFundAmount_root.assertEquals(old_totalFundAmount_root);
460475

461476
// TODO: check number of investor
462-
// maybe add time in the future
463477
let new_R_root = R_witness.calculateRoot(
464478
ValueStorage.calculateLeaf(proof.publicOutput.sum_R)
465479
);
@@ -469,14 +483,17 @@ export class FundingContract extends SmartContract {
469483
let new_requestId_root = requestId_witness.calculateRoot(
470484
RequestIdStorage.calculateLeaf(requestId)
471485
);
486+
let new_totalFundAmount_root = totalFundAmount_witness.calculateRoot(
487+
TotalFundStorage.calculateLeaf(proof.publicOutput.totalFundAmount)
488+
);
472489

473490
// update on-chain state
474491
this.R_root.set(new_R_root);
475492
this.M_root.set(new_M_root);
476493
this.requestId_root.set(new_requestId_root);
477-
this.actionStatus.set(proof.publicOutput.finalStatusRoot);
494+
this.totalFundAmount_root.set(new_totalFundAmount_root);
478495

479-
// TODO: request to Request contract
496+
// Request to Request contract
480497
// Verify zkApp references
481498
let zkApps = this.zkApps.getAndRequireEquals();
482499

@@ -506,6 +523,7 @@ export class FundingContract extends SmartContract {
506523
requestId,
507524
sumR: proof.publicOutput.sum_R,
508525
sumM: proof.publicOutput.sum_M,
526+
totalFundAmount: proof.publicOutput.totalFundAmount,
509527
})
510528
);
511529
}

src/contracts/FundingStorage.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,23 @@ export class RequestIdStorage extends FundingStorage<RequestIdLeaf> {
124124
return RequestIdStorage.calculateLevel1Index(campaignId);
125125
}
126126
}
127+
128+
export type TotalFundLeaf = Field;
129+
130+
export class TotalFundStorage extends FundingStorage<RequestIdLeaf> {
131+
static calculateLeaf(totalFundAmount: TotalFundLeaf): Field {
132+
return totalFundAmount;
133+
}
134+
135+
calculateLeaf(totalFundAmount: TotalFundLeaf): Field {
136+
return TotalFundStorage.calculateLeaf(totalFundAmount);
137+
}
138+
139+
static calculateLevel1Index(campaignId: Field): Field {
140+
return campaignId;
141+
}
142+
143+
calculateLevel1Index(campaignId: Field): Field {
144+
return TotalFundStorage.calculateLevel1Index(campaignId);
145+
}
146+
}

src/contracts/SharedStorage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ export function getZkAppRef(
122122
export const enum ActionStatus {
123123
NOT_EXISTED,
124124
REDUCED,
125-
ROLL_UPED,
126125
}
127126

128127
export class ReduceStorage {

src/scripts/Funding.test.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import {
2828
FundingInput,
2929
FundingAction,
3030
} from '../contracts/Funding.js';
31-
import { RequestIdStorage, ValueStorage } from '../contracts/FundingStorage.js';
31+
import {
32+
RequestIdStorage,
33+
TotalFundStorage,
34+
ValueStorage,
35+
} from '../contracts/FundingStorage.js';
3236
import { Key, Config } from './helper/config.js';
3337
import {
3438
AddressStorage,
@@ -64,6 +68,7 @@ describe('Funding', () => {
6468
let sumRStorage = new ValueStorage();
6569
let sumMStorage = new ValueStorage();
6670
let requestIdStorage = new RequestIdStorage();
71+
let totalFundStorage = new TotalFundStorage();
6772
let fundingAddressStorage = new AddressStorage();
6873
let fundingAction: FundingAction[] = [];
6974
let fundingInput: FundingInput[];
@@ -233,11 +238,24 @@ describe('Funding', () => {
233238

234239
let { R, M } = result!;
235240

241+
let dimension = fundingInput[i].secretVector.length;
242+
let totalMinaInvest = Provable.witness(Field, () => {
243+
let curSum = 0n;
244+
for (let j = 0; j < dimension.toBigInt(); j++) {
245+
curSum += fundingInput[i].secretVector
246+
.get(Field(j))
247+
.toScalar()
248+
.toBigInt();
249+
}
250+
return Field(curSum);
251+
});
252+
236253
fundingAction.push(
237254
new FundingAction({
238255
campaignId: fundingInput[i].campaignId,
239256
R,
240257
M,
258+
fundAmount: totalMinaInvest,
241259
})
242260
);
243261
}
@@ -315,14 +333,6 @@ describe('Funding', () => {
315333
fundingActionStates[index + 1 + i]
316334
)
317335
);
318-
319-
// update storage:
320-
fundingReduceStorage.updateLeaf(
321-
fundingReduceStorage.calculateIndex(
322-
fundingActionStates[index + 1 + i]
323-
),
324-
fundingReduceStorage.calculateLeaf(ActionStatus.ROLL_UPED)
325-
);
326336
}
327337

328338
tx = await Mina.transaction(feePayerKey.publicKey, () => {
@@ -345,6 +355,11 @@ describe('Funding', () => {
345355
fundingAction[0].campaignId
346356
)
347357
),
358+
totalFundStorage.getLevel1Witness(
359+
requestIdStorage.calculateLevel1Index(
360+
fundingAction[0].campaignId
361+
)
362+
),
348363
fundingAddressStorage.getZkAppRef(
349364
ZkAppEnum.REQUEST,
350365
contracts[Contract.REQUEST].contract.address

src/scripts/Platform.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ import {
6565
FundingAction,
6666
FundingInput,
6767
} from '../contracts/Funding.js';
68-
import { RequestIdStorage, ValueStorage } from '../contracts/FundingStorage.js';
68+
import {
69+
RequestIdStorage,
70+
ValueStorage,
71+
TotalFundStorage,
72+
} from '../contracts/FundingStorage.js';
6973
import {
7074
ParticipationContract,
7175
JoinCampaign,
@@ -181,6 +185,7 @@ async function main() {
181185
let fundingReduceStorage = new ReduceStorage();
182186
let sumRStorage = new ValueStorage();
183187
let sumMStorage = new ValueStorage();
188+
let totalFundStorage = new TotalFundStorage();
184189
let requestIdStorage = new RequestIdStorage();
185190
let fundingAddressStorage = new AddressStorage();
186191
let fundingAction: FundingAction[] = [];
@@ -857,11 +862,24 @@ async function main() {
857862

858863
let { R, M } = result!;
859864

865+
let dimension = fundingInput[i].secretVector.length;
866+
let totalMinaInvest = Provable.witness(Field, () => {
867+
let curSum = 0n;
868+
for (let j = 0; j < dimension.toBigInt(); j++) {
869+
curSum += fundingInput[i].secretVector
870+
.get(Field(j))
871+
.toScalar()
872+
.toBigInt();
873+
}
874+
return Field(curSum);
875+
});
876+
860877
fundingAction.push(
861878
new FundingAction({
862879
campaignId: fundingInput[i].campaignId,
863880
R,
864881
M,
882+
fundAmount: totalMinaInvest,
865883
})
866884
);
867885
}
@@ -930,14 +948,6 @@ async function main() {
930948
fundingActionStates[index + 1 + i]
931949
)
932950
);
933-
934-
// update storage:
935-
fundingReduceStorage.updateLeaf(
936-
fundingReduceStorage.calculateIndex(
937-
fundingActionStates[index + 1 + i]
938-
),
939-
fundingReduceStorage.calculateLeaf(ActionStatus.ROLL_UPED)
940-
);
941951
}
942952

943953
tx = await Mina.transaction(
@@ -962,6 +972,11 @@ async function main() {
962972
fundingAction[0].campaignId
963973
)
964974
),
975+
totalFundStorage.getLevel1Witness(
976+
requestIdStorage.calculateLevel1Index(
977+
fundingAction[0].campaignId
978+
)
979+
),
965980
fundingAddressStorage.getZkAppRef(
966981
ZkAppEnum.REQUEST,
967982
contracts[Contract.REQUEST].contract.address
@@ -1090,12 +1105,17 @@ async function main() {
10901105
Account(projects[i].publicKey).balance.get()
10911106
);
10921107
tx = await Mina.transaction(
1093-
{ sender: projects[i].publicKey, fee },
1094-
() => {
1095-
treasuryContract.claimFund(claimFundInput[i]);
1096-
}
1108+
{ sender: projects[i].publicKey, fee },
1109+
() => {
1110+
treasuryContract.claimFund(claimFundInput[i]);
1111+
}
1112+
);
1113+
await proveAndSend(
1114+
tx,
1115+
[projects[i]],
1116+
Contract.TREASURY,
1117+
'claimFund'
10971118
);
1098-
await proveAndSend(tx, [projects[i]], Contract.TREASURY, 'claimFund');
10991119
let balanceAfter = Number(
11001120
Account(projects[i].publicKey).balance.get()
11011121
);

0 commit comments

Comments
 (0)