Skip to content

Commit

Permalink
fix: add map to track redeemed balances per user
Browse files Browse the repository at this point in the history
  • Loading branch information
whoabuddy committed Apr 2, 2024
1 parent 61620ed commit c8a06dd
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions contracts/extensions/ccd012-redemption-mia.clar
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
(define-constant ERR_ALREADY_ENABLED (err u12004))
(define-constant ERR_NOT_ENABLED (err u12005))
(define-constant ERR_BALANCE_NOT_FOUND (err u12006))
(define-constant ERR_NOTHING_TO_REDEEM (err u12007))

;; DATA VARS

Expand All @@ -25,6 +26,14 @@
(define-data-var contractBalance uint u0)
(define-data-var redemptionRatio uint u0)

;; DATA MAPS

;; track totals per principal
(define-map RedemptionClaims
principal ;; address
uint ;; total redemption amount
)

;; PUBLIC FUNCTIONS

(define-public (is-dao-or-extension)
Expand Down Expand Up @@ -69,16 +78,21 @@
(define-public (redeem-mia)
(let
(
(balanceV2 (unwrap! (contract-call? .miamicoin-token-v2 get-balance tx-sender) ERR_BALANCE_NOT_FOUND))
(redemptionAmount (unwrap! (get-redemption-amount balanceV2) ERR_PANIC)) ;; TODO: more specific err?
(userAddress tx-sender)
(balanceV2 (unwrap! (contract-call? .miamicoin-token-v2 get-balance userAddress) ERR_BALANCE_NOT_FOUND))
(redemptionAmount (unwrap! (get-redemption-amount balanceV2) ERR_NOTHING_TO_REDEEM))
(redemptionClaims (default-to u0 (get-redemption-claims (some userAddress))))
)
;; check if redemptions are enabled
(asserts! (var-get redemptionsEnabled) ERR_NOT_ENABLED)
;; check that redemption amount is > 0
(asserts! (> redemptionAmount u0) ERR_NOTHING_TO_REDEEM)
;; burn MIA
(try! (contract-call? .miamicoin-token-v2 burn balanceV2 tx-sender))
(try! (contract-call? .miamicoin-token-v2 burn balanceV2 userAddress))
;; transfer STX
(try! (as-contract (stx-transfer? redemptionAmount tx-sender userAddress)))
(try! (as-contract (stx-transfer? redemptionAmount userAddress userAddress)))
;; update redemption claims
(map-set RedemptionClaims userAddress (+ redemptionClaims redemptionAmount))
(ok true)
)
)
Expand Down Expand Up @@ -123,6 +137,13 @@
)
)

(define-read-only (get-redemption-claims (address (optional principal)))
(if (is-some address)
(map-get? RedemptionClaims (unwrap! address none))
(map-get? RedemptionClaims tx-sender)
)
)

;; TODO: read-only that returns all post conditions in one call

;; PRIVATE FUNCTIONS

0 comments on commit c8a06dd

Please sign in to comment.