|
1 |
| - |
2 |
| -;; title: aibtcdev-basedao |
3 |
| -;; version: |
4 |
| -;; summary: |
5 |
| -;; description: |
| 1 | +;; title: aibtcdev-dao |
| 2 | +;; version: 1.0.0 |
| 3 | +;; summary: An ExecutorDAO implementation for aibtcdev |
6 | 4 |
|
7 | 5 | ;; traits
|
8 | 6 | ;;
|
9 | 7 |
|
10 |
| -;; token definitions |
11 |
| -;; |
| 8 | +(use-trait proposal-trait .aibtcdev-proposal-trait.proposal-trait) |
| 9 | +(use-trait extension-trait .aibtcdev-extension-trait.extension-trait) |
12 | 10 |
|
13 | 11 | ;; constants
|
14 | 12 | ;;
|
15 | 13 |
|
| 14 | +(define-constant ERR_UNAUTHORIZED (err u900)) |
| 15 | +(define-constant ERR_ALREADY_EXECUTED (err u901)) |
| 16 | +(define-constant ERR_INVALID_EXTENSION (err u902)) |
| 17 | + |
16 | 18 | ;; data vars
|
17 | 19 | ;;
|
18 | 20 |
|
| 21 | +;; used for initial construction, set to contract itself after |
| 22 | +(define-data-var executive principal tx-sender) |
| 23 | + |
19 | 24 | ;; data maps
|
20 | 25 | ;;
|
21 | 26 |
|
| 27 | +;; tracks block height of executed proposals |
| 28 | +(define-map ExecutedProposals principal uint) |
| 29 | +;; tracks enabled status of extensions |
| 30 | +(define-map Extensions principal bool) |
| 31 | + |
22 | 32 | ;; public functions
|
23 | 33 | ;;
|
24 | 34 |
|
| 35 | +;; initial construction of the DAO |
| 36 | +(define-public (construct (proposal <proposal-trait>)) |
| 37 | + (let |
| 38 | + ((sender tx-sender)) |
| 39 | + (asserts! (is-eq sender (var-get executive)) ERR_UNAUTHORIZED) |
| 40 | + (var-set executive (as-contract tx-sender)) |
| 41 | + (as-contract (execute proposal sender)) |
| 42 | + ) |
| 43 | +) |
| 44 | + |
| 45 | +;; execute Clarity code in a proposal |
| 46 | +(define-public (execute (proposal <proposal-trait>) (sender principal)) |
| 47 | + (begin |
| 48 | + (try! (is-self-or-extension)) |
| 49 | + (asserts! (map-insert ExecutedProposals (contract-of proposal) block-height) ERR_ALREADY_EXECUTED) |
| 50 | + (print {event: "execute", proposal: proposal}) |
| 51 | + (as-contract (contract-call? proposal execute sender)) |
| 52 | + ) |
| 53 | +) |
| 54 | + |
| 55 | +;; add an extension or update the status of an existing one |
| 56 | +(define-public (set-extension (extension principal) (enabled bool)) |
| 57 | + (begin |
| 58 | + (try! (is-self-or-extension)) |
| 59 | + (print {event: "extension", enabled: enabled, extension: extension,}) |
| 60 | + (ok (map-set Extensions extension enabled)) |
| 61 | + ) |
| 62 | +) |
| 63 | + |
| 64 | +;; add multiple extensions or update the status of existing ones |
| 65 | +(define-public (set-extensions (extensionList (list 200 {extension: principal, enabled: bool}))) |
| 66 | + (begin |
| 67 | + (try! (is-self-or-extension)) |
| 68 | + (ok (map set-extensions-iter extensionList)) |
| 69 | + ) |
| 70 | +) |
| 71 | + |
| 72 | +;; request a callback from an extension |
| 73 | +(define-public (request-extension-callback (extension <extension-trait>) (memo (buff 34))) |
| 74 | + (let |
| 75 | + ((sender tx-sender)) |
| 76 | + (asserts! (is-extension contract-caller) ERR_INVALID_EXTENSION) |
| 77 | + (asserts! (is-eq contract-caller (contract-of extension)) ERR_INVALID_EXTENSION) |
| 78 | + (as-contract (contract-call? extension callback sender memo)) |
| 79 | + ) |
| 80 | +) |
| 81 | + |
25 | 82 | ;; read only functions
|
26 | 83 | ;;
|
27 | 84 |
|
| 85 | +(define-read-only (is-extension (extension principal)) |
| 86 | + (default-to false (map-get? Extensions extension)) |
| 87 | +) |
| 88 | + |
| 89 | +(define-read-only (executed-at (proposal <proposal-trait>)) |
| 90 | + (map-get? ExecutedProposals (contract-of proposal)) |
| 91 | +) |
| 92 | + |
28 | 93 | ;; private functions
|
29 | 94 | ;;
|
30 | 95 |
|
| 96 | +;; authorization check |
| 97 | +(define-private (is-self-or-extension) |
| 98 | + (ok (asserts! (or (is-eq tx-sender (as-contract tx-sender)) (is-extension contract-caller)) ERR_UNAUTHORIZED)) |
| 99 | +) |
| 100 | + |
| 101 | +;; set extensions helper function |
| 102 | +(define-private (set-extensions-iter (item {extension: principal, enabled: bool})) |
| 103 | + (begin |
| 104 | + (print {event: "extension", enabled: (get enabled item), extension: (get extension item)}) |
| 105 | + (map-set Extensions (get extension item) (get enabled item)) |
| 106 | + ) |
| 107 | +) |
0 commit comments