@claritydao/midnight-agora-contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,331 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma language_version 0.18.0;
3
+
4
+ module ProposalModule {
5
+ import CompactStandardLibrary;
6
+ import '../utils/TypesModule';
7
+ import '../DAO/StakeModule';
8
+ import '../utils/DivModule';
9
+
10
+ // Ledger state for proposals
11
+ export ledger proposals: Map<Uint<64>, Proposal>;
12
+
13
+ // Ledger state for vote counts (tracked in proposal itself)
14
+
15
+ // Ledger state for user votes per proposal (to prevent double voting)
16
+ // Using hash of proposalId + voter as unique key
17
+ export ledger userVotes: Map<Bytes<32>, VoteRecord>;
18
+
19
+ // Ledger state for proposal counter
20
+ export ledger proposalCounter: Counter;
21
+
22
+ // Circuit to create a new proposal (validations moved to Governor)
23
+ export circuit createProposal(
24
+ creator: Bytes<32>,
25
+ title: Opaque<'string'>,
26
+ description: Opaque<'string'>,
27
+ effectId: Uint<64>,
28
+ proof: ProofOfStake,
29
+ votingStartTime: Uint<64>,
30
+ votingEndTime: Uint<64>
31
+ ): Uint<64> {
32
+ assert(proof.userId == disclose(creator), "Creator must match proof");
33
+
34
+
35
+ // Get governance config for proposal setup
36
+ const config = getGovernanceConfig();
37
+
38
+ // Generate proposal ID
39
+ const proposalId = generateProposalId();
40
+
41
+ // Create proposal
42
+ const proposal = Proposal {
43
+ id: proposalId,
44
+ creator: disclose(creator),
45
+ title: disclose(title),
46
+ description: disclose(description),
47
+ effectId: disclose(effectId),
48
+ status: ProposalStatus.Active,
49
+ votingStartTime: votingStartTime,
50
+ votingEndTime: votingEndTime,
51
+ votesFor: 0 as Uint<64>,
52
+ votesAgainst: 0 as Uint<64>,
53
+ votesAbstain: 0 as Uint<64>,
54
+ quorumRequired: config.quorumThreshold,
55
+ threshold: 51 as Uint<64>, // 51% threshold for passing
56
+ createdAt: votingStartTime,
57
+ finalizedAt: 0 as Uint<64>
58
+ };
59
+
60
+ proposals.insert(proposalId, disclose(proposal));
61
+
62
+ return proposalId;
63
+ }
64
+
65
+ // Circuit to cast a vote on a proposal (validations moved to Governor)
66
+ export circuit castVote(
67
+ proposalId: Uint<64>,
68
+ voter: Bytes<32>,
69
+ choice: VoteChoice,
70
+ proof: ProofOfStake,
71
+ votingPower: Uint<64>
72
+ ): [] {
73
+ assert(proof.userId == disclose(voter), "Voter must match proof");
74
+
75
+ // NOTE: Most validations moved to Governor for proper ledger state access
76
+ // This function focuses only on vote recording logic
77
+
78
+ assert(proposals.member(disclose(proposalId)), "Proposal does not exist");
79
+ const proposal = proposals.lookup(disclose(proposalId));
80
+ const currentTime = getCurrentTime();
81
+
82
+ // Check for double voting (this is safe to keep here as it's module-specific)
83
+ const voteKey = createVoteKey(disclose(proposalId), disclose(voter));
84
+ assert(!userVotes.member(voteKey), "User has already voted on this proposal");
85
+
86
+
87
+ // Create vote record
88
+ const voteRecord = VoteRecord {
89
+ proposalId: disclose(proposalId),
90
+ voter: disclose(voter),
91
+ choice: disclose(choice),
92
+ power: votingPower,
93
+ timestamp: currentTime
94
+ };
95
+
96
+ // Add vote to user vote tracking
97
+ userVotes.insert(voteKey, disclose(voteRecord));
98
+
99
+ // Update proposal vote counts
100
+ const updatedProposal = updateProposalVotes(proposal, disclose(choice), votingPower);
101
+ proposals.insert(disclose(proposalId), updatedProposal);
102
+ }
103
+
104
+ // Circuit to finalize a proposal (validations moved to Governor)
105
+ export circuit finalizeProposal(proposalId: Uint<64>, proof: ProofOfGovernorApproval): [] {
106
+ assert(proof.proposalId == disclose(proposalId), "Proposal ID mismatch");
107
+
108
+ // NOTE: Proposal existence, status, timing, and lifetime validations moved to Governor contract
109
+ // This allows proper access to ledger state
110
+
111
+ const proposal = proposals.lookup(disclose(proposalId));
112
+ const currentTime = getCurrentTime();
113
+
114
+ // Determine final status based on vote counts
115
+ const totalVotes = (proposal.votesFor + proposal.votesAgainst + proposal.votesAbstain) as Uint<64>;
116
+ const quorumMet = totalVotes >= proposal.quorumRequired;
117
+
118
+ // Calculate threshold percentage (threshold is stored as percentage, e.g., 51 = 51%)
119
+ const thresholdVotes = div((totalVotes * proposal.threshold) as Uint<128>, 100 as Uint<128>, default<Vector<1000, Uint<1>>>).quotient as Uint<64>;
120
+ const thresholdMet = proposal.votesFor >= thresholdVotes;
121
+
122
+ // Update proposal status and finalization time
123
+ const updatedProposal = Proposal {
124
+ id: proposal.id,
125
+ creator: proposal.creator,
126
+ title: proposal.title,
127
+ description: proposal.description,
128
+ effectId: proposal.effectId,
129
+ status: quorumMet && thresholdMet ? ProposalStatus.Succeeded : ProposalStatus.Defeated,
130
+ votingStartTime: proposal.votingStartTime,
131
+ votingEndTime: proposal.votingEndTime,
132
+ votesFor: proposal.votesFor,
133
+ votesAgainst: proposal.votesAgainst,
134
+ votesAbstain: proposal.votesAbstain,
135
+ quorumRequired: proposal.quorumRequired,
136
+ threshold: proposal.threshold,
137
+ createdAt: proposal.createdAt,
138
+ finalizedAt: currentTime
139
+ };
140
+
141
+ proposals.insert(disclose(proposalId), updatedProposal);
142
+ }
143
+
144
+ // Circuit to queue a proposal for execution (validations moved to Governor)
145
+ export circuit queueProposal(proposalId: Uint<64>, proof: ProofOfGovernorApproval): [] {
146
+ assert(proof.proposalId == disclose(proposalId), "Proposal ID mismatch");
147
+
148
+ // NOTE: Proposal existence, status, execution delay, and grace period validations moved to Governor contract
149
+ // This allows proper access to ledger state
150
+
151
+ const proposal = proposals.lookup(disclose(proposalId));
152
+ const updatedProposal = Proposal {
153
+ id: proposal.id,
154
+ creator: proposal.creator,
155
+ title: proposal.title,
156
+ description: proposal.description,
157
+ effectId: proposal.effectId,
158
+ status: ProposalStatus.Queued,
159
+ votingStartTime: proposal.votingStartTime,
160
+ votingEndTime: proposal.votingEndTime,
161
+ votesFor: proposal.votesFor,
162
+ votesAgainst: proposal.votesAgainst,
163
+ votesAbstain: proposal.votesAbstain,
164
+ quorumRequired: proposal.quorumRequired,
165
+ threshold: proposal.threshold,
166
+ createdAt: proposal.createdAt,
167
+ finalizedAt: proposal.finalizedAt
168
+ };
169
+ proposals.insert(disclose(proposalId), updatedProposal);
170
+ }
171
+
172
+ // Circuit to mark proposal as executed (validations moved to Governor)
173
+ export circuit markProposalExecuted(proposalId: Uint<64>, proof: ProofOfEffectExecution): [] {
174
+ // NOTE: Proposal existence, status, grace period, and execution window validations moved to Governor contract
175
+ // This allows proper access to ledger state
176
+
177
+ const proposal = proposals.lookup(disclose(proposalId));
178
+ const updatedProposal = Proposal {
179
+ id: proposal.id,
180
+ creator: proposal.creator,
181
+ title: proposal.title,
182
+ description: proposal.description,
183
+ effectId: proposal.effectId,
184
+ status: ProposalStatus.Executed,
185
+ votingStartTime: proposal.votingStartTime,
186
+ votingEndTime: proposal.votingEndTime,
187
+ votesFor: proposal.votesFor,
188
+ votesAgainst: proposal.votesAgainst,
189
+ votesAbstain: proposal.votesAbstain,
190
+ quorumRequired: proposal.quorumRequired,
191
+ threshold: proposal.threshold,
192
+ createdAt: proposal.createdAt,
193
+ finalizedAt: proposal.finalizedAt
194
+ };
195
+ proposals.insert(disclose(proposalId), updatedProposal);
196
+ }
197
+
198
+ // Circuit to cancel a proposal (validations moved to Governor)
199
+ export circuit cancelProposal(proposalId: Uint<64>, proof: ProofOfGovernorApproval): [] {
200
+ assert(proof.proposalId == disclose(proposalId), "Proposal ID mismatch");
201
+
202
+ // NOTE: Proposal existence and status validations moved to Governor contract
203
+ // This allows proper access to ledger state
204
+
205
+ const proposal = proposals.lookup(disclose(proposalId));
206
+ const updatedProposal = Proposal {
207
+ id: proposal.id,
208
+ creator: proposal.creator,
209
+ title: proposal.title,
210
+ description: proposal.description,
211
+ effectId: proposal.effectId,
212
+ status: ProposalStatus.Cancelled,
213
+ votingStartTime: proposal.votingStartTime,
214
+ votingEndTime: proposal.votingEndTime,
215
+ votesFor: proposal.votesFor,
216
+ votesAgainst: proposal.votesAgainst,
217
+ votesAbstain: proposal.votesAbstain,
218
+ quorumRequired: proposal.quorumRequired,
219
+ threshold: proposal.threshold,
220
+ createdAt: proposal.createdAt,
221
+ finalizedAt: proposal.finalizedAt
222
+ };
223
+ proposals.insert(disclose(proposalId), updatedProposal);
224
+ }
225
+
226
+ // Circuit to get proposal details
227
+ export circuit getProposal(proposalId: Uint<64>): Proposal {
228
+ assert(proposals.member(disclose(proposalId)), "Proposal does not exist");
229
+ return proposals.lookup(disclose(proposalId));
230
+ }
231
+
232
+ // Circuit to get vote count for a proposal
233
+ export circuit getProposalVoteCount(proposalId: Uint<64>): Uint<64> {
234
+ assert(proposals.member(disclose(proposalId)), "Proposal does not exist");
235
+ const proposal = proposals.lookup(disclose(proposalId));
236
+ return (proposal.votesFor + proposal.votesAgainst + proposal.votesAbstain) as Uint<64>;
237
+ }
238
+
239
+ // Circuit to update proposal effect ID
240
+ export circuit updateProposalEffect(proposalId: Uint<64>, effectId: Uint<64>): [] {
241
+ assert(proposals.member(disclose(proposalId)), "Proposal does not exist");
242
+ const proposal = proposals.lookup(disclose(proposalId));
243
+ const updatedProposal = Proposal {
244
+ id: proposal.id,
245
+ creator: proposal.creator,
246
+ title: proposal.title,
247
+ description: proposal.description,
248
+ effectId: disclose(effectId),
249
+ status: proposal.status,
250
+ votingStartTime: proposal.votingStartTime,
251
+ votingEndTime: proposal.votingEndTime,
252
+ votesFor: proposal.votesFor,
253
+ votesAgainst: proposal.votesAgainst,
254
+ votesAbstain: proposal.votesAbstain,
255
+ quorumRequired: proposal.quorumRequired,
256
+ threshold: proposal.threshold,
257
+ createdAt: proposal.createdAt,
258
+ finalizedAt: proposal.finalizedAt
259
+ };
260
+ proposals.insert(disclose(proposalId), updatedProposal);
261
+ }
262
+
263
+ // Helper functions
264
+ export circuit generateProposalId(): Uint<64> {
265
+ proposalCounter.increment(1);
266
+ return proposalCounter.read();
267
+ }
268
+
269
+ export pure circuit createVoteKey(proposalId: Uint<64>, voter: Bytes<32>): Bytes<32> {
270
+ // Use persistentCommit with proposalId as value and voter as randomness
271
+ return persistentCommit<Uint<64>>(proposalId, voter);
272
+ }
273
+
274
+ export pure circuit updateProposalVotes(proposal: Proposal, choice: VoteChoice, power: Uint<64>): Proposal {
275
+ if (choice == VoteChoice.For) {
276
+ return Proposal {
277
+ id: proposal.id,
278
+ creator: proposal.creator,
279
+ title: proposal.title,
280
+ description: proposal.description,
281
+ effectId: proposal.effectId,
282
+ status: proposal.status,
283
+ votingStartTime: proposal.votingStartTime,
284
+ votingEndTime: proposal.votingEndTime,
285
+ votesFor: (proposal.votesFor + power) as Uint<64>,
286
+ votesAgainst: proposal.votesAgainst,
287
+ votesAbstain: proposal.votesAbstain,
288
+ quorumRequired: proposal.quorumRequired,
289
+ threshold: proposal.threshold,
290
+ createdAt: proposal.createdAt,
291
+ finalizedAt: proposal.finalizedAt
292
+ };
293
+ } else if (choice == VoteChoice.Against) {
294
+ return Proposal {
295
+ id: proposal.id,
296
+ creator: proposal.creator,
297
+ title: proposal.title,
298
+ description: proposal.description,
299
+ effectId: proposal.effectId,
300
+ status: proposal.status,
301
+ votingStartTime: proposal.votingStartTime,
302
+ votingEndTime: proposal.votingEndTime,
303
+ votesFor: proposal.votesFor,
304
+ votesAgainst: (proposal.votesAgainst + power) as Uint<64>,
305
+ votesAbstain: proposal.votesAbstain,
306
+ quorumRequired: proposal.quorumRequired,
307
+ threshold: proposal.threshold,
308
+ createdAt: proposal.createdAt,
309
+ finalizedAt: proposal.finalizedAt
310
+ };
311
+ } else {
312
+ return Proposal {
313
+ id: proposal.id,
314
+ creator: proposal.creator,
315
+ title: proposal.title,
316
+ description: proposal.description,
317
+ effectId: proposal.effectId,
318
+ status: proposal.status,
319
+ votingStartTime: proposal.votingStartTime,
320
+ votingEndTime: proposal.votingEndTime,
321
+ votesFor: proposal.votesFor,
322
+ votesAgainst: proposal.votesAgainst,
323
+ votesAbstain: (proposal.votesAbstain + power) as Uint<64>,
324
+ quorumRequired: proposal.quorumRequired,
325
+ threshold: proposal.threshold,
326
+ createdAt: proposal.createdAt,
327
+ finalizedAt: proposal.finalizedAt
328
+ };
329
+ }
330
+ }
331
+ }
@@ -0,0 +1,301 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma language_version 0.18.0;
3
+
4
+ module StakeModule {
5
+
6
+ import CompactStandardLibrary;
7
+ import '../utils/TypesModule';
8
+
9
+ // Ledger state for stake records
10
+ export ledger stakes: Map<Bytes<32>, StakeRecord>;
11
+
12
+ // Ledger state for delegation mapping
13
+ export ledger delegations: Map<DelegationKey, DelegationRecord>;
14
+
15
+ // Ledger state for total stake amounts
16
+ export ledger totalStakes: Map<Bytes<32>, Uint<64>>;
17
+
18
+ // Ledger state for governance configuration
19
+ export ledger governanceConfig: GovernanceConfig;
20
+
21
+ // Circuit to register stake (validations moved to Governor)
22
+ export circuit registerStake(userId: Bytes<32>, amount: Uint<64>, proof: ProofOfStake): [] {
23
+ assert(proof.userId == userId, "User ID must match proof");
24
+ assert(proof.amount == amount, "Amount must match proof");
25
+
26
+ // NOTE: Minimum stake amount validation moved to Governor contract
27
+ // This allows proper access to ledger state
28
+
29
+ const currentTime = getCurrentTime();
30
+ const userIdKey = disclose(userId);
31
+ const hasExistingStake = stakes.member(userIdKey);
32
+
33
+ if (!hasExistingStake) {
34
+ // New stake record
35
+ const stakeRecord = StakeRecord {
36
+ userId: userIdKey,
37
+ amount: disclose(amount),
38
+ delegatedTo: userIdKey,
39
+ status: DelegationStatus.SelfStaked,
40
+ stakedAt: currentTime
41
+ };
42
+ stakes.insert(userIdKey, stakeRecord);
43
+ } else {
44
+ // Update existing stake
45
+ const existingStake = stakes.lookup(userIdKey);
46
+ const updatedStake = StakeRecord {
47
+ userId: existingStake.userId,
48
+ amount: (existingStake.amount + disclose(amount)) as Uint<64>,
49
+ delegatedTo: existingStake.delegatedTo,
50
+ status: existingStake.status,
51
+ stakedAt: existingStake.stakedAt
52
+ };
53
+ stakes.insert(userIdKey, updatedStake);
54
+ }
55
+
56
+ // Update total stakes
57
+ const currentTotal = totalStakes.member(userIdKey) ? totalStakes.lookup(userIdKey) : 0;
58
+ totalStakes.insert(userIdKey, (currentTotal + disclose(amount)) as Uint<64>);
59
+ }
60
+
61
+ // Circuit to withdraw stake (validations moved to Governor)
62
+ export circuit withdrawStake(userId: Bytes<32>, amount: Uint<64>, proof: ProofOfStake): [] {
63
+ assert(proof.userId == userId, "User ID must match proof");
64
+ assert(proof.amount == amount, "Amount must match proof");
65
+
66
+ // NOTE: Sufficient stake and withdrawal conditions validations moved to Governor contract
67
+ // This allows proper access to ledger state
68
+
69
+ const currentTime = getCurrentTime();
70
+ const userIdKey = disclose(userId);
71
+ const stakeRecord = stakes.lookup(userIdKey);
72
+
73
+ const updatedStake = StakeRecord {
74
+ userId: stakeRecord.userId,
75
+ amount: (stakeRecord.amount - disclose(amount)) as Uint<64>,
76
+ delegatedTo: stakeRecord.delegatedTo,
77
+ status: stakeRecord.status,
78
+ stakedAt: stakeRecord.stakedAt
79
+ };
80
+ stakes.insert(userIdKey, updatedStake);
81
+
82
+ // Update total stakes
83
+ const currentTotal = totalStakes.member(userIdKey) ? totalStakes.lookup(userIdKey) : 0;
84
+ totalStakes.insert(userIdKey, (currentTotal - disclose(amount)) as Uint<64>);
85
+ }
86
+
87
+ // Circuit to delegate stake (validations moved to Governor)
88
+ export circuit delegateStake(
89
+ delegator: Bytes<32>,
90
+ delegatee: Bytes<32>,
91
+ amount: Uint<64>,
92
+ proof: ProofOfStake
93
+ ): [] {
94
+ assert(proof.userId == delegator, "Delegator must match proof");
95
+
96
+ // NOTE: Amount validation and sufficient stake validation moved to Governor contract
97
+ // This allows proper access to ledger state
98
+
99
+ const currentTime = getCurrentTime();
100
+ const delegationKey = DelegationKey {
101
+ delegator: disclose(delegator),
102
+ delegatee: disclose(delegatee)
103
+ };
104
+
105
+ // Update or create delegation record
106
+ if (delegations.member(delegationKey)) {
107
+ const existingDelegation = delegations.lookup(delegationKey);
108
+ const updatedDelegation = DelegationRecord {
109
+ delegator: existingDelegation.delegator,
110
+ delegatee: existingDelegation.delegatee,
111
+ amount: (existingDelegation.amount + disclose(amount)) as Uint<64>,
112
+ delegatedAt: currentTime,
113
+ status: DelegationStatus.Delegated
114
+ };
115
+ delegations.insert(delegationKey, updatedDelegation);
116
+ } else {
117
+ const newDelegation = DelegationRecord {
118
+ delegator: disclose(delegator),
119
+ delegatee: disclose(delegatee),
120
+ amount: disclose(amount),
121
+ delegatedAt: currentTime,
122
+ status: DelegationStatus.Delegated
123
+ };
124
+ delegations.insert(delegationKey, newDelegation);
125
+ }
126
+
127
+ // Update delegator's stake record
128
+ const delegatorStake = stakes.lookup(disclose(delegator));
129
+ const updatedDelegatorStake = StakeRecord {
130
+ userId: delegatorStake.userId,
131
+ amount: (delegatorStake.amount - disclose(amount)) as Uint<64>,
132
+ delegatedTo: disclose(delegatee),
133
+ status: DelegationStatus.Delegated,
134
+ stakedAt: delegatorStake.stakedAt
135
+ };
136
+ stakes.insert(disclose(delegator), updatedDelegatorStake);
137
+ }
138
+
139
+ // Circuit to undelegate stake (validations moved to Governor)
140
+ export circuit undelegateStake(
141
+ delegator: Bytes<32>,
142
+ delegatee: Bytes<32>,
143
+ amount: Uint<64>,
144
+ proof: ProofOfStake
145
+ ): [] {
146
+ assert(proof.userId == delegator, "Delegator must match proof");
147
+
148
+ // NOTE: Amount validation and sufficient delegation validation moved to Governor contract
149
+ // This allows proper access to ledger state
150
+
151
+ const delegationKey = DelegationKey {
152
+ delegator: disclose(delegator),
153
+ delegatee: disclose(delegatee)
154
+ };
155
+
156
+ // Update delegation record
157
+ const delegation = delegations.lookup(delegationKey);
158
+ const updatedDelegation = DelegationRecord {
159
+ delegator: delegation.delegator,
160
+ delegatee: delegation.delegatee,
161
+ amount: (delegation.amount - disclose(amount)) as Uint<64>,
162
+ delegatedAt: delegation.delegatedAt,
163
+ status: delegation.status
164
+ };
165
+ delegations.insert(delegationKey, updatedDelegation);
166
+
167
+ // Update delegator's stake record
168
+ const delegatorStake = stakes.lookup(disclose(delegator));
169
+ const updatedDelegatorStake = StakeRecord {
170
+ userId: delegatorStake.userId,
171
+ amount: (delegatorStake.amount + disclose(amount)) as Uint<64>,
172
+ delegatedTo: disclose(delegator),
173
+ status: DelegationStatus.SelfStaked,
174
+ stakedAt: delegatorStake.stakedAt
175
+ };
176
+ stakes.insert(disclose(delegator), updatedDelegatorStake);
177
+ }
178
+
179
+ // Circuit to get stake information
180
+ export circuit getStakeInfo(userId: Bytes<32>): StakeInfo {
181
+ const userIdKey = disclose(userId);
182
+ const stakeRecord = stakes.member(userIdKey) ? stakes.lookup(userIdKey) : StakeRecord {
183
+ userId: userIdKey,
184
+ amount: 0,
185
+ delegatedTo: userIdKey,
186
+ status: DelegationStatus.SelfStaked,
187
+ stakedAt: 0
188
+ };
189
+ const totalStake = totalStakes.member(userIdKey) ? totalStakes.lookup(userIdKey) : 0;
190
+ const votingPower = getTotalDelegatedPower(userIdKey);
191
+
192
+ return StakeInfo {
193
+ userId,
194
+ stakedAmount: stakeRecord.amount,
195
+ totalStake: totalStake,
196
+ votingPower: votingPower,
197
+ delegatedTo: stakeRecord.delegatedTo,
198
+ status: stakeRecord.status
199
+ };
200
+ }
201
+
202
+ // Circuit to get total delegated power for a user
203
+ export circuit getTotalDelegatedPower(userId: Bytes<32>): Uint<64> {
204
+ const userIdKey = disclose(userId);
205
+ const directStake = stakes.member(userIdKey) ? stakes.lookup(userIdKey) : StakeRecord {
206
+ userId: userIdKey,
207
+ amount: 0,
208
+ delegatedTo: userIdKey,
209
+ status: DelegationStatus.SelfStaked,
210
+ stakedAt: 0
211
+ };
212
+ const totalDelegated: Uint<64> = 0;
213
+
214
+ // In a real implementation, this would iterate through all delegations
215
+ // to this user. For now, we'll use a simplified approach.
216
+ return (directStake.amount + totalDelegated) as Uint<64>;
217
+ }
218
+
219
+ // Circuit to check if user has proposal threshold
220
+ export circuit hasProposalThreshold(userId: Bytes<32>): Boolean {
221
+ const votingPower = getTotalDelegatedPower(userId);
222
+ return votingPower >= governanceConfig.proposalThreshold;
223
+ }
224
+
225
+ // Circuit to check if user has voting power
226
+ export circuit hasVotingPower(userId: Bytes<32>): Boolean {
227
+ const votingPower = getTotalDelegatedPower(userId);
228
+ return votingPower > 0;
229
+ }
230
+
231
+ // Circuit to update governance configuration
232
+ export circuit updateGovernanceConfig(
233
+ newConfig: GovernanceConfig,
234
+ proof: ProofOfGovernorApproval
235
+ ): [] {
236
+ governanceConfig = disclose(newConfig);
237
+ }
238
+
239
+ // Circuit to get current governance configuration
240
+ export circuit getGovernanceConfig(): GovernanceConfig {
241
+ return governanceConfig;
242
+ }
243
+
244
+ // Circuit to get stake record
245
+ export circuit getStakeRecord(userId: Bytes<32>): StakeRecord {
246
+ const userIdKey = disclose(userId);
247
+ const hasStake = stakes.member(userIdKey);
248
+
249
+ if (!hasStake) {
250
+ // Return default stake record for non-existent users
251
+ return StakeRecord {
252
+ userId: userIdKey,
253
+ amount: 0,
254
+ delegatedTo: userIdKey,
255
+ status: DelegationStatus.SelfStaked,
256
+ stakedAt: 0
257
+ };
258
+ }
259
+
260
+ return stakes.lookup(userIdKey);
261
+ }
262
+
263
+ // Circuit to get delegation record
264
+ export circuit getDelegationRecord(
265
+ delegator: Bytes<32>,
266
+ delegatee: Bytes<32>
267
+ ): DelegationRecord {
268
+ const delegationKey = DelegationKey {
269
+ delegator: disclose(delegator),
270
+ delegatee: disclose(delegatee)
271
+ };
272
+
273
+ if (delegations.member(delegationKey)) {
274
+ return delegations.lookup(delegationKey);
275
+ } else {
276
+ // Return default delegation record for non-existent delegations
277
+ return DelegationRecord {
278
+ delegator: disclose(delegator),
279
+ delegatee: disclose(delegatee),
280
+ amount: 0,
281
+ delegatedAt: 0,
282
+ status: DelegationStatus.SelfStaked
283
+ };
284
+ }
285
+ }
286
+
287
+ // Helper function to validate stake withdrawal
288
+ export circuit validateStakeWithdrawal(
289
+ stakeRecord: StakeRecord,
290
+ amount: Uint<64>,
291
+ currentTime: Uint<64>
292
+ ): Boolean {
293
+ // Check if enough time has passed since staking
294
+ const stakingPeriod = governanceConfig.stakingPeriod;
295
+ const timeSinceStaking = (currentTime - stakeRecord.stakedAt) as Uint<64>;
296
+
297
+ return timeSinceStaking >= stakingPeriod;
298
+ }
299
+
300
+
301
+ }