Skip to content

Commit

Permalink
Merge pull request #17 from aibtcdev/refactor/simplify-token-def
Browse files Browse the repository at this point in the history
hardcode token where possible, updates contracts and traits
  • Loading branch information
whoabuddy authored Jan 14, 2025
2 parents 70ba26f + 0726495 commit 86248e8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 92 deletions.
10 changes: 0 additions & 10 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,11 @@ path = 'contracts/dao/proposals/aibtc-action-proposals-set-protocol-treasury.cla
clarity_version = 2
epoch = 3.0

[contracts.aibtc-action-proposals-set-voting-token]
path = 'contracts/dao/proposals/aibtc-action-proposals-set-voting-token.clar'
clarity_version = 2
epoch = 3.0

[contracts.aibtc-core-proposals-set-protocol-treasury]
path = 'contracts/dao/proposals/aibtc-core-proposals-set-protocol-treasury.clar'
clarity_version = 2
epoch = 3.0

[contracts.aibtc-core-proposals-set-voting-token]
path = 'contracts/dao/proposals/aibtc-core-proposals-set-voting-token.clar'
clarity_version = 2
epoch = 3.0

# dao traits

[contracts.aibtcdev-dao-traits-v1]
Expand Down
68 changes: 12 additions & 56 deletions contracts/dao/extensions/aibtc-action-proposals.clar
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@
(define-constant ERR_TREASURY_CANNOT_BE_SAME (err u1202))

;; error messages - voting token
(define-constant ERR_TOKEN_ALREADY_INITIALIZED (err u1300))
(define-constant ERR_TOKEN_MISMATCH (err u1301))
(define-constant ERR_INSUFFICIENT_BALANCE (err u1302))
(define-constant ERR_TOKEN_CANNOT_BE_SELF (err u1303))
(define-constant ERR_TOKEN_CANNOT_BE_SAME (err u1304))
(define-constant ERR_INSUFFICIENT_BALANCE (err u1300))
(define-constant ERR_FETCHING_TOKEN_DATA (err u1301))

;; error messages - proposals
(define-constant ERR_PROPOSAL_NOT_FOUND (err u1400))
Expand All @@ -56,7 +53,6 @@
;; data vars
;;
(define-data-var protocolTreasury principal SELF) ;; the treasury contract for protocol funds
(define-data-var votingToken principal SELF) ;; the FT contract used for voting
(define-data-var proposalCount uint u0) ;; total number of proposals

;; data maps
Expand Down Expand Up @@ -113,40 +109,16 @@
)
)

