/
The-Fapping-DAO.clar
296 lines (253 loc) · 9.91 KB
/
The-Fapping-DAO.clar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;; Title: Multi-Fap
(use-trait executor-trait 'SP3XD84X3PE79SHJAZCDW1V5E9EA8JSKRBPEKAEK7.multisafe-traits.executor-trait)
(use-trait safe-trait 'SP3XD84X3PE79SHJAZCDW1V5E9EA8JSKRBPEKAEK7.multisafe-traits.safe-trait)
(impl-trait 'SP3XD84X3PE79SHJAZCDW1V5E9EA8JSKRBPEKAEK7.multisafe-traits.safe-trait)
;; Errors
(define-constant ERR-CALLER-MUST-BE-SELF (err u100))
(define-constant ERR-OWNER-ALREADY-EXISTS (err u110))
(define-constant ERR-OWNER-NOT-EXISTS (err u120))
(define-constant ERR-UNAUTHORIZED-SENDER (err u130))
(define-constant ERR-TX-NOT-FOUND (err u140))
(define-constant ERR-TX-ALREADY-CONFIRMED-BY-OWNER (err u150))
(define-constant ERR-TX-INVALID-EXECUTOR (err u160))
(define-constant ERR-INVALID-SAFE (err u170))
(define-constant ERR-TX-CONFIRMED (err u180))
(define-constant ERR-TX-NOT-CONFIRMED-BY-SENDER (err u190))
(define-constant ERR-AT-LEAST-ONE-OWNER-REQUIRED (err u200))
(define-constant ERR-MIN-CONFIRMATION-CANT-BE-ZERO (err u210))
;; Principal of deployed contract
(define-constant SELF (as-contract tx-sender))
;; --- Version
;; Version string
(define-constant VERSION "0.0.69.doge")
;; Returns version of the safe contract
;; @returns string-ascii
(define-read-only (get-version)
VERSION
)
;; --- Owners
;; The owners list
(define-data-var owners (list 20 principal) (list))
;; Returns owner list
;; @returns list
(define-read-only (get-owners)
(var-get owners)
)
;; Private function to push a new member to the owners list
;; @params owner
;; @returns bool
(define-private (add-owner-internal (owner principal))
(var-set owners (unwrap-panic (as-max-len? (append (var-get owners) owner) u20)))
)
;; Adds new owner
;; @restricted to SELF
;; @params owner
;; @returns (response bool)
(define-public (add-owner (owner principal))
(begin
(asserts! (is-eq tx-sender SELF) ERR-CALLER-MUST-BE-SELF)
(asserts! (is-none (index-of (var-get owners) owner)) ERR-OWNER-ALREADY-EXISTS)
(ok (add-owner-internal owner))
)
)
;; A helper variable to filter owners while removing one
(define-data-var rem-owner principal tx-sender)
;; Returns a new owner list removing the given as parameter
;; @param owner
;; @returns list
(define-private (remove-owner-filter (owner principal)) (not (is-eq owner (var-get rem-owner))))
;; Removes an owner
;; @restricted to SELF
;; @params owner
;; @returns (response bool)
(define-public (remove-owner (owner principal))
(let
(
(owners-list (var-get owners))
)
(asserts! (is-eq tx-sender SELF) ERR-CALLER-MUST-BE-SELF)
(asserts! (is-some (index-of owners-list owner)) ERR-OWNER-NOT-EXISTS)
(asserts! (> (len owners-list) u1) ERR-AT-LEAST-ONE-OWNER-REQUIRED)
(var-set rem-owner owner)
(ok (var-set owners (unwrap-panic (as-max-len? (filter remove-owner-filter owners-list) u20))))
)
)
;; --- Minimum confirmation requirement
(define-data-var min-confirmation uint u0)
;; Returns minimum confirmation
;; @returns uint
(define-read-only (get-min-confirmation)
(var-get min-confirmation)
)
;; Private function to set minimum required confirmation number
;; @params value
;; return bool
(define-private (set-min-confirmation-internal (value uint))
(var-set min-confirmation value)
)
;; Updates minimum required confirmation number
;; @restricted to SELF
;; @params value
;; @returns (response bool)
(define-public (set-min-confirmation (value uint))
(begin
(asserts! (is-eq tx-sender SELF) ERR-CALLER-MUST-BE-SELF)
(asserts! (> value u0) ERR-MIN-CONFIRMATION-CANT-BE-ZERO)
(ok (set-min-confirmation-internal value))
)
)
;; --- Nonce
;; Incrementing number to use as id for new transactions
(define-data-var nonce uint u0)
;; Returns nonce
;; @returns uint
(define-read-only (get-nonce)
(var-get nonce)
)
;; Increases nonce
;; @returns bool
(define-private (increase-nonce)
(var-set nonce (+ (var-get nonce) u1))
)
;; --- Transactions
(define-map transactions
uint
{
executor: principal,
confirmations: (list 20 principal),
confirmed: bool,
param-p: principal,
param-u: uint
}
)
;; Private function to insert a new transaction into transactions map
;; @params executor ; contract address to be executed
;; @params param-p ; principal parameter to be passed to the executor function
;; @params param-u ; uint argument to be passed to the executor function
;; @returns uint
(define-private (add (executor <executor-trait>) (param-p principal) (param-u uint))
(let
(
(tx-id (get-nonce))
)
(map-insert transactions tx-id {executor: (contract-of executor), confirmations: (list), confirmed: false, param-p: param-p, param-u: param-u})
(increase-nonce)
tx-id
)
)
;; Returns a transaction by id
;; @params tx-id ; transaction id
;; @returns tuple
(define-read-only (get-transaction (tx-id uint))
(unwrap-panic (map-get? transactions tx-id))
)
;; Returns transactions by ids
;; @params tx-ids ; transaction id list
;; @returns list
(define-read-only (get-transactions (tx-ids (list 20 uint)))
(map get-transaction tx-ids)
)
;; A helper variable to filter confirmations while removing one
(define-data-var rem-confirmation principal tx-sender)
;; Returns a new confirmations list removing the given as parameter
;; @param owner
;; @returns list
(define-private (remove-confirmation-filter (owner principal)) (not (is-eq owner (var-get rem-confirmation))))
;; Allows an owner to remove their confirmation on the transaction
;; @restricted to owner who confirmed the transaction before
;; @params tx-id ; transaction id
;; @returns (response bool)
(define-public (revoke (tx-id uint))
(let
(
(tx (unwrap! (map-get? transactions tx-id) ERR-TX-NOT-FOUND))
(confirmations (get confirmations tx))
)
(asserts! (is-eq (get confirmed tx) false) ERR-TX-CONFIRMED)
(asserts! (is-some (index-of confirmations tx-sender)) ERR-TX-NOT-CONFIRMED-BY-SENDER)
(var-set rem-confirmation tx-sender)
(let
(
(new-confirmations (unwrap-panic (as-max-len? (filter remove-confirmation-filter confirmations) u20)))
(new-tx (merge tx {confirmations: new-confirmations}))
)
(map-set transactions tx-id new-tx)
(print {action: "multisafe-revoke", sender: tx-sender, tx-id: tx-id})
(ok true)
)
)
)
;; Allows an owner to confirm a tranaction. If the transaction reaches sufficient confirmation number
;; then the executor specified on the transaction gets triggered.
;; @restricted to owners who hasn't confirmed the transaction yet
;; @params executor ; contract address to be executed
;; @params safe ; address of safe instance / SELF
;; @returns (response bool)
(define-public (confirm (tx-id uint) (executor <executor-trait>) (safe <safe-trait>))
(begin
(asserts! (is-some (index-of (var-get owners) tx-sender)) ERR-UNAUTHORIZED-SENDER)
(asserts! (is-eq (contract-of safe) SELF) ERR-INVALID-SAFE)
(let
(
(tx (unwrap! (map-get? transactions tx-id) ERR-TX-NOT-FOUND))
(confirmations (get confirmations tx))
)
(asserts! (is-none (index-of confirmations tx-sender)) ERR-TX-ALREADY-CONFIRMED-BY-OWNER)
(asserts! (is-eq (get executor tx) (contract-of executor)) ERR-TX-INVALID-EXECUTOR)
(let
(
(new-confirmations (unwrap-panic (as-max-len? (append confirmations tx-sender) u20)))
(confirmed (>= (len new-confirmations) (var-get min-confirmation)))
(new-tx (merge tx {confirmations: new-confirmations, confirmed: confirmed}))
)
(map-set transactions tx-id new-tx)
(and confirmed (try! (as-contract (contract-call? executor execute safe (get param-p tx) (get param-u tx)))))
(print {action: "multisafe-confirmation", sender: tx-sender, tx-id: tx-id, confirmed: confirmed})
(ok confirmed)
)
)
)
)
;; Allows an owner to add a new transaction and confirms it for the owner who submitted it.
;; So, a newly submitted transaction gets one confirmation automatically. If the safe's minimum
;; required confirmation number is one then the transaction gets executed in this step.
;; @restricted to owners
;; @params executor ; contract address to be executed
;; @params safe ; address of safe instance / SELF
;; @params param-p ; principal parameter to be passed to the executor function
;; @params param-u ; uint argument to be passed to the executor function
;; @returns (response uint)
(define-public (submit (executor <executor-trait>) (safe <safe-trait>) (param-p principal) (param-u uint))
(begin
(asserts! (is-some (index-of (var-get owners) tx-sender)) ERR-UNAUTHORIZED-SENDER)
(asserts! (is-eq (contract-of safe) SELF) ERR-INVALID-SAFE)
(let
((tx-id (add executor param-p param-u)))
(print {action: "multisafe-submit", sender: tx-sender, tx-id: tx-id, executor: executor, param-p: param-p, param-u: param-u})
(unwrap-panic (confirm tx-id executor safe))
(ok tx-id)
)
)
)
;; Safe initializer
;; @params o ; owners list
;; @params m ; minimum required confirmation number
(define-private (init (o (list 20 principal)) (m uint))
(begin
(map add-owner-internal o)
(set-min-confirmation-internal m)
(print {action: "multisafe-init"})
)
)
(init (list
'SP69MS8W17WWT6MNH8AB4A7BMY5AX6MAMWD89CCR
'SP2V6HR6CTCKYSBC47F1V6D1FMCSHJ0SXM0MJZYVY
'SP3Q1CZZNXM95DZVTB7VBND4FW4B2E0YXM2KJ7FAH
'SP1XPG9QFX5M95G36SGN9R8YJ4KJ0JB7ZXNH892N6
'SPN4Y5QPGQA8882ZXW90ADC2DHYXMSTN8VAR8C3X
'SP2E03GHWY145XMFDTHX4Z913EADP4RMZ0P0DCTE9
'SP3XJTH5TJ3PEE67T02AA4DSBC89A80S028SQS769
'SPBQ5AB9W17MHXJYP67CZNYBPKFPMTFN1JBHHV2G
'SP2F40S465JTD7AMZ2X9SMN229617HZ9YB0HHY98A
'SP3XVFQ1AB7DD5N19GS0412CG4JG7XWSBYAG98PVW
) u4)