Skip to content

Commit c36728a

Browse files
committed
fix: remove init functions and fix errors
This greatly simplifies things as we can drop in template values where needed vs trying to initialize everything after the fact and require calling functions with extra information, e.g. trait contracts. From here we can review how to calculate liquid balance at proposal block, then complete the voting logic.
1 parent 91b41b2 commit c36728a

7 files changed

+48
-200
lines changed

Clarinet.toml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,6 @@ path = 'contracts/dao/proposals/aibtc-treasury-withdraw-stx.clar'
302302
clarity_version = 2
303303
epoch = 3.0
304304

305-
[contracts.aibtc-action-proposals-set-protocol-treasury]
306-
path = 'contracts/dao/proposals/aibtc-action-proposals-set-protocol-treasury.clar'
307-
clarity_version = 2
308-
epoch = 3.0
309-
310-
[contracts.aibtc-core-proposals-set-protocol-treasury]
311-
path = 'contracts/dao/proposals/aibtc-core-proposals-set-protocol-treasury.clar'
312-
clarity_version = 2
313-
epoch = 3.0
314-
315305
# dao traits
316306

317307
[contracts.aibtcdev-dao-traits-v1]

contracts/dao/extensions/aibtc-action-proposals.clar

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,38 @@
1616
;;
1717
(define-constant SELF (as-contract tx-sender))
1818
(define-constant VOTING_PERIOD u144) ;; 144 Bitcoin blocks, ~1 day
19-
(define-constant VOTING_QUORUM u66) ;; 66% of liquid supply (total supply - treasury)
19+
(define-constant VOTING_QUORUM u66) ;; 66% of liquid supply
2020

2121
;; error messages - authorization
2222
(define-constant ERR_NOT_DAO_OR_EXTENSION (err u1000))
2323

24-
;; error messages - initialization
25-
(define-constant ERR_NOT_INITIALIZED (err u1100))
26-
27-
;; error messages - treasury
28-
(define-constant ERR_TREASURY_CANNOT_BE_SELF (err u1200))
29-
(define-constant ERR_TREASURY_MISMATCH (err u1201))
30-
(define-constant ERR_TREASURY_CANNOT_BE_SAME (err u1202))
31-
3224
;; error messages - voting token
33-
(define-constant ERR_INSUFFICIENT_BALANCE (err u1300))
34-
(define-constant ERR_FETCHING_TOKEN_DATA (err u1301))
25+
(define-constant ERR_INSUFFICIENT_BALANCE (err u1100))
26+
(define-constant ERR_FETCHING_TOKEN_DATA (err u1101))
3527

3628
;; error messages - proposals
37-
(define-constant ERR_PROPOSAL_NOT_FOUND (err u1400))
38-
(define-constant ERR_PROPOSAL_STILL_ACTIVE (err u1401))
39-
(define-constant ERR_SAVING_PROPOSAL (err u1402))
40-
(define-constant ERR_PROPOSAL_ALREADY_CONCLUDED (err u1403))
41-
(define-constant ERR_RETRIEVING_START_BLOCK_HASH (err u1404))
29+
(define-constant ERR_PROPOSAL_NOT_FOUND (err u1200))
30+
(define-constant ERR_PROPOSAL_STILL_ACTIVE (err u1201))
31+
(define-constant ERR_SAVING_PROPOSAL (err u1202))
32+
(define-constant ERR_PROPOSAL_ALREADY_CONCLUDED (err u1203))
33+
(define-constant ERR_RETRIEVING_START_BLOCK_HASH (err u1204))
4234

4335
;; error messages - voting
44-
(define-constant ERR_VOTE_TOO_SOON (err u1500))
45-
(define-constant ERR_VOTE_TOO_LATE (err u1501))
46-
(define-constant ERR_ALREADY_VOTED (err u1502))
47-
(define-constant ERR_ZERO_VOTING_POWER (err u1503))
48-
(define-constant ERR_QUORUM_NOT_REACHED (err u1504))
36+
(define-constant ERR_VOTE_TOO_SOON (err u1300))
37+
(define-constant ERR_VOTE_TOO_LATE (err u1301))
38+
(define-constant ERR_ALREADY_VOTED (err u1302))
39+
(define-constant ERR_QUORUM_NOT_REACHED (err u1303))
4940

5041
;; error messages - actions
51-
(define-constant ERR_INVALID_ACTION (err u1600))
42+
(define-constant ERR_INVALID_ACTION (err u1400))
43+
44+
;; contracts used for voting calculations
45+
(define-constant VOTING_TOKEN_DEX .aibtc-token-dex)
46+
(define-constant VOTING_TOKEN_POOL .aibtc-bitflow-pool)
47+
(define-constant VOTING_TREASURY .aibtc-treasury)
5248