(define-public (set-voting-token (token <ft-trait>))
(define-public (propose-action (action <action-trait>) (parameters (buff 2048)))
(let
(
(tokenContract (contract-of token))
)
(try! (is-dao-or-extension))
;; cannot set token to self
(asserts! (not (is-eq tokenContract SELF)) ERR_TOKEN_CANNOT_BE_SELF)
;; cannot set token to same value
(asserts! (not (is-eq tokenContract (var-get votingToken))) ERR_TOKEN_CANNOT_BE_SAME)
;; cannot set token if already set once
(asserts! (is-eq (var-get votingToken) SELF) ERR_TOKEN_ALREADY_INITIALIZED)
(print {
notification: "set-voting-token",
payload: {
token: tokenContract
}
})
(ok (var-set votingToken tokenContract))
)
)

(define-public (propose-action (action <action-trait>) (parameters (buff 2048)) (token <ft-trait>))
(let
(
(tokenContract (contract-of token))
(newId (+ (var-get proposalCount) u1))
(voterBalance (unwrap! (contract-call? .aibtc-token get-balance tx-sender) ERR_FETCHING_TOKEN_DATA))
)
;; required variables must be set
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
;; token matches set voting token
(asserts! (is-eq tokenContract (var-get votingToken)) ERR_TOKEN_MISMATCH)
;; caller has the required balance
(asserts! (> (try! (contract-call? token get-balance tx-sender)) u0) ERR_INSUFFICIENT_BALANCE)
(asserts! (> voterBalance u0) ERR_INSUFFICIENT_BALANCE)
;; print proposal creation event
(print {
notification: "propose-action",
Expand Down Expand Up @@ -178,23 +150,19 @@
)
)

(define-public (vote-on-proposal (proposalId uint) (token <ft-trait>) (vote bool))
(define-public (vote-on-proposal (proposalId uint) (vote bool))
(let
(
(proposalRecord (unwrap! (map-get? Proposals proposalId) ERR_PROPOSAL_NOT_FOUND))
(proposalBlock (get startBlock proposalRecord))
(proposalBlockHash (unwrap! (get-block-hash proposalBlock) ERR_RETRIEVING_START_BLOCK_HASH))
(tokenContract (contract-of token))
(senderBalanceResponse (at-block proposalBlockHash (contract-call? .aibtc-token get-balance tx-sender)))
(senderBalance (unwrap-panic senderBalanceResponse))
(senderBalance (unwrap! (at-block proposalBlockHash (contract-call? .aibtc-token get-balance tx-sender)) ERR_FETCHING_TOKEN_DATA))
)
;; required variables must be set
(asserts! (is-initialized) ERR_NOT_INITIALIZED)
;; token matches set voting token
(asserts! (is-eq tokenContract (var-get votingToken)) ERR_TOKEN_MISMATCH)
;; caller has the required balance
(asserts! (> senderBalance u0) ERR_INSUFFICIENT_BALANCE)
;; proposal is still active
;; proposal not still active
(asserts! (>= burn-block-height (get startBlock proposalRecord)) ERR_VOTE_TOO_SOON)
(asserts! (< burn-block-height (get endBlock proposalRecord)) ERR_VOTE_TOO_LATE)
;; proposal not already concluded
Expand Down Expand Up @@ -222,14 +190,13 @@
)
)

(define-public (conclude-proposal (proposalId uint) (action <action-trait>) (treasury <treasury-trait>) (token <ft-trait>))
(define-public (conclude-proposal (proposalId uint) (action <action-trait>) (treasury <treasury-trait>))
(let
(
(proposalRecord (unwrap! (map-get? Proposals proposalId) ERR_PROPOSAL_NOT_FOUND))
(tokenContract (contract-of token))
(tokenTotalSupply (try! (contract-call? token get-total-supply)))
(tokenTotalSupply (unwrap! (contract-call? .aibtc-token get-total-supply) ERR_FETCHING_TOKEN_DATA))
(treasuryContract (contract-of treasury))
(treasuryBalance (try! (contract-call? token get-balance treasuryContract)))
(treasuryBalance (unwrap! (contract-call? .aibtc-token get-balance treasuryContract) ERR_FETCHING_TOKEN_DATA))
(votePassed (> (get votesFor proposalRecord) (* tokenTotalSupply (- u100 treasuryBalance) VOTING_QUORUM)))
)
;; required variables must be set
Expand Down Expand Up @@ -277,13 +244,6 @@
)
)

(define-read-only (get-voting-token)
(if (is-eq (var-get votingToken) SELF)
none
(some (var-get votingToken))
)
)

(define-read-only (get-proposal (proposalId uint))
(map-get? Proposals proposalId)
)
Expand All @@ -293,11 +253,7 @@
)

(define-read-only (is-initialized)
;; check if the required variables are set
(not (or
(is-eq (var-get votingToken) SELF)
(is-eq (var-get protocolTreasury) SELF)
))
(not (is-eq (var-get protocolTreasury) SELF))
)

(define-read-only (get-voting-period)
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao/extensions/aibtc-payments-invoices.clar
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
(define-data-var totalRevenue uint u0)

;; dao can update payment address
(define-data-var paymentAddress principal .aibtc-bank-account)
(define-data-var paymentAddress principal .aibtc-treasury)

;; data maps
;;
Expand Down

This file was deleted.

14 changes: 9 additions & 5 deletions contracts/dao/proposals/aibtc-base-bootstrap-initialization.clar
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

(define-public (execute (sender principal))
(begin
;; set initial extensions
;; set initial extensions list
(try! (contract-call? .aibtcdev-base-dao set-extensions
(list
{extension: .aibtc-action-proposals, enabled: true}
{extension: .aibtc-bank-account, enabled: true}
{extension: .aibtc-core-proposals, enabled: true}
{extension: .aibtc-onchain-messaging, enabled: true}
{extension: .aibtc-payments-invoices, enabled: true}
{extension: .aibtc-token-owner, enabled: true}
{extension: .aibtc-treasury, enabled: true}
{extension: .set-account-holder, enabled: true}
{extension: .send-message, enabled: true}
)
))

;; initialize action proposals
(try! (contract-call? .aibtc-action-proposals set-protocol-treasury .aibtc-treasury))
(try! (contract-call? .aibtc-action-proposals set-voting-token .aibtc-token))
;; initialize core proposals
(try! (contract-call? .aibtc-core-proposals set-protocol-treasury .aibtc-treasury))
;; send DAO manifest as onchain message
(try! (contract-call? .aibtc-onchain-messaging send DAO_MANIFEST true))
;; allow assets in treasury
(try! (contract-call? .aibtc-treasury allow-asset .aibtc-token true))
;; print manifest
(print DAO_MANIFEST)
(ok true)
Expand Down

This file was deleted.

13 changes: 3 additions & 10 deletions contracts/dao/traits/aibtcdev-dao-traits-v1.clar
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,22 @@
;; @param treasury the treasury contract principal
;; @returns (response bool uint)
(set-protocol-treasury (<treasury>) (response bool uint))
;; set the voting token contract
;; @param token the token contract principal
;; @returns (response bool uint)
(set-voting-token (<ft-trait>) (response bool uint))
;; propose a new action
;; @param action the action contract
;; @param parameters encoded action parameters
;; @param token the voting token contract
;; @returns (response bool uint)
(propose-action (<action> (buff 2048) <ft-trait>) (response bool uint))
(propose-action (<action> (buff 2048)) (response bool uint))
;; vote on an existing proposal
;; @param proposal the proposal id
;; @param token the voting token contract
;; @param vote true for yes, false for no
;; @returns (response bool uint)
(vote-on-proposal (uint <ft-trait> bool) (response bool uint))
(vote-on-proposal (uint bool) (response bool uint))
;; conclude a proposal after voting period
;; @param proposal the proposal id
;; @param action the action contract
;; @param treasury the treasury contract
;; @param token the voting token contract
;; @returns (response bool uint)
(conclude-proposal (uint <action> <treasury> <ft-trait>) (response bool uint))
(conclude-proposal (uint <action> <treasury>) (response bool uint))
))

(define-trait bank-account (
Expand Down

0 comments on commit 86248e8

Please sign in to comment.