Source Code

;; Braille Press - Braille book production fund
;; Halal - spreading knowledge to blind
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var book-count uint u0)
(define-data-var total-funded uint u0)
(define-map books uint { title: (string-utf8 100), pages: uint, cost: uint, funded: bool, block: uint })
(define-map sponsors { book-id: uint, donor: principal } uint)
(define-public (add-book (title (string-utf8 100)) (pages uint) (cost uint))
  (let ((id (+ (var-get book-count) u1)))
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-AUTH)
    (map-set books id { title: title, pages: pages, cost: cost, funded: false, block: stacks-block-height })
    (var-set book-count id) (ok id)))
(define-public (sponsor-book (book-id uint) (amount uint))
  (let ((b (unwrap! (map-get? books book-id) ERR-NONE)))
    (try! (stx-transfer? amount tx-sender CONTRACT-OWNER))
    (map-set sponsors { book-id: book-id, donor: tx-sender } amount)
    (var-set total-funded (+ (var-get total-funded) amount)) (ok amount)))
(define-public (mark-funded (book-id uint))
  (let ((b (unwrap! (map-get? books book-id) ERR-NONE)))
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-AUTH)
    (map-set books book-id (merge b { funded: true })) (ok true)))
(define-read-only (get-book (id uint)) (map-get? books id))
(define-read-only (get-stats) (ok { books: (var-get book-count), funded: (var-get total-funded) }))

Functions (5)

FunctionAccessArgs
add-bookpublictitle: (string-utf8 100
sponsor-bookpublicbook-id: uint, amount: uint
mark-fundedpublicbook-id: uint
get-bookread-onlyid: uint
get-statsread-only