5349
;; data vars
5450
;;
55-
(define-data-var protocolTreasury principal SELF) ;; the treasury contract for protocol funds
5651
(define-data-var proposalCount uint u0) ;; total number of proposals
5752

5853
;; data maps
@@ -90,35 +85,13 @@
9085
(ok true)
9186
)
9287

93-
(define-public (set-protocol-treasury (treasury <treasury-trait>))
94-
(let
95-
(
96-
(treasuryContract (contract-of treasury))
97-
)
98-
(try! (is-dao-or-extension))
99-
;; cannot set treasury to self
100-
(asserts! (not (is-eq treasuryContract SELF)) ERR_TREASURY_CANNOT_BE_SELF)
101-
;; cannot set treasury to same value
102-
(asserts! (not (is-eq treasuryContract (var-get protocolTreasury))) ERR_TREASURY_CANNOT_BE_SAME)
103-
(print {
104-
notification: "set-protocol-treasury",
105-
payload: {
106-
treasury: treasuryContract
107-
}
108-
})
109-
(ok (var-set protocolTreasury treasuryContract))
110-
)
111-
)
112-
11388
(define-public (propose-action (action <action-trait>) (parameters (buff 2048)))
11489
(let
11590
(
11691
(newId (+ (var-get proposalCount) u1))
11792
(voterBalance (unwrap! (contract-call? .aibtc-token get-balance tx-sender) ERR_FETCHING_TOKEN_DATA))
11893
(liquidTokens (contract-call? .aibtc-token get-liquid-supply))
11994
)
120-
;; required variables must be set
121-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
12295
;; caller has the required balance
12396
(asserts! (> voterBalance u0) ERR_INSUFFICIENT_BALANCE)
12497
;; print proposal creation event
@@ -161,8 +134,6 @@
161134
(proposalBlockHash (unwrap! (get-block-hash proposalBlock) ERR_RETRIEVING_START_BLOCK_HASH))
162135
(senderBalance (unwrap! (at-block proposalBlockHash (contract-call? .aibtc-token get-balance tx-sender)) ERR_FETCHING_TOKEN_DATA))
163136
)
164-
;; required variables must be set
165-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
166137
;; caller has the required balance
167138
(asserts! (> senderBalance u0) ERR_INSUFFICIENT_BALANCE)
168139
;; proposal not still active
@@ -193,19 +164,15 @@
193164
)
194165
)
195166

