Source Code

;; garden-share -- community garden plot sharing platform
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var plot-count uint u0)
(define-data-var harvest-count uint u0)
(define-map plots uint { owner: principal, location: (string-utf8 100), size-sqm: uint, crop: (string-utf8 50), available: bool })
(define-map harvests uint { plot-id: uint, kg-harvested: uint, block: uint })

(define-public (register-plot (location (string-utf8 100)) (size-sqm uint) (crop (string-utf8 50)))
  (let ((id (+ (var-get plot-count) u1)))
    (var-set plot-count id)
    (map-set plots id { owner: tx-sender, location: location, size-sqm: size-sqm, crop: crop, available: true })
    (ok id)))

(define-public (toggle-availability (id uint))
  (let ((p (unwrap! (map-get? plots id) ERR-NONE)))
    (asserts! (is-eq tx-sender (get owner p)) ERR-AUTH)
    (map-set plots id (merge p { available: (not (get available p)) })) (ok true)))

(define-public (log-harvest (plot-id uint) (kg-harvested uint))
  (let ((id (+ (var-get harvest-count) u1)))
    (var-set harvest-count id)
    (map-set harvests id { plot-id: plot-id, kg-harvested: kg-harvested, block: stacks-block-height })
    (ok id)))

(define-read-only (get-plot (id uint)) (map-get? plots id))
(define-read-only (get-harvest (id uint)) (map-get? harvests id))
(define-read-only (get-plot-count) (var-get plot-count))

Functions (6)

FunctionAccessArgs
register-plotpubliclocation: (string-utf8 100
toggle-availabilitypublicid: uint
log-harvestpublicplot-id: uint, kg-harvested: uint
get-plotread-onlyid: uint
get-harvestread-onlyid: uint
get-plot-countread-only