Skip to content

Commit

Permalink
feat(tests): finish up main test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman-zishan committed Dec 3, 2023
1 parent 35c6dcd commit 143bf80
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 152 deletions.
6 changes: 2 additions & 4 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ authors = []
telemetry = true
cache_dir = './.cache'
requirements = []
[contracts.hello-world]
path = 'contracts/hello-world.clar'
clarity_version = 2
epoch = 2.1

[contracts.keys]
path = 'contracts/keys.clar'
clarity_version = 2
epoch = 2.1


[repl.analysis]
passes = ['check_checker']

Expand Down
36 changes: 0 additions & 36 deletions contracts/hello-world.clar

This file was deleted.

112 changes: 29 additions & 83 deletions contracts/keys.clar
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@
(define-constant err-first-buy (err u500))
;; Insufficient payment
(define-constant err-insufficient-payment (err u501))
;; Insufficient contract balance
(define-constant err-insufficient-contract-balance (err u502))
;; Cannot sell last share
(define-constant err-cannot-sell-last-share (err u502))
(define-constant err-cannot-sell-last-share (err u503))
;; Insufficient shares
(define-constant err-insufficient-shares (err u503))
(define-constant err-insufficient-shares (err u504))
;; Generic invalid value
(define-constant err-invalid-value (err u504))
(define-constant err-invalid-value (err u505))

;; data vars
;;
(define-data-var protocolFeePercent uint u5)
(define-data-var subjectFeePercent uint u5)
(define-data-var protocolFeePercent uint u10)
(define-data-var subjectFeePercent uint u10)
(define-data-var protocolFeeDestination principal contract-owner)

;; data maps
Expand Down Expand Up @@ -78,50 +80,31 @@

)

;; (define-public (buy-keys (subject principal) (amount uint))
;; (let
;; (
;; (supply (default-to u0 (map-get? keysSupply { subject: subject })))
;; (price (get-price supply amount))
;; )
;; (if (or (> supply u0) (is-eq tx-sender subject))
;; (begin
;; (match (stx-transfer? price tx-sender (as-contract tx-sender))
;; success
;; (begin
;; (map-set keysBalance { subject: subject, holder: tx-sender }
;; (+ (default-to u0 (map-get? keysBalance { subject: subject, holder: tx-sender })) amount)
;; )
;; (map-set keysSupply { subject: subject } (+ supply amount))
;; (ok true)
;; )
;; error
;; (err u2)
;; )
;; )
;; (err u1)
;; )
;; )
;; )

