@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,6 @@
1
+ import * as contractModule from "./managed/governor/contract/index.cjs";
2
+ declare const Contract: typeof contractModule.Contract, ledger: typeof contractModule.ledger, pureCircuits: contractModule.PureCircuits, contractReferenceLocations: any, GovernorPermission: typeof contractModule.GovernorPermission, TreasuryOperationType: typeof contractModule.TreasuryOperationType, TreasuryTransactionType: typeof contractModule.TreasuryTransactionType, VoteChoice: typeof contractModule.VoteChoice, DelegationStatus: typeof contractModule.DelegationStatus, EffectActionType: typeof contractModule.EffectActionType, EffectStatus: typeof contractModule.EffectStatus, ProposalStatus: typeof contractModule.ProposalStatus, TimeComparison: typeof contractModule.TimeComparison;
3
+ export default contractModule;
4
+ export { Contract };
5
+ export { ledger, pureCircuits, contractReferenceLocations, GovernorPermission, TreasuryOperationType, TreasuryTransactionType, VoteChoice, DelegationStatus, EffectActionType, EffectStatus, ProposalStatus, TimeComparison, };
6
+ export * from "./witnesses.js";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ // Import from CJS and re-export explicitly (Vite can't handle export * from CJS)
2
+ // Use namespace import to correctly map CommonJS named exports under Vite
3
+ import * as contractModule from "./managed/governor/contract/index.cjs";
4
+ const { Contract, ledger, pureCircuits, contractReferenceLocations, GovernorPermission, TreasuryOperationType, TreasuryTransactionType, VoteChoice, DelegationStatus, EffectActionType, EffectStatus, ProposalStatus, TimeComparison, } = contractModule;
5
+ export default contractModule;
6
+ // Export Contract sep I'marately to preserve class type signature
7
+ export { Contract };
8
+ // Export other values
9
+ export { ledger, pureCircuits, contractReferenceLocations, GovernorPermission, TreasuryOperationType, TreasuryTransactionType, VoteChoice, DelegationStatus, EffectActionType, EffectStatus, ProposalStatus, TimeComparison, };
10
+ export * from "./witnesses.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,0EAA0E;AAC1E,OAAO,KAAK,cAAc,MAAM,uCAAuC,CAAC;AACxE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,0BAA0B,EAChE,kBAAkB,EAClB,qBAAqB,EACrB,uBAAuB,EACvB,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,cAAc,GACf,GAAG,cAAc,CAAC;AAEnB,eAAe,cAAc,CAAC;AAE9B,kEAAkE;AAClE,OAAO,EAAE,QAAQ,EAAE,CAAC;AAEpB,sBAAsB;AACtB,OAAO,EACL,MAAM,EACN,YAAY,EACZ,0BAA0B,EAC1B,kBAAkB,EAClB,qBAAqB,EACrB,uBAAuB,EACvB,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,cAAc,GACf,CAAC;AAEF,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { WitnessContext } from '@midnight-ntwrk/compact-runtime';
2
+ import type { Ledger } from './managed/governor/contract/index.cjs';
3
+ export type GovernorPrivateState = {
4
+ secretKey: Uint8Array | null;
5
+ currentTime: bigint;
6
+ userStake: {
7
+ amount: bigint;
8
+ delegatedTo: Uint8Array;
9
+ stakedAt: bigint;
10
+ };
11
+ votingHistory: any[];
12
+ proposalHistory: any[];
13
+ governanceConfig: {
14
+ votingPeriod: bigint;
15
+ quorumThreshold: bigint;
16
+ proposalThreshold: bigint;
17
+ proposalLifetime: bigint;
18
+ executionDelay: bigint;
19
+ gracePeriod: bigint;
20
+ minStakeAmount: bigint;
21
+ stakingPeriod: bigint;
22
+ };
23
+ };
24
+ export declare const witnesses: {
25
+ local_secret_key: ({ privateState }: WitnessContext<Ledger, GovernorPrivateState>) => [GovernorPrivateState, {
26
+ is_some: boolean;
27
+ value: Uint8Array;
28
+ }];
29
+ };
@@ -0,0 +1,10 @@
1
+ // Implement the witnesses object
2
+ export const witnesses = {
3
+ local_secret_key: ({ privateState }) => [
4
+ privateState,
5
+ privateState.secretKey
6
+ ? { is_some: true, value: privateState.secretKey }
7
+ : { is_some: false, value: new Uint8Array(32) },
8
+ ],
9
+ };
10
+ //# sourceMappingURL=witnesses.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"witnesses.js","sourceRoot":"","sources":["../src/witnesses.ts"],"names":[],"mappings":"AA2BA,iCAAiC;AACjC,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,gBAAgB,EAAE,CAChB,EAAE,YAAY,EAAgD,EACG,EAAE,CAAC;QACpE,YAAY;QACZ,YAAY,CAAC,SAAS;YACpB,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,CAAC,SAAS,EAAE;YAClD,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,UAAU,CAAC,EAAE,CAAC,EAAE;KAClD;CACF,CAAC"}
@@ -0,0 +1,38 @@
1
+ import js from "@eslint/js";
2
+ import plugin from "@typescript-eslint/eslint-plugin";
3
+ import parser from "@typescript-eslint/parser";
4
+ import pluginPrettier from "eslint-plugin-prettier";
5
+
6
+ export default [
7
+ js.configs.recommended,
8
+ {
9
+ files: ["**/*.ts", "**/*.tsx"],
10
+ languageOptions: {
11
+ parser,
12
+ parserOptions: {
13
+ ecmaVersion: "latest",
14
+ sourceType: "module",
15
+ project: ["./tsconfig.json"]
16
+ }
17
+ },
18
+ plugins: {
19
+ "@typescript-eslint": plugin,
20
+ prettier: pluginPrettier
21
+ },
22
+ rules: {
23
+ "prettier/prettier": "error",
24
+ "@typescript-eslint/no-misused-promises": "off",
25
+ "@typescript-eslint/no-floating-promises": "warn",
26
+ "@typescript-eslint/promise-function-async": "off",
27
+ "@typescript-eslint/no-redeclare": "off",
28
+ "@typescript-eslint/no-invalid-void-type": "off",
29
+ "@typescript-eslint/no-unsafe-call": "off",
30
+ "@typescript-eslint/no-unsafe-member-access": "off",
31
+ "@typescript-eslint/explicit-function-return-type": "off",
32
+ "@typescript-eslint/consistent-type-definitions": "off"
33
+ }
34
+ },
35
+ {
36
+ ignores: ["src/managed/**"]
37
+ }
38
+ ];
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@claritydao/midnight-agora-contracts",
3
+ "version": "0.1.0",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "require": "./dist/index.js",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "compact": "compactc src/contracts/DAO/Governor.compact src/managed/governor",
19
+ "test": "vitest run",
20
+ "test:compile": "npm run compact && vitest run",
21
+ "test:coverage": "vitest run --coverage",
22
+ "build": "rm -rf dist && tsc --project tsconfig.build.json && cp -Rf ./src/managed ./dist/managed && cp ./src/contracts/DAO/Governor.compact ./dist",
23
+ "lint": "eslint src",
24
+ "typecheck": "tsc -p tsconfig.json --noEmit"
25
+ },
26
+ "devDependencies": {
27
+ "@vitest/coverage-v8": "3.2.4",
28
+ "fast-check": "^4.2.0",
29
+ "vitest": "^3.2.4"
30
+ }
31
+ }
@@ -0,0 +1,282 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma language_version 0.18.0;
3
+
4
+ module EffectModule
5
+ {
6
+ import CompactStandardLibrary;
7
+ import '../utils/TypesModule';
8
+ import '../fungigleToken/FungibleToken';
9
+
10
+ // Ledger state for effects
11
+ export ledger effects: Map<Uint<64>, Effect>;
12
+
13
+ // Ledger state for effect counter
14
+ export ledger effectCounter: Counter;
15
+
16
+ // Ledger state for treasury balance (imported from treasury module)
17
+ export ledger treasuryBalance: Map<Bytes<32>, Uint<64>>;
18
+
19
+ // Ledger state for DAO configuration
20
+ export ledger daoConfig: Map<Bytes<32>, Bytes<32>>;
21
+
22
+ // Circuit to create a new effect (validations moved to Governor)
23
+ export circuit createEffect(
24
+ proposalId: Uint<64>,
25
+ actions: Vector<10, EffectAction>,
26
+ proof: ProofOfStake
27
+ ): Uint<64> {
28
+
29
+ // NOTE: Effect actions validation moved to Governor contract
30
+ // This allows proper access to ledger state for validations
31
+
32
+ // Generate effect ID
33
+ const effectId = generateEffectId();
34
+
35
+ // Create effect
36
+ const effect = Effect {
37
+ id: effectId,
38
+ proposalId: disclose(proposalId),
39
+ actions: disclose(actions),
40
+ status: EffectStatus.Pending,
41
+ executedAt: 0
42
+ };
43
+
44
+ effects.insert(effectId, disclose(effect));
45
+
46
+ return effectId;
47
+ }
48
+
49
+ // Circuit to execute an effect (validations moved to Governor)
50
+ export circuit executeEffect(input: Uint<64>, proof: ProofOfGovernorApproval): [] {
51
+ const effectKey = disclose(input);
52
+
53
+ // Check if effect exists before trying to execute it
54
+ if (!effects.member(effectKey)) {
55
+ // Effect doesn't exist, skip execution
56
+ return;
57
+ }
58
+
59
+ const effect = effects.lookup(effectKey);
60
+
61
+ for (const action of effect.actions) {
62
+ if (action.typ == EffectActionType.MintToken) {
63
+ mint(action.token, action.amount, action.to);
64
+ } else if (action.typ == EffectActionType.Transfer) {
65
+ const bal = treasuryBalance.member(action.token) ? treasuryBalance.lookup(action.token) : 0;
66
+ assert(bal >= action.amount, "Insufficient treasury balance");
67
+ treasuryBalance.insert(action.token, bal - action.amount);
68
+ send(action.to, action.token, action.amount);
69
+ } else if (action.typ == EffectActionType.UpdateConfig) {
70
+ daoConfig.insert(action.configKey, action.configValue);
71
+ }
72
+ }
73
+
74
+ const currentTime = getCurrentTime();
75
+ const executedEffect = Effect {
76
+ id: effect.id,
77
+ proposalId: effect.proposalId,
78
+ actions: effect.actions,
79
+ status: EffectStatus.Executed,
80
+ executedAt: currentTime
81
+ };
82
+
83
+ effects.insert(effectKey, disclose(executedEffect));
84
+ }
85
+
86
+ // Circuit to cancel an effect (validations moved to Governor)
87
+ export circuit cancelEffect(effectId: Uint<64>, proof: ProofOfGovernorApproval): [] {
88
+ const effectKey = disclose(effectId);
89
+
90
+ // Check if effect exists before trying to cancel it
91
+ if (!effects.member(effectKey)) {
92
+ // Effect doesn't exist, skip cancellation
93
+ return;
94
+ }
95
+
96
+ const effect = effects.lookup(effectKey);
97
+
98
+ const cancelledEffect = Effect {
99
+ id: effect.id,
100
+ proposalId: effect.proposalId,
101
+ actions: effect.actions,
102
+ status: EffectStatus.Cancelled,
103
+ executedAt: 0
104
+ };
105
+
106
+ effects.insert(effectKey, cancelledEffect);
107
+ }
108
+
109
+ // Circuit to get effect details
110
+ export circuit getEffect(effectId: Uint<64>): Effect {
111
+ const effectKey = disclose(effectId);
112
+ if (!effects.member(effectKey)) {
113
+ // Return default effect for non-existent effects
114
+ return Effect {
115
+ id: 0,
116
+ proposalId: 0,
117
+ actions: default<Vector<10, EffectAction>>,
118
+ status: EffectStatus.Cancelled,
119
+ executedAt: 0
120
+ };
121
+ }
122
+ return effects.lookup(effectKey);
123
+ }
124
+
125
+ // Circuit to get effect count by proposal
126
+ export circuit getEffectCountByProposal(proposalId: Uint<64>): Uint<64> {
127
+ // In a real implementation, this would count effects for the proposal
128
+ // For now, we'll return 0 as a placeholder
129
+ return 0;
130
+ }
131
+
132
+ // Circuit to check if effect can be executed
133
+ export circuit canExecuteEffect(effectId: Uint<64>): Boolean {
134
+ const effectKey = disclose(effectId);
135
+ if (!effects.member(effectKey)) {
136
+ return false; // Effect doesn't exist, can't execute
137
+ }
138
+ const effect = effects.lookup(effectKey);
139
+ const currentTime = getCurrentTime();
140
+ return effect.status == EffectStatus.Pending;
141
+ }
142
+
143
+ // Circuit to validate effect actions before creation
144
+ export circuit validateEffectActions(actions: Vector<10, EffectAction>): Boolean {
145
+ for (const action of disclose(actions)) {
146
+ if (action.typ == EffectActionType.MintToken) {
147
+ assert(action.amount > 0, "Mint amount must be positive");
148
+ assert(action.to != default<Bytes<32>>, "Invalid mint recipient");
149
+ } else if (action.typ == EffectActionType.Transfer) {
150
+ assert(action.amount > 0, "Transfer amount must be positive");
151
+ assert(action.to != default<Bytes<32>>, "Invalid transfer recipient");
152
+ const bal = treasuryBalance.member(action.token) ? treasuryBalance.lookup(action.token) : 0;
153
+ assert(bal >= action.amount, "Insufficient treasury balance for transfer");
154
+ } else if (action.typ == EffectActionType.UpdateConfig) {
155
+ assert(action.configKey != default<Bytes<32>>, "Config key cannot be empty");
156
+ assert(action.configValue != default<Bytes<32>>, "Config value cannot be empty");
157
+ }
158
+ }
159
+
160
+ return true;
161
+ }
162
+
163
+ // Circuit to batch execute multiple effects (validations moved to Governor)
164
+ export circuit batchExecuteEffects(
165
+ effectIds: Vector<10, Uint<64>>,
166
+ proof: ProofOfGovernorApproval
167
+ ): [] {
168
+
169
+ // NOTE: Effect existence and status validations moved to Governor contract
170
+ // This allows proper access to ledger state
171
+
172
+ for (const effectId of effectIds) {
173
+ const effectKey = disclose(effectId);
174
+ executeEffect(effectKey, proof);
175
+ }
176
+ }
177
+
178
+ // Circuit to create a mint token effect
179
+ export circuit createMintTokenEffect(
180
+ proposalId: Uint<64>,
181
+ token: Bytes<32>,
182
+ amount: Uint<64>,
183
+ to: Bytes<32>,
184
+ proof: ProofOfStake
185
+ ): Uint<64> {
186
+ const action = EffectAction {
187
+ typ: EffectActionType.MintToken,
188
+ token: token,
189
+ amount: amount,
190
+ to: to,
191
+ configKey: default<Bytes<32>>,
192
+ configValue: default<Bytes<32>>
193
+ };
194
+
195
+ const actions = default<Vector<10, EffectAction>>;
196
+ // Note: Vector handling is different - this would need proper Vector management
197
+ // For now, using default Vector as placeholder
198
+
199
+ return createEffect(proposalId, actions, proof);
200
+ }
201
+
202
+ // Circuit to create a transfer effect
203
+ export circuit createTransferEffect(
204
+ proposalId: Uint<64>,
205
+ token: Bytes<32>,
206
+ amount: Uint<64>,
207
+ to: Bytes<32>,
208
+ proof: ProofOfStake
209
+ ): Uint<64> {
210
+ const action = EffectAction {
211
+ typ: EffectActionType.Transfer,
212
+ token: token,
213
+ amount: amount,
214
+ to: to,
215
+ configKey: default<Bytes<32>>,
216
+ configValue: default<Bytes<32>>
217
+ };
218
+
219
+ const actions = default<Vector<10, EffectAction>>;
220
+ // Note: Vector handling is different - this would need proper Vector management
221
+ // For now, using default Vector as placeholder
222
+
223
+ return createEffect(proposalId, actions, proof);
224
+ }
225
+
226
+ // Circuit to create a config update effect
227
+ export circuit createConfigUpdateEffect(
228
+ proposalId: Uint<64>,
229
+ configKey: Bytes<32>,
230
+ configValue: Bytes<32>,
231
+ proof: ProofOfStake
232
+ ): Uint<64> {
233
+ const action = EffectAction {
234
+ typ: EffectActionType.UpdateConfig,
235
+ token: default<Bytes<32>>,
236
+ amount: 0,
237
+ to: default<Bytes<32>>,
238
+ configKey: configKey,
239
+ configValue: configValue
240
+ };
241
+
242
+ const actions = default<Vector<10, EffectAction>>;
243
+ // Note: Vector handling is different - this would need proper Vector management
244
+ // For now, using default Vector as placeholder
245
+
246
+ return createEffect(proposalId, actions, proof);
247
+ }
248
+
249
+ // Helper functions
250
+ export circuit generateEffectId(): Uint<64> {
251
+ effectCounter.increment(1);
252
+ const counter = effectCounter.read();
253
+ return counter;
254
+ }
255
+
256
+ // Token operations (these would be implemented based on the specific blockchain)
257
+ export circuit mint(token: Bytes<32>, amount: Uint<64>, to: Bytes<32>): [] {
258
+ // Adapt to FungibleToken interface
259
+ // You need to convert Bytes<32> to Either<ZswapCoinPublicKey, ContractAddress> if needed
260
+ // Example assumes 'to' is a ZswapCoinPublicKey
261
+ _mint(left<ZswapCoinPublicKey, ContractAddress>(ZswapCoinPublicKey{bytes: to}), amount as Uint<128>);
262
+ }
263
+
264
+ export circuit send(to: Bytes<32>, token: Bytes<32>, amount: Uint<64>): [] {
265
+ // Again, adapt to FungibleToken interface
266
+ // The sender is the DAO/treasury, so you may need to use a special key or contract address
267
+ // Example assumes 'to' is a ZswapCoinPublicKey
268
+ _unsafeTransfer(left<ZswapCoinPublicKey, ContractAddress>(ZswapCoinPublicKey{bytes: to}), amount as Uint<128>);
269
+ }
270
+
271
+ // Circuit to get DAO configuration value
272
+ export circuit getDaoConfig(key: Bytes<32>): Bytes<32> {
273
+ const keyDisclosed = disclose(key);
274
+ if (daoConfig.member(keyDisclosed)) {
275
+ return daoConfig.lookup(keyDisclosed);
276
+ } else {
277
+ // Return default value if key doesn't exist
278
+ return default<Bytes<32>>;
279
+ }
280
+ }
281
+
282
+ }