196-
(define-public (conclude-proposal (proposalId uint) (action <action-trait>) (treasury <treasury-trait>))
167+
(define-public (conclude-proposal (proposalId uint) (action <action-trait>))
197168
(let
198169
(
199170
(proposalRecord (unwrap! (map-get? Proposals proposalId) ERR_PROPOSAL_NOT_FOUND))
200171
;; if VOTING_QUORUM <= ((votesFor * 100) / liquidTokens)
201172
(votePassed (<= VOTING_QUORUM (/ (* (get votesFor proposalRecord) u100) (get liquidTokens proposalRecord))))
202173
)
203-
;; required variables must be set
204-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
205174
;; verify extension still active in dao
206175
(try! (as-contract (is-dao-or-extension)))
207-
;; verify treasury matches protocol treasury
208-
(asserts! (is-eq (contract-of treasury) (var-get protocolTreasury)) ERR_TREASURY_MISMATCH)
209176
;; proposal past end block height
210177
(asserts! (>= burn-block-height (get endBlock proposalRecord)) ERR_PROPOSAL_STILL_ACTIVE)
211178
;; proposal not already concluded
@@ -248,12 +215,12 @@
248215
)
249216
)
250217

251-
252-
(define-read-only (get-protocol-treasury)
253-
(if (is-eq (var-get protocolTreasury) SELF)
254-
none
255-
(some (var-get protocolTreasury))
256-
)
218+
(define-read-only (get-linked-voting-contracts)
219+
{
220+
treasury: VOTING_TREASURY,
221+
token-dex: VOTING_TOKEN_DEX,
222+
token-pool: VOTING_TOKEN_POOL
223+
}
257224
)
258225

259226
(define-read-only (get-proposal (proposalId uint))
@@ -264,10 +231,6 @@
264231
(default-to u0 (map-get? VotingRecords {proposalId: proposalId, voter: voter}))
265232
)
266233

267-
(define-read-only (is-initialized)
268-
(not (is-eq (var-get protocolTreasury) SELF))
269-
)
270-
271234
(define-read-only (get-voting-period)
272235
VOTING_PERIOD
273236
)

contracts/dao/extensions/aibtc-core-proposals.clar

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
(define-constant SELF (as-contract tx-sender))
1919
(define-constant VOTING_PERIOD u144) ;; 144 Bitcoin blocks, ~1 day
20-
(define-constant VOTING_QUORUM u95) ;; 95% of liquid supply (total supply - treasury)
20+
(define-constant VOTING_QUORUM u95) ;; 95% of liquid supply
2121

2222
;; error messages - authorization
2323
(define-constant ERR_NOT_DAO_OR_EXTENSION (err u3000))
24-
25-
;; error messages - initialization
26-
(define-constant ERR_NOT_INITIALIZED (err u3100))
24+
(define-constant ERR_FETCHING_TOKEN_DATA (err u3001))
2725

2826
;; error messages - treasury
2927
(define-constant ERR_TREASURY_CANNOT_BE_SELF (err u3200))
@@ -49,12 +47,12 @@
4947
(define-constant ERR_VOTE_TOO_SOON (err u3500))
5048
(define-constant ERR_VOTE_TOO_LATE (err u3501))
5149
(define-constant ERR_ALREADY_VOTED (err u3502))
52-
(define-constant ERR_QUORUM_NOT_REACHED (err u3504))
50+
(define-constant ERR_QUORUM_NOT_REACHED (err u3503))
5351

54-
;; data vars
55-
;;
56-
(define-data-var protocolTreasury principal SELF) ;; the treasury contract for protocol funds
57-
(define-data-var votingToken principal SELF) ;; the FT contract used for voting
52+
;; contracts used for voting calculations
53+
(define-constant VOTING_TOKEN_DEX .aibtc-token-dex)
54+
(define-constant VOTING_TOKEN_POOL .aibtc-bitflow-pool)
55+
(define-constant VOTING_TREASURY .aibtc-treasury)
5856

5957
;; data maps
6058
;;
@@ -88,60 +86,13 @@
8886
(ok true)
8987
)
9088

91-
(define-public (set-protocol-treasury (treasury <treasury-trait>))
92-
(let
93-
(
94-
(treasuryContract (contract-of treasury))
95-
)
96-
(try! (is-dao-or-extension))
97-
;; cannot set treasury to self
98-
(asserts! (not (is-eq treasuryContract SELF)) ERR_TREASURY_CANNOT_BE_SELF)
99-
;; cannot set treasury to same value
100-
(asserts! (not (is-eq treasuryContract (var-get protocolTreasury))) ERR_TREASURY_CANNOT_BE_SAME)
101-
(print {
102-
notification: "set-protocol-treasury",
103-
payload: {
104-
treasury: treasuryContract
105-
}
106-
})
107-
(ok (var-set protocolTreasury treasuryContract))
108-
)
109-
)
110-
111-
(define-public (set-voting-token (token <ft-trait>))
112-
(let
113-
(
114-
(tokenContract (contract-of token))
115-
)
116-
(try! (is-dao-or-extension))
117-
;; cannot set token to self
118-
(asserts! (not (is-eq tokenContract SELF)) ERR_TOKEN_CANNOT_BE_SELF)
119-
;; cannot set token to same value
120-
(asserts! (not (is-eq tokenContract (var-get votingToken))) ERR_TOKEN_CANNOT_BE_SAME)
121-
;; cannot set token if already set once
122-
(asserts! (is-eq (var-get votingToken) SELF) ERR_TOKEN_ALREADY_INITIALIZED)
123-
(print {
124-
notification: "set-voting-token",
125-
payload: {
126-
token: tokenContract
127-
}
128-
})
129-
(ok (var-set votingToken tokenContract))
130-
)
131-
)
132-
133-
(define-public (create-proposal (proposal <proposal-trait>) (token <ft-trait>))
89+
(define-public (create-proposal (proposal <proposal-trait>))
13490
(let
13591
(
13692
(proposalContract (contract-of proposal))
137-
(tokenContract (contract-of token))
13893
)
139-
;; required variables must be set
140-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
141-
;; token matches set voting token
142-
(asserts! (is-eq tokenContract (var-get votingToken)) ERR_TOKEN_MISMATCH)
14394
;; caller has the required balance
144-
(asserts! (> (try! (contract-call? token get-balance tx-sender)) u0) ERR_INSUFFICIENT_BALANCE)
95+
(asserts! (> (unwrap! (contract-call? .aibtc-token get-balance tx-sender) ERR_FETCHING_TOKEN_DATA) u0) ERR_INSUFFICIENT_BALANCE)
14596
;; proposal was not already executed
14697
(asserts! (is-none (contract-call? .aibtcdev-base-dao executed-at proposal)) ERR_PROPOSAL_ALREADY_EXECUTED)
14798
;; print proposal creation event
@@ -168,21 +119,16 @@
168119
}) ERR_SAVING_PROPOSAL))
169120
))
170121

