@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,326 @@
1
+ // This file is part of midnightntwrk/agora-dao.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+
16
+ pragma language_version 0.18.0;
17
+
18
+ module TreasuryModule
19
+ {
20
+ import CompactStandardLibrary;
21
+ import '../utils/TypesModule';
22
+ import '../fungigleToken/FungibleToken';
23
+
24
+ // Ledger state for treasury balances
25
+ export ledger treasuryBalances: Map<Bytes<32>, TreasuryBalance>;
26
+
27
+ // Ledger state for authorized spenders
28
+ export ledger authorizedSpenders: Map<Bytes<32>, Boolean>;
29
+
30
+ // Ledger state for spending limits
31
+ export ledger spendingLimits: Map<Bytes<32>, Uint<64>>;
32
+
33
+ // Ledger state for total treasury value
34
+ export ledger totalTreasuryValue: Map<Bytes<32>, Uint<64>>;
35
+
36
+ // Ledger state for transaction counter (simplified approach)
37
+ export ledger transactionCounter: Counter;
38
+
39
+ // Circuit to deposit tokens into treasury (validations moved to Governor)
40
+ export circuit depositToTreasury(
41
+ token: Bytes<32>,
42
+ amount: Uint<64>,
43
+ depositor: Bytes<32>,
44
+ proof: ProofOfStake
45
+ ): [] {
46
+ assert(proof.userId == depositor, "Depositor must match proof");
47
+
48
+ // NOTE: Amount validation moved to Governor contract
49
+ // This allows proper access to ledger state
50
+
51
+ const currentTime = getCurrentTime();
52
+ const tokenKey = disclose(token);
53
+ const currentBalance = treasuryBalances.member(tokenKey) ? treasuryBalances.lookup(tokenKey) : TreasuryBalance {
54
+ token: tokenKey,
55
+ amount: 0,
56
+ lastUpdated: 0
57
+ };
58
+
59
+ const newBalance = TreasuryBalance {
60
+ token: tokenKey,
61
+ amount: (currentBalance.amount + disclose(amount)) as Uint<64>,
62
+ lastUpdated: currentTime
63
+ };
64
+
65
+ treasuryBalances.insert(tokenKey, disclose(newBalance));
66
+
67
+ // Update total treasury value
68
+ const totalValue = totalTreasuryValue.member(tokenKey) ? totalTreasuryValue.lookup(tokenKey) : 0;
69
+ totalTreasuryValue.insert(tokenKey, (totalValue + disclose(amount)) as Uint<64>);
70
+
71
+ // Increment transaction counter
72
+ transactionCounter.increment(1);
73
+ }
74
+
75
+ // Circuit to withdraw tokens from treasury (validations moved to Governor)
76
+ export circuit withdrawFromTreasury(
77
+ token: Bytes<32>,
78
+ amount: Uint<64>,
79
+ recipient: Bytes<32>,
80
+ effectId: Uint<64>,
81
+ proof: ProofOfEffectExecution
82
+ ): [] {
83
+ assert(proof.effectId == effectId, "Effect ID must match proof");
84
+
85
+ // NOTE: Amount validation and sufficient balance validation moved to Governor contract
86
+ // This allows proper access to ledger state
87
+
88
+ const tokenKey = disclose(token);
89
+ const currentBalance = treasuryBalances.lookup(tokenKey);
90
+
91
+ const currentTime = getCurrentTime();
92
+ const newBalance = TreasuryBalance {
93
+ token: tokenKey,
94
+ amount: (currentBalance.amount - disclose(amount)) as Uint<64>,
95
+ lastUpdated: currentTime
96
+ };
97
+
98
+ treasuryBalances.insert(tokenKey, disclose(newBalance));
99
+
100
+ // Update total treasury value
101
+ const totalValue = totalTreasuryValue.member(tokenKey) ? totalTreasuryValue.lookup(tokenKey) : 0;
102
+ totalTreasuryValue.insert(tokenKey, (totalValue - disclose(amount)) as Uint<64>);
103
+
104
+ // Transfer tokens to recipient
105
+ _unsafeTransfer(left<ZswapCoinPublicKey, ContractAddress>(ZswapCoinPublicKey{bytes: recipient}), disclose(amount) as Uint<128>);
106
+
107
+ // Increment transaction counter
108
+ transactionCounter.increment(1);
109
+ }
110
+
111
+ // Circuit to transfer tokens between treasury accounts (validations moved to Governor)
112
+ export circuit transferTreasuryTokens(
113
+ fromToken: Bytes<32>,
114
+ toToken: Bytes<32>,
115
+ amount: Uint<64>,
116
+ effectId: Uint<64>,
117
+ proof: ProofOfEffectExecution
118
+ ): [] {
119
+ assert(proof.effectId == effectId, "Effect ID must match proof");
120
+
121
+ // NOTE: Amount validation, token difference validation, and sufficient balance validation moved to Governor contract
122
+ // This allows proper access to ledger state
123
+
124
+ const currentTime = getCurrentTime();
125
+
126
+ // Update source balance
127
+ const fromTokenKey = disclose(fromToken);
128
+ const fromBalance = treasuryBalances.lookup(fromTokenKey);
129
+ const newFromBalance = TreasuryBalance {
130
+ token: fromTokenKey,
131
+ amount: (fromBalance.amount - amount) as Uint<64>,
132
+ lastUpdated: currentTime
133
+ };
134
+ treasuryBalances.insert(fromTokenKey, disclose(newFromBalance));
135
+
136
+ // Update destination balance
137
+ const toTokenKey = disclose(toToken);
138
+ const toBalance = treasuryBalances.member(toTokenKey) ? treasuryBalances.lookup(toTokenKey) : TreasuryBalance {
139
+ token: toTokenKey,
140
+ amount: 0,
141
+ lastUpdated: currentTime
142
+ };
143
+ const newToBalance = TreasuryBalance {
144
+ token: toTokenKey,
145
+ amount: (toBalance.amount + amount) as Uint<64>,
146
+ lastUpdated: currentTime
147
+ };
148
+ treasuryBalances.insert(toTokenKey, disclose(newToBalance));
149
+
150
+ // Increment transaction counter
151
+ transactionCounter.increment(1);
152
+ }
153
+
154
+ // Circuit to burn treasury tokens (validations moved to Governor)
155
+ export circuit burnTreasuryTokens(
156
+ token: Bytes<32>,
157
+ amount: Uint<64>,
158
+ effectId: Uint<64>,
159
+ proof: ProofOfEffectExecution
160
+ ): [] {
161
+ assert(proof.effectId == effectId, "Effect ID must match proof");
162
+
163
+ // NOTE: Amount validation and sufficient balance validation moved to Governor contract
164
+ // This allows proper access to ledger state
165
+
166
+ const tokenKey = disclose(token);
167
+ const currentBalance = treasuryBalances.lookup(tokenKey);
168
+
169
+ const currentTime = getCurrentTime();
170
+ const newBalance = TreasuryBalance {
171
+ token: tokenKey,
172
+ amount: (currentBalance.amount - disclose(amount)) as Uint<64>,
173
+ lastUpdated: currentTime
174
+ };
175
+
176
+ treasuryBalances.insert(tokenKey, disclose(newBalance));
177
+
178
+ // Update total treasury value
179
+ const totalValue = totalTreasuryValue.member(tokenKey) ? totalTreasuryValue.lookup(tokenKey) : 0;
180
+ totalTreasuryValue.insert(tokenKey, (totalValue - disclose(amount)) as Uint<64>);
181
+
182
+ // Burn tokens
183
+ _burn(right<ZswapCoinPublicKey, ContractAddress>(kernel.self()), disclose(amount) as Uint<128>);
184
+
185
+ // Increment transaction counter
186
+ transactionCounter.increment(1);
187
+ }
188
+
189
+ // Circuit to set spending limit for a token
190
+ export circuit setSpendingLimit(
191
+ token: Bytes<32>,
192
+ limit: Uint<64>,
193
+ proof: ProofOfGovernorApproval
194
+ ): [] {
195
+ const tokenKey = disclose(token);
196
+ spendingLimits.insert(tokenKey, disclose(limit));
197
+ }
198
+
199
+ // Circuit to authorize a spender
200
+ export circuit authorizeSpender(
201
+ spender: Bytes<32>,
202
+ proof: ProofOfGovernorApproval
203
+ ): [] {
204
+ const spenderKey = disclose(spender);
205
+ authorizedSpenders.insert(spenderKey, true);
206
+ }
207
+
208
+ // Circuit to revoke spender authorization
209
+ export circuit revokeSpenderAuthorization(
210
+ spender: Bytes<32>,
211
+ proof: ProofOfGovernorApproval
212
+ ): [] {
213
+ const spenderKey = disclose(spender);
214
+ authorizedSpenders.insert(spenderKey, false);
215
+ }
216
+
217
+ // Circuit to get treasury balance for a token
218
+ export circuit getTreasuryBalance(token: Bytes<32>): TreasuryBalance {
219
+ const tokenKey = disclose(token);
220
+ return treasuryBalances.member(tokenKey) ? treasuryBalances.lookup(tokenKey) : TreasuryBalance {
221
+ token: tokenKey,
222
+ amount: 0,
223
+ lastUpdated: 0
224
+ };
225
+ }
226
+
227
+ // Circuit to get total treasury value for a token
228
+ export circuit getTotalTreasuryValue(token: Bytes<32>): Uint<64> {
229
+ const tokenKey = disclose(token);
230
+ return totalTreasuryValue.member(tokenKey) ? totalTreasuryValue.lookup(tokenKey) : 0;
231
+ }
232
+
233
+ // Circuit to get transaction count
234
+ export circuit getTreasuryTransactionCount(): Uint<64> {
235
+ return transactionCounter.read();
236
+ }
237
+
238
+ // Circuit to get spending limit for a token
239
+ export circuit getSpendingLimit(token: Bytes<32>): Uint<64> {
240
+ const tokenKey = disclose(token);
241
+ return spendingLimits.member(tokenKey) ? spendingLimits.lookup(tokenKey) : 0;
242
+ }
243
+
244
+ // Circuit to check if spender is authorized
245
+ export circuit isAuthorizedSpender(spender: Bytes<32>): Boolean {
246
+ const spenderKey = disclose(spender);
247
+ return authorizedSpenders.member(spenderKey) ? authorizedSpenders.lookup(spenderKey) : false;
248
+ }
249
+
250
+ // Circuit to validate treasury spending
251
+ export circuit validateTreasurySpending(
252
+ token: Bytes<32>,
253
+ amount: Uint<64>,
254
+ spender: Bytes<32>
255
+ ): Boolean {
256
+ // Check if spender is authorized
257
+ const spenderKey = disclose(spender);
258
+ if (!authorizedSpenders.member(spenderKey) || !authorizedSpenders.lookup(spenderKey)) {
259
+ return false;
260
+ }
261
+
262
+ // Check spending limit
263
+ const tokenKey = disclose(token);
264
+ const limit = spendingLimits.member(tokenKey) ? spendingLimits.lookup(tokenKey) : 0;
265
+ if (limit > 0 && disclose(amount) > limit) {
266
+ return false;
267
+ }
268
+
269
+ // Check treasury balance
270
+ const balance = treasuryBalances.member(tokenKey) ? treasuryBalances.lookup(tokenKey) : TreasuryBalance {
271
+ token: tokenKey,
272
+ amount: 0,
273
+ lastUpdated: 0
274
+ };
275
+ if (balance.amount < disclose(amount)) {
276
+ return false;
277
+ }
278
+
279
+ return true;
280
+ }
281
+
282
+ // Circuit to get treasury statistics
283
+ export circuit getTreasuryStatistics(): TreasuryStatistics {
284
+ // This would calculate various treasury metrics
285
+ return TreasuryStatistics {
286
+ totalTokens: 0,
287
+ totalValue: 0,
288
+ lastTransaction: getCurrentTime(),
289
+ transactionCount: transactionCounter.read()
290
+ };
291
+ }
292
+
293
+ // Circuit to batch process treasury operations
294
+ export circuit batchTreasuryOperations(
295
+ operations: Vector<10, TreasuryOperation>,
296
+ proof: ProofOfEffectExecution
297
+ ): [] {
298
+ for (const operation of disclose(operations)) {
299
+ if (operation.operationType == TreasuryOperationType.Withdraw) {
300
+ withdrawFromTreasury(
301
+ operation.token,
302
+ operation.amount,
303
+ operation.recipient,
304
+ operation.effectId,
305
+ proof
306
+ );
307
+ } else if (operation.operationType == TreasuryOperationType.Transfer) {
308
+ transferTreasuryTokens(
309
+ operation.token,
310
+ operation.targetToken,
311
+ operation.amount,
312
+ operation.effectId,
313
+ proof
314
+ );
315
+ } else if (operation.operationType == TreasuryOperationType.Burn) {
316
+ burnTreasuryTokens(
317
+ operation.token,
318
+ operation.amount,
319
+ operation.effectId,
320
+ proof
321
+ );
322
+ }
323
+ }
324
+ }
325
+
326
+ }