Skip to content

Commit

Permalink
Merge pull request #20 from aibtcdev/feat/add-contract-tests
Browse files Browse the repository at this point in the history
Add contract tests
  • Loading branch information
whoabuddy authored Jan 15, 2025
2 parents 2f52f35 + e1fac36 commit cf57507
Show file tree
Hide file tree
Showing 56 changed files with 1,024 additions and 603 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/clarinet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
- name: "Execute unit tests"
run: npm run test:report
- name: "Upload code coverage"
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
files: ./coverage.lcov
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}
12 changes: 7 additions & 5 deletions contracts/dao/extensions/aibtc-action-proposals.clar
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
createdAt: uint, ;; block height
caller: principal, ;; contract caller
creator: principal, ;; proposal creator (tx-sender)
startBlock: uint, ;; block height
endBlock: uint, ;; block height
startBlockStx: uint, ;; block height for at-block calls
startBlock: uint, ;; burn block height
endBlock: uint, ;; burn block height
votesFor: uint, ;; total votes for
votesAgainst: uint, ;; total votes against
liquidTokens: uint, ;; liquid tokens
Expand Down Expand Up @@ -94,6 +95,7 @@
parameters: parameters,
creator: tx-sender,
liquidTokens: liquidTokens,
startBlockStx: block-height,
startBlock: burn-block-height,
endBlock: (+ burn-block-height VOTING_PERIOD)
}
Expand All @@ -105,6 +107,7 @@
createdAt: burn-block-height,
caller: contract-caller,
creator: tx-sender,
startBlockStx: block-height,
startBlock: burn-block-height,
endBlock: (+ burn-block-height VOTING_PERIOD),
votesFor: u0,
Expand All @@ -122,7 +125,7 @@
(let
(
(proposalRecord (unwrap! (map-get? Proposals proposalId) ERR_PROPOSAL_NOT_FOUND))
(proposalBlock (get startBlock proposalRecord))
(proposalBlock (get startBlockStx proposalRecord))
(proposalBlockHash (unwrap! (get-block-hash proposalBlock) ERR_RETRIEVING_START_BLOCK_HASH))
(senderBalance (unwrap! (at-block proposalBlockHash (contract-call? .aibtc-token get-balance tx-sender)) ERR_FETCHING_TOKEN_DATA))
)
Expand Down Expand Up @@ -201,7 +204,7 @@
(let
(
(proposalRecord (unwrap! (map-get? Proposals proposalId) ERR_PROPOSAL_NOT_FOUND))
(proposalBlockHash (unwrap! (get-block-hash (get startBlock proposalRecord)) ERR_RETRIEVING_START_BLOCK_HASH))
(proposalBlockHash (unwrap! (get-block-hash (get startBlockStx proposalRecord)) ERR_RETRIEVING_START_BLOCK_HASH))
)
(at-block proposalBlockHash (contract-call? .aibtc-token get-balance who))
)
Expand Down Expand Up @@ -244,7 +247,6 @@
))
)

;; get block hash by height
(define-private (get-block-hash (blockHeight uint))
(get-block-info? id-header-hash blockHeight)
)
Expand Down
21 changes: 12 additions & 9 deletions contracts/dao/extensions/aibtc-core-proposals.clar
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
(define-constant SELF (as-contract tx-sender))
(define-constant VOTING_PERIOD u144) ;; 144 Bitcoin blocks, ~1 day
(define-constant VOTING_QUORUM u95) ;; 95% of liquid supply
(define-constant DEPLOYED_AT burn-block-height)

;; error messages
(define-constant ERR_NOT_DAO_OR_EXTENSION (err u3000))
Expand All @@ -32,6 +33,7 @@
(define-constant ERR_VOTE_TOO_SOON (err u3009))
(define-constant ERR_VOTE_TOO_LATE (err u3010))
(define-constant ERR_ALREADY_VOTED (err u3011))
(define-constant ERR_FIRST_VOTING_PERIOD (err u3012))