171-
(define-public (vote-on-proposal (proposal <proposal-trait>) (token <ft-trait>) (vote bool))
122+
(define-public (vote-on-proposal (proposal <proposal-trait>) (vote bool))
172123
(let
173124
(
174125
(proposalContract (contract-of proposal))
175126
(proposalRecord (unwrap! (map-get? Proposals proposalContract) ERR_PROPOSAL_NOT_FOUND))
176127
(proposalBlock (get startBlock proposalRecord))
177128
(proposalBlockHash (unwrap! (get-block-hash proposalBlock) ERR_RETRIEVING_START_BLOCK_HASH))
178-
(tokenContract (contract-of token))
179129
(senderBalanceResponse (at-block proposalBlockHash (contract-call? .aibtc-token get-balance tx-sender)))
180130
(senderBalance (unwrap-panic senderBalanceResponse))
181131
)
182-
;; required variables must be set
183-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
184-
;; token matches set voting token
185-
(asserts! (is-eq tokenContract (var-get votingToken)) ERR_TOKEN_MISMATCH)
186132
;; caller has the required balance
187133
(asserts! (> senderBalance u0) ERR_INSUFFICIENT_BALANCE)
188134
;; proposal was not already executed
@@ -215,21 +161,15 @@
215161
)
216162
)
217163

218-
(define-public (conclude-proposal (proposal <proposal-trait>) (treasury <treasury-trait>) (token <ft-trait>))
164+
(define-public (conclude-proposal (proposal <proposal-trait>))
219165
(let
220166
(
221167
(proposalContract (contract-of proposal))
222168
(proposalRecord (unwrap! (map-get? Proposals proposalContract) ERR_PROPOSAL_NOT_FOUND))
223-
(tokenContract (contract-of token))
224-
(tokenTotalSupply (try! (contract-call? token get-total-supply)))
225-
(treasuryContract (contract-of treasury))
226-
(treasuryBalance (try! (contract-call? token get-balance treasuryContract)))
169+
(tokenTotalSupply (unwrap! (contract-call? .aibtc-token get-total-supply) ERR_FETCHING_TOKEN_DATA))
170+
(treasuryBalance (unwrap! (contract-call? .aibtc-token get-balance .aibtc-treasury) ERR_FETCHING_TOKEN_DATA))
227171
(votePassed (> (get votesFor proposalRecord) (* tokenTotalSupply (- u100 treasuryBalance) VOTING_QUORUM)))
228172
)
229-
;; required variables must be set
230-
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
231-
;; verify treasury matches protocol treasury
232-
(asserts! (is-eq treasuryContract (var-get protocolTreasury)) ERR_TREASURY_MISMATCH)
233173
;; proposal was not already executed
234174
(asserts! (is-none (contract-call? .aibtcdev-base-dao executed-at proposal)) ERR_PROPOSAL_ALREADY_EXECUTED)
235175
;; proposal past end block height
@@ -261,18 +201,12 @@
261201
;; read only functions
262202
;;
263203

264-
(define-read-only (get-protocol-treasury)
265-
(if (is-eq (var-get protocolTreasury) SELF)
266-
none
267-
(some (var-get protocolTreasury))
268-
)
269-
)
270-
271-
(define-read-only (get-voting-token)
272-
(if (is-eq (var-get votingToken) SELF)
273-
none
274-
(some (var-get votingToken))
275-
)
204+
(define-read-only (get-linked-voting-contracts)
205+
{
206+
treasury: VOTING_TREASURY,
207+
token-dex: VOTING_TOKEN_DEX,
208+
token-pool: VOTING_TOKEN_POOL
209+
}
276210
)
277211

278212
(define-read-only (get-proposal (proposal principal))
@@ -283,14 +217,6 @@
283217
(default-to u0 (map-get? VotingRecords {proposal: proposal, voter: voter}))
284218
)
285219

286-
(define-read-only (is-initialized)
287-
;; check if the required variables are set
288-
(not (or
289-
(is-eq (var-get votingToken) SELF)
290-
(is-eq (var-get protocolTreasury) SELF)
291-
))
292-
)
293-
294220
(define-read-only (get-voting-period)
295221
VOTING_PERIOD
296222
)

contracts/dao/proposals/aibtc-action-proposals-set-protocol-treasury.clar

Lines changed: 0 additions & 5 deletions
This file was deleted.

contracts/dao/proposals/aibtc-base-bootstrap-initialization.clar

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
{extension: .aibtc-action-toggle-resource-by-name, enabled: true}
2929
)
3030
))
31-
;; initialize action proposals
32-
(try! (contract-call? .aibtc-action-proposals set-protocol-treasury .aibtc-treasury))
33-
;; initialize core proposals
34-
(try! (contract-call? .aibtc-core-proposals set-protocol-treasury .aibtc-treasury))
3531
;; send DAO manifest as onchain message
3632
(try! (contract-call? .aibtc-onchain-messaging send DAO_MANIFEST true))
3733
;; allow assets in treasury

contracts/dao/proposals/aibtc-core-proposals-set-protocol-treasury.clar

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)