@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.
- package/.prettierrc +1 -0
- package/README.md +435 -0
- package/dist/Governor.compact +875 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/witnesses.d.ts +29 -0
- package/dist/witnesses.js +10 -0
- package/dist/witnesses.js.map +1 -0
- package/eslint.config.mjs +38 -0
- package/package.json +31 -0
- package/src/contracts/DAO/EffectModule.compact +282 -0
- package/src/contracts/DAO/Governor.compact +875 -0
- package/src/contracts/DAO/ProposalModule.compact +331 -0
- package/src/contracts/DAO/StakeModule.compact +301 -0
- package/src/contracts/DAO/TreasuryModule.compact +326 -0
- package/src/contracts/fungigleToken/FungibleToken.compact +570 -0
- package/src/contracts/utils/DivModule.compact +39 -0
- package/src/contracts/utils/Initializable.compact +42 -0
- package/src/contracts/utils/Pausable.compact +61 -0
- package/src/contracts/utils/TypesModule.compact +285 -0
- package/src/contracts/utils/Utils.compact +76 -0
- package/src/index.ts +37 -0
- package/src/test/governance-simulator.ts +703 -0
- package/src/test/governor-contract.test.ts +1655 -0
- package/src/test/utils/address.ts +82 -0
- package/src/witnesses.ts +38 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +40 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma language_version 0.18.0;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @module Pausable
|
|
6
|
+
* @description Pausable allows the implementing contract to implement
|
|
7
|
+
* an emergency stop mechanism. Only circuits that call `assertPaused`
|
|
8
|
+
* or `assertNotPaused` will be affected by this mechanism.
|
|
9
|
+
*/
|
|
10
|
+
module Pausable {
|
|
11
|
+
import CompactStandardLibrary;
|
|
12
|
+
|
|
13
|
+
export ledger _isPaused: Boolean;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description Returns true if the contract is paused, and false otherwise.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Boolean} True if paused.
|
|
19
|
+
*/
|
|
20
|
+
export circuit isPaused(): Boolean {
|
|
21
|
+
return _isPaused;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @description Makes a circuit only callable when the contract is paused.
|
|
26
|
+
*
|
|
27
|
+
* @returns None.
|
|
28
|
+
*/
|
|
29
|
+
export circuit assertPaused(): [] {
|
|
30
|
+
assert (_isPaused, "Pausable: not paused");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @description Makes a circuit only callable when the contract is not paused.
|
|
35
|
+
*
|
|
36
|
+
* @returns None.
|
|
37
|
+
*/
|
|
38
|
+
export circuit assertNotPaused(): [] {
|
|
39
|
+
assert (!_isPaused, "Pausable: paused") ;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @description Triggers a stopped state.
|
|
44
|
+
*
|
|
45
|
+
* @returns None.
|
|
46
|
+
*/
|
|
47
|
+
export circuit _pause(): [] {
|
|
48
|
+
assertNotPaused();
|
|
49
|
+
_isPaused = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @description Lifts the pause on the contract.
|
|
54
|
+
*
|
|
55
|
+
* @returns None.
|
|
56
|
+
*/
|
|
57
|
+
export circuit _unpause(): [] {
|
|
58
|
+
assertPaused();
|
|
59
|
+
_isPaused = false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma language_version 0.18.0;
|
|
3
|
+
|
|
4
|
+
module TypesModule {
|
|
5
|
+
import CompactStandardLibrary;
|
|
6
|
+
|
|
7
|
+
// Timestamp storage ledger
|
|
8
|
+
export ledger last_admin_timestamp: Uint<64>;
|
|
9
|
+
|
|
10
|
+
// Time comparison types
|
|
11
|
+
export enum TimeComparison {
|
|
12
|
+
LessThan,
|
|
13
|
+
LessThanOrEqual,
|
|
14
|
+
GreaterThan,
|
|
15
|
+
GreaterThanOrEqual
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Proposal status enumeration
|
|
19
|
+
export enum ProposalStatus {
|
|
20
|
+
Draft,
|
|
21
|
+
Active,
|
|
22
|
+
Defeated,
|
|
23
|
+
Succeeded,
|
|
24
|
+
Queued,
|
|
25
|
+
Expired,
|
|
26
|
+
Executed,
|
|
27
|
+
Cancelled
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Effect status enumeration
|
|
31
|
+
export enum EffectStatus {
|
|
32
|
+
Pending,
|
|
33
|
+
Executed,
|
|
34
|
+
Cancelled
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Effect action type enumeration
|
|
38
|
+
export enum EffectActionType {
|
|
39
|
+
MintToken,
|
|
40
|
+
Transfer,
|
|
41
|
+
UpdateConfig
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Stake delegation status
|
|
45
|
+
export enum DelegationStatus {
|
|
46
|
+
SelfStaked,
|
|
47
|
+
Delegated
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Voting choice enumeration
|
|
51
|
+
export enum VoteChoice {
|
|
52
|
+
For,
|
|
53
|
+
Against,
|
|
54
|
+
Abstain
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Treasury transaction types
|
|
58
|
+
export enum TreasuryTransactionType {
|
|
59
|
+
Deposit,
|
|
60
|
+
Withdrawal,
|
|
61
|
+
Transfer,
|
|
62
|
+
Burn
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Treasury operation types
|
|
66
|
+
export enum TreasuryOperationType {
|
|
67
|
+
Withdraw,
|
|
68
|
+
Transfer,
|
|
69
|
+
Burn
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Governor permission levels
|
|
73
|
+
export enum GovernorPermission {
|
|
74
|
+
None,
|
|
75
|
+
Proposer,
|
|
76
|
+
Executor,
|
|
77
|
+
Admin
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Stake record structure
|
|
81
|
+
export struct StakeRecord {
|
|
82
|
+
userId: Bytes<32>;
|
|
83
|
+
amount: Uint<64>;
|
|
84
|
+
delegatedTo: Bytes<32>;
|
|
85
|
+
status: DelegationStatus;
|
|
86
|
+
stakedAt: Uint<64>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Delegation key structure
|
|
90
|
+
export struct DelegationKey {
|
|
91
|
+
delegator: Bytes<32>;
|
|
92
|
+
delegatee: Bytes<32>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Delegation record structure
|
|
96
|
+
export struct DelegationRecord {
|
|
97
|
+
delegator: Bytes<32>;
|
|
98
|
+
delegatee: Bytes<32>;
|
|
99
|
+
amount: Uint<64>;
|
|
100
|
+
delegatedAt: Uint<64>;
|
|
101
|
+
status: DelegationStatus;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Stake info structure
|
|
105
|
+
export struct StakeInfo {
|
|
106
|
+
userId: Bytes<32>;
|
|
107
|
+
stakedAmount: Uint<64>;
|
|
108
|
+
totalStake: Uint<64>;
|
|
109
|
+
votingPower: Uint<64>;
|
|
110
|
+
delegatedTo: Bytes<32>;
|
|
111
|
+
status: DelegationStatus;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Vote record structure
|
|
115
|
+
export struct VoteRecord {
|
|
116
|
+
proposalId: Uint<64>;
|
|
117
|
+
voter: Bytes<32>;
|
|
118
|
+
choice: VoteChoice;
|
|
119
|
+
power: Uint<64>;
|
|
120
|
+
timestamp: Uint<64>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Effect action structure
|
|
124
|
+
export struct EffectAction {
|
|
125
|
+
typ: EffectActionType;
|
|
126
|
+
token: Bytes<32>;
|
|
127
|
+
amount: Uint<64>;
|
|
128
|
+
to: Bytes<32>;
|
|
129
|
+
configKey: Bytes<32>;
|
|
130
|
+
configValue: Bytes<32>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Effect structure
|
|
134
|
+
export struct Effect {
|
|
135
|
+
id: Uint<64>;
|
|
136
|
+
proposalId: Uint<64>;
|
|
137
|
+
actions: Vector<10, EffectAction>;
|
|
138
|
+
status: EffectStatus;
|
|
139
|
+
executedAt: Uint<64>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Proposal structure
|
|
143
|
+
export struct Proposal {
|
|
144
|
+
id: Uint<64>;
|
|
145
|
+
creator: Bytes<32>;
|
|
146
|
+
title: Opaque<'string'>;
|
|
147
|
+
description: Opaque<'string'>;
|
|
148
|
+
effectId: Uint<64>;
|
|
149
|
+
status: ProposalStatus;
|
|
150
|
+
votingStartTime: Uint<64>;
|
|
151
|
+
votingEndTime: Uint<64>;
|
|
152
|
+
votesFor: Uint<64>;
|
|
153
|
+
votesAgainst: Uint<64>;
|
|
154
|
+
votesAbstain: Uint<64>;
|
|
155
|
+
quorumRequired: Uint<64>;
|
|
156
|
+
threshold: Uint<64>;
|
|
157
|
+
createdAt: Uint<64>;
|
|
158
|
+
finalizedAt: Uint<64>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Governance parameters structure
|
|
162
|
+
export struct GovernanceConfig {
|
|
163
|
+
votingPeriod: Uint<64>;
|
|
164
|
+
quorumThreshold: Uint<64>;
|
|
165
|
+
proposalThreshold: Uint<64>;
|
|
166
|
+
proposalLifetime: Uint<64>;
|
|
167
|
+
executionDelay: Uint<64>;
|
|
168
|
+
gracePeriod: Uint<64>;
|
|
169
|
+
minStakeAmount: Uint<64>;
|
|
170
|
+
stakingPeriod: Uint<64>;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ZK-proof authorization structures
|
|
174
|
+
export struct ProofOfStake {
|
|
175
|
+
userId: Bytes<32>;
|
|
176
|
+
amount: Uint<64>;
|
|
177
|
+
nonce: Bytes<32>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export struct ProofOfGovernorApproval {
|
|
181
|
+
proposalId: Uint<64>;
|
|
182
|
+
effectId: Uint<64>;
|
|
183
|
+
approvalTime: Uint<64>;
|
|
184
|
+
nonce: Bytes<32>;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export struct ProofOfEffectExecution {
|
|
188
|
+
effectId: Uint<64>;
|
|
189
|
+
executorId: Bytes<32>;
|
|
190
|
+
executionTime: Uint<64>;
|
|
191
|
+
nonce: Bytes<32>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Treasury balance entry
|
|
195
|
+
export struct TreasuryBalance {
|
|
196
|
+
token: Bytes<32>;
|
|
197
|
+
amount: Uint<64>;
|
|
198
|
+
lastUpdated: Uint<64>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Treasury transaction record
|
|
202
|
+
export struct TreasuryTransaction {
|
|
203
|
+
id: Bytes<32>;
|
|
204
|
+
transactionType: TreasuryTransactionType;
|
|
205
|
+
token: Bytes<32>;
|
|
206
|
+
amount: Uint<64>;
|
|
207
|
+
from: Bytes<32>;
|
|
208
|
+
to: Bytes<32>;
|
|
209
|
+
timestamp: Uint<64>;
|
|
210
|
+
effectId: Uint<64>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Treasury operation structure
|
|
214
|
+
export struct TreasuryOperation {
|
|
215
|
+
operationType: TreasuryOperationType;
|
|
216
|
+
token: Bytes<32>;
|
|
217
|
+
targetToken: Bytes<32>;
|
|
218
|
+
amount: Uint<64>;
|
|
219
|
+
recipient: Bytes<32>;
|
|
220
|
+
effectId: Uint<64>;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Treasury statistics structure
|
|
224
|
+
export struct TreasuryStatistics {
|
|
225
|
+
totalTokens: Uint<64>;
|
|
226
|
+
totalValue: Uint<64>;
|
|
227
|
+
lastTransaction: Uint<64>;
|
|
228
|
+
transactionCount: Uint<64>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Governor configuration structure
|
|
232
|
+
export struct GovernorConfig {
|
|
233
|
+
governance: GovernanceConfig;
|
|
234
|
+
executionDelay: Uint<64>;
|
|
235
|
+
gracePeriod: Uint<64>;
|
|
236
|
+
emergencyVetoEnabled: Boolean;
|
|
237
|
+
emergencyVetoThreshold: Uint<64>;
|
|
238
|
+
maxActiveProposals: Uint<64>;
|
|
239
|
+
proposalLifetime: Uint<64>;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Queued proposal structure
|
|
243
|
+
export struct QueuedProposal {
|
|
244
|
+
proposalId: Uint<64>;
|
|
245
|
+
effectId: Uint<64>;
|
|
246
|
+
queuedAt: Uint<64>;
|
|
247
|
+
executeAfter: Uint<64>;
|
|
248
|
+
expiresAt: Uint<64>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Governor status structure
|
|
252
|
+
export struct GovernorStatus {
|
|
253
|
+
currentTime: Uint<64>;
|
|
254
|
+
activeProposalCount: Uint<64>;
|
|
255
|
+
queuedProposalCount: Uint<64>;
|
|
256
|
+
config: GovernorConfig;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
// Timestamp storage and validation functions
|
|
262
|
+
export circuit storeTimestamp(ts: Uint<64>): [] {
|
|
263
|
+
assert(disclose(ts) > 0, "Timestamp must be greater than 0");
|
|
264
|
+
last_admin_timestamp = disclose(ts);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export circuit validateTime(time: Uint<64>, comparison: TimeComparison): Boolean {
|
|
268
|
+
if (disclose(comparison) == TimeComparison.LessThan) {
|
|
269
|
+
return time < last_admin_timestamp;
|
|
270
|
+
}
|
|
271
|
+
if (disclose(comparison) == TimeComparison.LessThanOrEqual) {
|
|
272
|
+
return time <= last_admin_timestamp;
|
|
273
|
+
}
|
|
274
|
+
if (disclose(comparison) == TimeComparison.GreaterThan) {
|
|
275
|
+
return time > last_admin_timestamp;
|
|
276
|
+
}
|
|
277
|
+
if (disclose(comparison) == TimeComparison.GreaterThanOrEqual) {
|
|
278
|
+
return time >= last_admin_timestamp;
|
|
279
|
+
}
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
export circuit getCurrentTime(): Uint<64> {
|
|
283
|
+
return disclose(last_admin_timestamp);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma language_version 0.18.0;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @module Utils.
|
|
6
|
+
* @description A library for common utilities used in Compact contracts.
|
|
7
|
+
*/
|
|
8
|
+
module Utils {
|
|
9
|
+
import CompactStandardLibrary;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @description Returns whether `keyOrAddress` is the zero address.
|
|
13
|
+
*
|
|
14
|
+
* @notice Midnight's burn address is represented as left<ZswapCoinPublicKey, ContractAddress>(default<ZswapCoinPublicKey>)
|
|
15
|
+
* in Compact, so we've chosen to represent the zero address as this structure as well.
|
|
16
|
+
*
|
|
17
|
+
* @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
|
|
18
|
+
* @return {Boolean} - Returns true if `keyOrAddress` is zero.
|
|
19
|
+
*/
|
|
20
|
+
export pure circuit isKeyOrAddressZero(keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
21
|
+
return isContractAddress(keyOrAddress)
|
|
22
|
+
? default<ContractAddress> == keyOrAddress.right
|
|
23
|
+
: default<ZswapCoinPublicKey> == keyOrAddress.left;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description Returns whether `key` is the zero address.
|
|
28
|
+
*
|
|
29
|
+
* @param {ZswapCoinPublicKey} key - A ZswapCoinPublicKey
|
|
30
|
+
* @return {Boolean} - Returns true if `key` is zero.
|
|
31
|
+
*/
|
|
32
|
+
export pure circuit isKeyZero(key: ZswapCoinPublicKey): Boolean {
|
|
33
|
+
const zero = default<ZswapCoinPublicKey>;
|
|
34
|
+
return zero == key;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @description Returns whether `keyOrAddress` is equal to `other`. Assumes that a ZswapCoinPublicKey
|
|
39
|
+
* and a ContractAddress can never be equal
|
|
40
|
+
*
|
|
41
|
+
* @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
|
|
42
|
+
* @param {Either<ZswapCoinPublicKey, ContractAddress>} other - The other value to check, either a ZswapCoinPublicKey or a ContractAddress.
|
|
43
|
+
* @return {Boolean} - Returns true if `keyOrAddress` is is equal to `other`.
|
|
44
|
+
*/
|
|
45
|
+
export pure circuit isKeyOrAddressEqual(
|
|
46
|
+
keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>,
|
|
47
|
+
other: Either<ZswapCoinPublicKey, ContractAddress>
|
|
48
|
+
): Boolean {
|
|
49
|
+
if (keyOrAddress.is_left && other.is_left) {
|
|
50
|
+
return keyOrAddress.left == other.left;
|
|
51
|
+
} else if (!keyOrAddress.is_left && !other.is_left) {
|
|
52
|
+
return keyOrAddress.right == other.right;
|
|
53
|
+
} else {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @description Returns whether `keyOrAddress` is a ContractAddress type
|
|
60
|
+
*
|
|
61
|
+
* @param {Either<ZswapCoinPublicKey, ContractAddress>} keyOrAddress - The target value to check, either a ZswapCoinPublicKey or a ContractAddress.
|
|
62
|
+
* @return {Boolean} - Returns true if `keyOrAddress` is a ContractAddress
|
|
63
|
+
*/
|
|
64
|
+
export pure circuit isContractAddress(keyOrAddress: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
65
|
+
return !keyOrAddress.is_left;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @description A helper function that returns the empty string: ""
|
|
70
|
+
*
|
|
71
|
+
* @return {Opaque<"string">} - The empty string: ""
|
|
72
|
+
*/
|
|
73
|
+
export pure circuit emptyString(): Opaque<"string"> {
|
|
74
|
+
return default<Opaque<"string">>;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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,
|
|
5
|
+
GovernorPermission,
|
|
6
|
+
TreasuryOperationType,
|
|
7
|
+
TreasuryTransactionType,
|
|
8
|
+
VoteChoice,
|
|
9
|
+
DelegationStatus,
|
|
10
|
+
EffectActionType,
|
|
11
|
+
EffectStatus,
|
|
12
|
+
ProposalStatus,
|
|
13
|
+
TimeComparison,
|
|
14
|
+
} = contractModule;
|
|
15
|
+
|
|
16
|
+
export default contractModule;
|
|
17
|
+
|
|
18
|
+
// Export Contract sep I'marately to preserve class type signature
|
|
19
|
+
export { Contract };
|
|
20
|
+
|
|
21
|
+
// Export other values
|
|
22
|
+
export {
|
|
23
|
+
ledger,
|
|
24
|
+
pureCircuits,
|
|
25
|
+
contractReferenceLocations,
|
|
26
|
+
GovernorPermission,
|
|
27
|
+
TreasuryOperationType,
|
|
28
|
+
TreasuryTransactionType,
|
|
29
|
+
VoteChoice,
|
|
30
|
+
DelegationStatus,
|
|
31
|
+
EffectActionType,
|
|
32
|
+
EffectStatus,
|
|
33
|
+
ProposalStatus,
|
|
34
|
+
TimeComparison,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export * from "./witnesses.js";
|