Skip to content

Commit

Permalink
refactor(contract): update buy and sell functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman-zishan committed Dec 2, 2023
1 parent e5ebb72 commit 6c5d8ba
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 89 deletions.
185 changes: 122 additions & 63 deletions contracts/keys.clar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

;; title: keys
;; version:
;; summary:
;; version: 1.0.0
;; summary:
;; description:

;; traits
Expand All @@ -17,6 +17,14 @@
;; errors
;;
(define-constant err-unauthorised (err u401))
;; Only the shares' subject can buy the first share
(define-constant err-first-buy (err u500))
;; Insufficient payment
(define-constant err-insufficient-payment (err u501))
;; Cannot sell last share
(define-constant err-cannot-sell-last-share (err u502))
;; Insufficient shares
(define-constant err-insufficient-shares (err u503))

;; data vars
;;
Expand Down Expand Up @@ -65,59 +73,110 @@

)

(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 (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))

)
(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
(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))

(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)
)
)

(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 (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))
(recipient tx-sender)
)
(asserts! (> supply amount) err-cannot-sell-last-share)
(asserts! (>= balance amount) err-insufficient-shares)

;; Send final price to tx-senders
(try! (stx-transfer? (- price protocolFee subjectFee) (as-contract tx-sender) recipient))
;; Send protocol fee to protocol contract
(try! (stx-transfer? protocolFee (as-contract tx-sender) (var-get protocolFeeDestination)))
;; Send subject fee to subject
(try! (stx-transfer? subjectFee (as-contract 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))
Expand All @@ -137,55 +196,55 @@

(define-read-only (get-keys-balance (subject principal) (holder principal))
;; Return the keysBalance for the given subject and holder
(map-get? keysBalance (tuple (subject subject) (holder holder)))
(default-to u0 (map-get? keysBalance { subject: subject, holder: holder }))
)

(define-read-only (get-keys-supply (subject principal))
;; Return the keysSupply for the given subject
(map-get? keysSupply (tuple (subject subject)))
(default-to u0 (map-get? keysSupply { subject: subject }))
)

(define-read-only (get-buy-price (subject principal) (amount uint))
;; Implement buy price logic

(let
(
(supply (unwrap! (get-keys-supply subject) (err u3)))
(supply (get-keys-supply subject))
)
(ok (get-price supply amount))
(get-price supply amount)
)
)

(define-read-only (get-sell-price (subject principal) (amount uint))
;; Implement sell price logic

(let
(
(supply (unwrap! (get-keys-supply subject) (err u4)))
(supply (get-keys-supply subject))
)
(ok (get-price (- supply amount) amount))
(get-price (- supply amount) amount)
)
)

(define-read-only (get-buy-price-after-fee (subject principal) (amount uint))
;; Implement buy price logic

(let
(
(price (unwrap! (get-buy-price subject amount) (err u3)))
(price (get-buy-price subject amount))
(protocolFee (/ (* price (var-get protocolFeePercent)) u100))
(subjectFee (/ (* price (var-get subjectFeePercent)) u100))
)
(ok (+ price protocolFee subjectFee))
(+ price protocolFee subjectFee)
)
)

(define-read-only (get-sell-price-after-fee (subject principal) (amount uint))
;; Implement sell price logic

(let
(
(price (unwrap! (get-sell-price subject amount) (err u3)))
(price (get-sell-price subject amount))
(protocolFee (/ (* price (var-get protocolFeePercent)) u100))
(subjectFee (/ (* price (var-get subjectFeePercent)) u100))
)
(ok (- price protocolFee subjectFee))
(- price protocolFee subjectFee)
)
)

Expand Down
26 changes: 0 additions & 26 deletions tests/keys_test.ts

This file was deleted.

0 comments on commit 6c5d8ba

Please sign in to comment.