;; contracts used for voting calculations
(define-constant VOTING_TOKEN_DEX .aibtc-token-dex)
Expand All @@ -46,8 +48,9 @@
createdAt: uint, ;; block height
caller: principal, ;; contract caller
creator: principal, ;; proposal creator (tx-sender)
startBlock: uint, ;; block height
endBlock: uint, ;; block height
startBlockStx: uint, ;; block height for at-block calls
startBlock: uint, ;; burn block height
endBlock: uint, ;; burn block height
votesFor: uint, ;; total votes for
votesAgainst: uint, ;; total votes against
liquidTokens: uint, ;; liquid tokens
Expand Down Expand Up @@ -77,6 +80,8 @@
(proposalContract (contract-of proposal))
(liquidTokens (try! (get-liquid-supply block-height)))
)
;; at least one voting period passed
(asserts! (>= burn-block-height (+ DEPLOYED_AT VOTING_PERIOD)) ERR_FIRST_VOTING_PERIOD)
;; caller has the required balance
(asserts! (> (unwrap! (contract-call? .aibtc-token get-balance tx-sender) ERR_FETCHING_TOKEN_DATA) u0) ERR_INSUFFICIENT_BALANCE)
;; proposal was not already executed
Expand All @@ -88,6 +93,7 @@
proposal: proposalContract,
creator: tx-sender,
liquidTokens: liquidTokens,
startBlockStx: block-height,
startBlock: burn-block-height,
endBlock: (+ burn-block-height VOTING_PERIOD)
}
Expand All @@ -97,6 +103,7 @@
createdAt: burn-block-height,
caller: contract-caller,
creator: tx-sender,
startBlockStx: block-height,
startBlock: burn-block-height,
endBlock: (+ burn-block-height VOTING_PERIOD),
votesFor: u0,
Expand All @@ -112,10 +119,9 @@
(
(proposalContract (contract-of proposal))
(proposalRecord (unwrap! (map-get? Proposals proposalContract) ERR_PROPOSAL_NOT_FOUND))
(proposalBlock (get startBlock proposalRecord))
(proposalBlock (get startBlockStx proposalRecord))
(proposalBlockHash (unwrap! (get-block-hash proposalBlock) ERR_RETRIEVING_START_BLOCK_HASH))
(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))
)
;; caller has the required balance
(asserts! (> senderBalance u0) ERR_INSUFFICIENT_BALANCE)
Expand Down Expand Up @@ -154,8 +160,6 @@
(
(proposalContract (contract-of proposal))
(proposalRecord (unwrap! (map-get? Proposals proposalContract) ERR_PROPOSAL_NOT_FOUND))
(tokenTotalSupply (unwrap! (contract-call? .aibtc-token get-total-supply) ERR_FETCHING_TOKEN_DATA))
(treasuryBalance (unwrap! (contract-call? .aibtc-token get-balance .aibtc-treasury) ERR_FETCHING_TOKEN_DATA))
;; if VOTING_QUORUM <= ((votesFor * 100) / liquidTokens)
(votePassed (<= VOTING_QUORUM (/ (* (get votesFor proposalRecord) u100) (get liquidTokens proposalRecord))))
)
Expand Down Expand Up @@ -194,7 +198,7 @@
(let
(
(proposalRecord (unwrap! (map-get? Proposals (contract-of proposal)) ERR_PROPOSAL_NOT_FOUND))
(proposalBlockHash (unwrap! (get-block-hash (get startBlock proposalRecord)) ERR_RETRIEVING_START_BLOCK_HASH))
(proposalBlockHash (unwrap! (get-block-hash (get startBlockStx proposalRecord)) ERR_RETRIEVING_START_BLOCK_HASH))
)
(at-block proposalBlockHash (contract-call? .aibtc-token get-balance who))
)
Expand Down Expand Up @@ -233,7 +237,6 @@
))
)

;; get block hash by height
(define-private (get-block-hash (blockHeight uint))
(get-block-info? id-header-hash blockHeight)
)
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao/extensions/aibtc-token-owner.clar
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
;; constants
;;

(define-constant ERR_UNAUTHORIZED (err u401))
(define-constant ERR_UNAUTHORIZED (err u7000))

;; public functions
;;
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao/proposals/aibtc-base-disable-extension.clar
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(impl-trait .aibtcdev-dao-traits-v1.proposal)

(define-constant ERR_EXTENSION_NOT_FOUND (err u404))
(define-constant ERR_EXTENSION_NOT_FOUND (err u3003))

(define-public (execute (sender principal))
;; disables an extension in the DAO
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao/proposals/aibtc-base-enable-extension.clar
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(impl-trait .aibtcdev-dao-traits-v1.proposal)

(define-constant ERR_EXTENSION_NOT_FOUND (err u404))
(define-constant ERR_EXTENSION_NOT_FOUND (err u3003))

(define-public (execute (sender principal))
;; disables an extension in the DAO
Expand Down
2 changes: 1 addition & 1 deletion contracts/dao/proposals/aibtc-base-replace-extension.clar
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(impl-trait .aibtcdev-dao-traits-v1.proposal)

(define-constant ERR_EXTENSION_NOT_FOUND (err u404))
(define-constant ERR_EXTENSION_NOT_FOUND (err u3003))

(define-public (execute (sender principal))
;; replaces an extension in the DAO
Expand Down
Loading

0 comments on commit cf57507

Please sign in to comment.