;; DeFi Protocol Registry
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-NOT-AUTHORIZED (err u1))
(define-constant ERR-ALREADY-EXISTS (err u2))
(define-data-var protocol-count uint u0)
(define-map protocols uint {name: (string-ascii 50), tvl: uint, active: bool})
(define-map protocol-by-name (string-ascii 50) uint)
(define-public (register-protocol (name (string-ascii 50)) (tvl uint))
(let ((id (var-get protocol-count)))
(asserts! (is-none (map-get? protocol-by-name name)) ERR-ALREADY-EXISTS)
(map-set protocols id {name: name, tvl: tvl, active: true})
(map-set protocol-by-name name id)
(var-set protocol-count (+ id u1))
(ok id)))
(define-public (update-tvl (id uint) (tvl uint))
(let ((p (unwrap! (map-get? protocols id) ERR-NOT-AUTHORIZED)))
(map-set protocols id (merge p {tvl: tvl}))
(ok tvl)))
(define-public (set-active (id uint) (active bool))
(let ((p (unwrap! (map-get? protocols id) ERR-NOT-AUTHORIZED)))
(asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED)
(map-set protocols id (merge p {active: active}))
(ok active)))
(define-read-only (get-protocol (id uint))
(map-get? protocols id))
(define-read-only (get-protocol-count)
(var-get protocol-count))