(define-public (buy-keys (subject principal) (amount uint))
(let
(
(supply (get-keys-supply subject))
(price (get-price supply amount))
(protocolFee (/ (* price (var-get protocolFeePercent)) u100))
(subjectFee (/ (* price (var-get subjectFeePercent)) u100))
(protocolFee (/ (* price (var-get protocolFeePercent)) u1000000))
(subjectFee (/ (* price (var-get subjectFeePercent)) u1000000))

)

(asserts! (or (> supply u0) (is-eq tx-sender subject)) err-first-buy)
(asserts! (> (stx-get-balance tx-sender) (+ price protocolFee subjectFee)) err-insufficient-payment)
;; Retain base amount in contract itself
(print {supply: supply, price:price, protocolFee:protocolFee, subjectFee:subjectFee})
(try! (stx-transfer? price tx-sender (as-contract tx-sender)))
;; Send protocol fee to protocol contract
(try! (stx-transfer? protocolFee tx-sender (var-get protocolFeeDestination)))
;; Send subject fee to subject
(try! (stx-transfer? subjectFee tx-sender subject))
(print {supply: supply, price:price, protocolFee:protocolFee, subjectFee:subjectFee, amount: amount})
(if (> price u0)
(begin
;; Retain base amount in contract itself
(try! (stx-transfer? price tx-sender (as-contract tx-sender)))
;; Send protocol fee to protocol contract
(try! (stx-transfer? protocolFee tx-sender (var-get protocolFeeDestination)))
;; Send subject fee to subject
(try! (stx-transfer? subjectFee tx-sender subject))
)
true
)

(map-set keysBalance { subject: subject, holder: tx-sender }
(+ (default-to u0 (map-get? keysBalance { subject: subject, holder: tx-sender })) amount)
Expand All @@ -131,70 +114,33 @@
)
)

;; (define-public (sell-keys (subject principal) (amount uint))
;; (let
;; (
;; (balance (default-to u0 (map-get? keysBalance { subject: subject, holder: tx-sender })))
;; (supply (default-to u0 (map-get? keysSupply { subject: subject })))
;; (price (get-price supply amount))
;; (recipient tx-sender)
;; )
;; (if (and (>= balance amount) (or (> supply u0) (is-eq tx-sender subject)))
;; (begin
;; (match (as-contract (stx-transfer? price tx-sender recipient))
;; success
;; (begin
;; (map-set keysBalance { subject: subject, holder: tx-sender } (- balance amount))
;; (map-set keysSupply { subject: subject } (- supply amount))
;; (ok true)
;; )
;; error
;; (err u2)
;; )
;; )
;; (err u1)
;; )
;; )
;; )

(define-public (sell-keys (subject principal) (amount uint))
(let
(
(balance (get-keys-balance subject tx-sender))
(supply (get-keys-supply subject))
(price (get-price (- supply amount) amount))
(protocolFee (/ (* price (var-get protocolFeePercent)) u100))
(subjectFee (/ (* price (var-get subjectFeePercent)) u100))
(protocolFee (/ (* price (var-get protocolFeePercent)) u1000000))
(subjectFee (/ (* price (var-get subjectFeePercent)) u1000000))
(recipient tx-sender)
)
(asserts! (> supply amount) err-cannot-sell-last-share)
(asserts! (>= balance amount) err-insufficient-shares)
;;(asserts! (> (stx-get-balance (as-contract tx-sender)) (+ price protocolFee subjectFee)) err-insufficient-contract-balance)

;; Send final price to tx-senders
(try! (stx-transfer? (- price protocolFee subjectFee) (as-contract tx-sender) recipient))
(try! (as-contract (stx-transfer? (- price protocolFee subjectFee) tx-sender recipient)))
;; Send protocol fee to protocol contract
(try! (stx-transfer? protocolFee (as-contract tx-sender) (var-get protocolFeeDestination)))
(try! (as-contract (stx-transfer? protocolFee tx-sender (var-get protocolFeeDestination))))
;; Send subject fee to subject
(try! (stx-transfer? subjectFee (as-contract tx-sender) subject))
(try! (as-contract (stx-transfer? subjectFee tx-sender subject)))

(map-set keysBalance { subject: subject, holder: tx-sender } (- balance amount))
(map-set keysSupply { subject: subject } (- supply amount))
(ok true)
)
)

;; read only functions
;;
;; (define-read-only (get-price (supply uint) (amount uint))
;; (let
;; (
;; (base-price u10)
;; (price-change-factor u100)
;; (adjusted-supply (+ supply amount))
;; )
;; (+ base-price (* amount (/ (* adjusted-supply adjusted-supply) price-change-factor)))
;; )
;; )
(define-read-only (get-price (supply uint) (amount uint))
(let
(
Expand All @@ -206,7 +152,7 @@
(/ (* (* (+ (- supply u1) amount) (+ supply amount)) (+ (* u2 (+ (- supply u1) amount )) u1)) u6)))
(summation (- sum2 sum1))
)
(/ (* summation u1000000000000000000) u16000) ;; equivalent to 'summation * 1 ether / 16000' in Solidity
(/ (* summation u1000000) u16) ;; equivalent to 'summation * 1 ether / 16000' in Solidity

)
)
Expand Down
34 changes: 34 additions & 0 deletions history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,37 @@
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys get-protocol-destination tx-sender)
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys get-protocol-destination)
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys get-owner)
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5 u1)
(contract-call? ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
(contract-call? ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
(contract-call? ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM .keys buy-keys tx-sender u1)
(contract-call? .keys buy-keys tx-sender u1)
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::reload
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::reload
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::reload
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::reload
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::help
::get_assets_map
::get_assets_maps
::reload
::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys tx-sender u1)
::set_tx_sender ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5 u1)
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys buy-keys 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5 u1)
::get_asset_maps
::get_assets_maps
(contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.keys sell-keys 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5 u1)
Loading

0 comments on commit 143bf80

Please sign in to comment.