;; title: welsh-credits-rc6
;; authors: rozar.btc
;; summary: A permissionless credit-tracking system supporting variable-amount
;; physical note transfers. Notes are redeemed via redeem-note. Deposits and
;; withdrawals wrap the underlying token. Uses an external signer utility.
(define-constant MAX_BATCH_SIZE u200)
(define-constant ERR_INSUFFICIENT_BALANCE (err u400))
(define-constant ERR_UNAUTHORIZED (err u401))
(define-constant ERR_TOO_MANY_OPERATIONS (err u402))
(define-map balances principal uint)
;; Raw balance helper (returns uint, no (ok ...))
(define-private (get-internal-balance (who principal))
(default-to u0 (map-get? balances who))
)
;; Internal credit transfer logic
(define-private (do-internal-transfer (from principal) (to principal) (amount uint))
(if (is-eq from to)
(ok true)
(let (
(current-from-balance (get-internal-balance from))
(current-to-balance (get-internal-balance to))
)
(asserts! (>= current-from-balance amount) ERR_INSUFFICIENT_BALANCE)
(map-set balances from (- current-from-balance amount))
(map-set balances to (+ current-to-balance amount))
(ok true)
)
)
)
;; Helper for batch note redemption
(define-private (try-redeem-note
(operation { signature: (buff 65), amount: uint, uuid: (string-ascii 36), to: principal })
)
(match (redeem-note
(get signature operation)
(get amount operation)
(get uuid operation)
(get to operation)
)
success true
error false
)
)
(define-read-only (get-name) (contract-call? 'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token get-name))
(define-read-only (get-symbol) (contract-call? 'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token get-symbol))
(define-read-only (get-decimals) (contract-call? 'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token get-decimals))
(define-read-only (get-token-uri) (contract-call? 'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token get-token-uri))
(define-read-only (get-total-supply) (contract-call? 'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token get-total-supply))
(define-read-only (get-balance (owner principal))
(ok (get-internal-balance owner))
)
(define-public (deposit (amount uint) (recipient (optional principal)))
(let (
(sender tx-sender)
(effective-recipient (default-to sender recipient))
(current-recipient-balance (get-internal-balance effective-recipient))
)
(try! (contract-call?
'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token
transfer amount sender (as-contract tx-sender) none))
(map-set balances effective-recipient (+ current-recipient-balance amount))
(print {event: "deposit", sender: sender, recipient: effective-recipient, amount: amount})
(ok true)
)
)
(define-public (withdraw (amount uint) (recipient (optional principal)))
(let (
(owner tx-sender)
(effective-recipient (default-to owner recipient))
(current-owner-balance (get-internal-balance owner))
)
(asserts! (>= current-owner-balance amount) ERR_INSUFFICIENT_BALANCE)
(map-set balances owner (- current-owner-balance amount))
(try! (as-contract (contract-call?
'SP3NE50GEXFG9SZGTT51P40X2CKYSZ5CC4ZTZ7A2G.welshcorgicoin-token
transfer amount tx-sender effective-recipient none)))
(print {event: "withdraw", owner: owner, recipient: effective-recipient, amount: amount})
(ok true)
)
)
(define-public (transfer (amount uint) (from principal) (to principal) (memo (optional (buff 34))))
(let ((sender tx-sender))
(asserts! (is-eq sender from) ERR_UNAUTHORIZED)
(try! (do-internal-transfer from to amount))
(print {event: "transfer", from: from, to: to, amount: amount, memo: memo})
(ok true)
)
)
(define-public (redeem-note
(signature (buff 65))
(amount uint)
(uuid (string-ascii 36))
(to principal)
)
(let (
(opcode (concat "TRANSFER_" (int-to-ascii amount)))
(signer-principal (try! (contract-call?
'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-rc9
submit signature opcode uuid)))
)
(try! (do-internal-transfer signer-principal to amount))
(print {event: "redeem-note", action: "transfer", from: signer-principal, to: to, amount: amount, uuid: uuid})
(ok true)
)
)
(define-public (batch-redeem-notes
(operations (list 200 {
signature: (buff 65),
amount: uint,
uuid: (string-ascii 36),
to: principal
}))
)
(begin
(asserts! (<= (len operations) MAX_BATCH_SIZE) ERR_TOO_MANY_OPERATIONS)
(ok (map try-redeem-note operations))
)
)