Source Code

;; water-well -- clean water well construction fund
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var well-count uint u0)
(define-data-var total-funded uint u0)
(define-map wells uint { builder: principal, village: (string-utf8 100), cost: uint, raised: uint, status: (string-ascii 20) })
(define-map well-donors { well-id: uint, donor: principal } uint)

(define-public (propose-well (village (string-utf8 100)) (cost uint))
  (let ((id (+ (var-get well-count) u1)))
    (var-set well-count id)
    (map-set wells id { builder: tx-sender, village: village, cost: cost, raised: u0, status: "fundraising" })
    (ok id)))

(define-public (donate-to-well (well-id uint) (amount uint))
  (let ((w (unwrap! (map-get? wells well-id) ERR-NONE))
        (prev (default-to u0 (map-get? well-donors { well-id: well-id, donor: tx-sender }))))
    (var-set total-funded (+ (var-get total-funded) amount))
    (map-set well-donors { well-id: well-id, donor: tx-sender } (+ prev amount))
    (map-set wells well-id (merge w { raised: (+ (get raised w) amount) }))
    (ok amount)))

(define-public (mark-built (well-id uint))
  (let ((w (unwrap! (map-get? wells well-id) ERR-NONE)))
    (asserts! (is-eq tx-sender (get builder w)) ERR-AUTH)
    (map-set wells well-id (merge w { status: "built" })) (ok true)))

(define-read-only (get-well (id uint)) (map-get? wells id))
(define-read-only (get-total-funded) (var-get total-funded))

Functions (5)

FunctionAccessArgs
propose-wellpublicvillage: (string-utf8 100
donate-to-wellpublicwell-id: uint, amount: uint
mark-builtpublicwell-id: uint
get-wellread-onlyid: uint
get-total-fundedread-only